]> source.dussan.org Git - redmine.git/commitdiff
Prevents n SQL queries (n = project count) on cross-project issues list.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Mon, 20 Dec 2010 20:03:22 +0000 (20:03 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Mon, 20 Dec 2010 20:03:22 +0000 (20:03 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4550 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/query.rb
test/unit/query_test.rb

index 7c246f9aec7e508eef4c2890097993e7b39edb46..94905453306c380937bf2305958130b339a79de6 100644 (file)
@@ -187,10 +187,18 @@ class Query < ActiveRecord::Base
     if project
       user_values += project.users.sort.collect{|s| [s.name, s.id.to_s] }
     else
-      project_ids = Project.all(:conditions => Project.visible_by(User.current)).collect(&:id)
-      if project_ids.any?
-        # members of the user's projects
-        user_values += User.active.find(:all, :conditions => ["#{User.table_name}.id IN (SELECT DISTINCT user_id FROM members WHERE project_id IN (?))", project_ids]).sort.collect{|s| [s.name, s.id.to_s] }
+      all_projects = Project.visible.all
+      if all_projects.any?
+        # members of visible projects
+        user_values += User.active.find(:all, :conditions => ["#{User.table_name}.id IN (SELECT DISTINCT user_id FROM members WHERE project_id IN (?))", all_projects.collect(&:id)]).sort.collect{|s| [s.name, s.id.to_s] }
+          
+        # project filter
+        project_values = []
+        Project.project_tree(all_projects) do |p, level|
+          prefix = (level > 0 ? ('--' * level + ' ') : '')
+          project_values << ["#{prefix}#{p.name}", p.id.to_s]
+        end
+        @available_filters["project_id"] = { :type => :list, :order => 1, :values => project_values} unless project_values.empty?
       end
     end
     @available_filters["assigned_to_id"] = { :type => :list_optional, :order => 4, :values => user_values } unless user_values.empty?
@@ -225,12 +233,6 @@ class Query < ActiveRecord::Base
         @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => system_shared_versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } }
       end
       add_custom_fields_filters(IssueCustomField.find(:all, :conditions => {:is_filter => true, :is_for_all => true}))
-      # project filter
-      project_values = Project.all(:conditions => Project.visible_by(User.current), :order => 'lft').map do |p|
-        pre = (p.level > 0 ? ('--' * p.level + ' ') : '')
-        ["#{pre}#{p.name}",p.id.to_s]
-      end
-      @available_filters["project_id"] = { :type => :list, :order => 1, :values => project_values}
     end
     @available_filters
   end
index f5d9847d6e93b93a350170e24845133c4eb42df1..173367a4bf7261b04431d790802c8cee051c0a25 100644 (file)
@@ -382,6 +382,12 @@ class QueryTest < ActiveSupport::TestCase
       assert users[:values].map{|u|u[1]}.include?("3")
     end
 
+    should "include visible projects in cross-project view" do
+      projects = @query.available_filters["project_id"]
+      assert_not_nil projects
+      assert projects[:values].map{|u|u[1]}.include?("1")
+    end
+
     context "'member_of_group' filter" do
       should "be present" do
         assert @query.available_filters.keys.include?("member_of_group")