Project

General

Profile

Patch #821 » 311_search_all_projects_default.patch

W Snyder, 2008-03-16 02:16

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
    
app/controllers/search_controller.rb (working copy)
28 28
    @question.strip!
29 29
    @all_words = params[:all_words] || (params[:submit] ? false : true)
30 30
    @titles_only = !params[:titles_only].nil?
31
    @all_projects = params[:all_projects] || (params[:submit] ? false : true)
31 32
    
32 33
    offset = nil
33 34
    begin; offset = params[:offset].to_time if params[:offset]; rescue; end
......
39 40
    end
40 41
    
41 42
    if @project
42
      # only show what the user is allowed to view
43
      @object_types = %w(issues news documents changesets wiki_pages messages)
44
      @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, @project)}
45
      
46
      @scope = @object_types.select {|t| params[t]}
47
      @scope = @object_types if @scope.empty?
43
      object_types_and_scope(@project)
44
      if @all_projects
45
        @projects = Project.find(:all, :conditions => Project.visible_by(User.current))
46
      else
47
        @projects = [@project]
48
      end
48 49
    else
49 50
      @object_types = @scope = %w(projects)
50 51
    end
......
62 63
      like_tokens = @tokens.collect {|w| "%#{w.downcase}%"}      
63 64
      @results = []
64 65
      limit = 10
65
      if @project        
66
        @scope.each do |s|
67
          @results += s.singularize.camelcase.constantize.search(like_tokens, @project,
68
            :all_words => @all_words,
69
            :titles_only => @titles_only,
70
            :limit => (limit+1),
71
            :offset => offset,
72
            :before => params[:previous].nil?)
66
      if @projects
67
        @projects.each do |project|
68
          object_types_and_scope(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?)
76
          end
73 77
        end
78
        object_types_and_scope(@project) if @project #make sure object types and scope match current project last
74 79
        @results = @results.sort {|a,b| b.event_datetime <=> a.event_datetime}
75 80
        if params[:previous].nil?
76 81
          @pagination_previous_date = @results[0].event_datetime if offset && @results[0]
......
108 113
  rescue ActiveRecord::RecordNotFound
109 114
    render_404
110 115
  end
116
  
117
  # only show what the user is allowed to view
118
  def object_types_and_scope(project)
119
    @object_types = %w(issues news documents changesets wiki_pages messages)
120
    @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, @project)}
121
    @scope = @object_types.select {|t| params[t]}
122
    @scope = @object_types if @scope.empty?
123
  end
111 124
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 %>
(2-2/5)