search_all_projects.diff

Angel Sciortino, 2008-03-10 02:40

Download (4.8 KB)

View differences:

test/functional/search_controller_test.rb (working copy)
25 25
    assert assigns(:results).include?(Project.find(1))
26 26
  end
27 27
  
28
  def test_search_all_projects
29
    @request.session[:user_id] = 1 # admin
30
    get :index, :id => 1, :q => 'Error 281', :all_projects => '1', :submit => 'Search'
31
    results = assigns(:results)
32
    assert_not_nil results
33
    assert_equal 2, results.size
34
  end
35
  
28 36
  def test_search_without_searchable_custom_fields
29 37
    CustomField.update_all "searchable = #{ActiveRecord::Base.connection.quoted_false}"
30 38
    
test/fixtures/issues.yml (working copy)
52 52
  id: 4
53 53
  fixed_version_id: 
54 54
  category_id: 
55
  description: Issue on project 2
55
  description: Issue on project 2. Error 281 was encountered.
56 56
  tracker_id: 1
57 57
  assigned_to_id: 
58 58
  author_id: 2
app/controllers/search_controller.rb (working copy)
26 26
    @question.strip!
27 27
    @all_words = params[:all_words] || (params[:submit] ? false : true)
28 28
    @titles_only = !params[:titles_only].nil?
29
    @all_projects = !params[:all_projects].nil?
29 30
    
30 31
    offset = nil
31 32
    begin; offset = params[:offset].to_time if params[:offset]; rescue; end
......
35 36
      redirect_to :controller => "issues", :action => "show", :id => $1
36 37
      return
37 38
    end
38
    
39

  
39 40
    if params[:id]
40 41
      find_project
41 42
      return unless check_project_privacy
42 43
    end
43
    
44
  
44 45
    if @project
45
      # only show what the user is allowed to view
46
      @object_types = %w(issues news documents changesets wiki_pages messages)
47
      @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, @project)}
48
      
49
      @scope = @object_types.select {|t| params[t]}
50
      @scope = @object_types if @scope.empty?
46
      object_types_and_scope(@project)
47
      if @all_projects
48
        @projects = Project.find(:all, :conditions => Project.visible_by(User.current))
49
      else
50
        @projects = [@project]
51
      end
51 52
    else
52 53
      @object_types = @scope = %w(projects)
53 54
    end
......
65 66
      like_tokens = @tokens.collect {|w| "%#{w.downcase}%"}      
66 67
      @results = []
67 68
      limit = 10
68
      if @project        
69
        @scope.each do |s|
70
          @results += s.singularize.camelcase.constantize.search(like_tokens, @project,
71
            :all_words => @all_words,
72
            :titles_only => @titles_only,
73
            :limit => (limit+1),
74
            :offset => offset,
75
            :before => params[:previous].nil?)
69
      if @projects
70
        @projects.each do |project|
71
          object_types_and_scope(project)
72
          @scope.each do |s|
73
            @results += s.singularize.camelcase.constantize.search(like_tokens, project,
74
              :all_words => @all_words,
75
              :titles_only => @titles_only,
76
              :limit => (limit+1),
77
              :offset => offset,
78
              :before => params[:previous].nil?)
79
          end
76 80
        end
81
        object_types_and_scope(@project) if @project #make sure object types and scope match current project last
77 82
        @results = @results.sort {|a,b| b.event_datetime <=> a.event_datetime}
78 83
        if params[:previous].nil?
79 84
          @pagination_previous_date = @results[0].event_datetime if offset && @results[0]
......
109 114
  rescue ActiveRecord::RecordNotFound
110 115
    render_404
111 116
  end
117
  
118
  # only show what the user is allowed to view
119
  def object_types_and_scope(project)
120
    @object_types = %w(issues news documents changesets wiki_pages messages)
121
    @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, @project)}
122
    @scope = @object_types.select {|t| params[t]}
123
    @scope = @object_types if @scope.empty?
124
  end
112 125
end
app/views/search/index.rhtml (working copy)
11 11
<br />
12 12
<label><%= check_box_tag 'all_words', 1, @all_words %> <%= l(:label_all_words) %></label>
13 13
<label><%= check_box_tag 'titles_only', 1, @titles_only %> <%= l(:label_search_titles_only) %></label>
14
<% if @project %>
15
  <label><%= check_box_tag 'all_projects', 1, @all_projects %> <%= l(:label_project_all) %></label>
16
<% end %>
14 17
</p>
15 18
<%= submit_tag l(:button_submit), :name => 'submit' %>
16 19
<% end %>