]> source.dussan.org Git - redmine.git/commitdiff
Merged r16839 (#26471).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 22 Jul 2017 07:11:26 +0000 (07:11 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 22 Jul 2017 07:11:26 +0000 (07:11 +0000)
git-svn-id: http://svn.redmine.org/redmine/branches/3.4-stable@16845 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/issue_query.rb
test/functional/issues_controller_test.rb

index dedbe8d0a6c68998acda4142a149e808304ac5c9..f852c144e2312f99c7a84acd14719daddf55670a 100644 (file)
@@ -187,18 +187,29 @@ class IssueQuery < Query
     @available_columns += issue_custom_fields.visible.collect {|cf| QueryCustomFieldColumn.new(cf) }
 
     if User.current.allowed_to?(:view_time_entries, project, :global => true)
+      # insert the columns after total_estimated_hours or at the end
       index = @available_columns.find_index {|column| column.name == :total_estimated_hours}
       index = (index ? index + 1 : -1)
-      # insert the column after total_estimated_hours or at the end
+
+      subselect = "SELECT SUM(hours) FROM #{TimeEntry.table_name}" +
+        " JOIN #{Project.table_name} ON #{Project.table_name}.id = #{TimeEntry.table_name}.project_id" +
+        " WHERE (#{TimeEntry.visible_condition(User.current)}) AND #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
+
       @available_columns.insert index, QueryColumn.new(:spent_hours,
-        :sortable => "COALESCE((SELECT SUM(hours) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id), 0)",
+        :sortable => "COALESCE((#{subselect}), 0)",
         :default_order => 'desc',
         :caption => :label_spent_time,
         :totalable => true
       )
+
+      subselect = "SELECT SUM(hours) FROM #{TimeEntry.table_name}" +
+        " JOIN #{Project.table_name} ON #{Project.table_name}.id = #{TimeEntry.table_name}.project_id" +
+        " JOIN #{Issue.table_name} subtasks ON subtasks.id = #{TimeEntry.table_name}.issue_id" +
+        " WHERE (#{TimeEntry.visible_condition(User.current)})" +
+        " AND subtasks.root_id = #{Issue.table_name}.root_id AND subtasks.lft >= #{Issue.table_name}.lft AND subtasks.rgt <= #{Issue.table_name}.rgt"
+
       @available_columns.insert index+1, QueryColumn.new(:total_spent_hours,
-        :sortable => "COALESCE((SELECT SUM(hours) FROM #{TimeEntry.table_name} JOIN #{Issue.table_name} subtasks ON subtasks.id = #{TimeEntry.table_name}.issue_id" +
-          " WHERE subtasks.root_id = #{Issue.table_name}.root_id AND subtasks.lft >= #{Issue.table_name}.lft AND subtasks.rgt <= #{Issue.table_name}.rgt), 0)",
+        :sortable => "COALESCE((#{subselect}), 0)",
         :default_order => 'desc',
         :caption => :label_total_spent_time
       )
@@ -251,15 +262,10 @@ class IssueQuery < Query
 
   # Returns sum of all the issue's time entries hours
   def total_for_spent_hours(scope)
-    total = if group_by_column.try(:name) == :project
-      # TODO: remove this when https://github.com/rails/rails/issues/21922 is fixed
-      # We have to do a custom join without the time_entries.project_id column
-      # that would trigger a ambiguous column name error
-      scope.joins("JOIN (SELECT issue_id, hours FROM #{TimeEntry.table_name}) AS joined_time_entries ON joined_time_entries.issue_id = #{Issue.table_name}.id").
-        sum("joined_time_entries.hours")
-    else
-      scope.joins(:time_entries).sum("#{TimeEntry.table_name}.hours")
-    end
+    total = scope.joins(:time_entries).
+      where(TimeEntry.visible_condition(User.current)).
+      sum("#{TimeEntry.table_name}.hours")
+
     map_total(total) {|t| t.to_f.round(2)}
   end
 
index 31b6a00256132c8470e590ee79d664a6c7c4d63b..38067b4919613d20c0614e479c5fdaefa430ed14 100644 (file)
@@ -941,6 +941,22 @@ class IssuesControllerTest < Redmine::ControllerTest
     assert_equal hours.sort.reverse, hours
   end
 
+  def test_index_sort_by_spent_hours_should_sort_by_visible_spent_hours
+    TimeEntry.delete_all
+    TimeEntry.generate!(:issue => Issue.generate!(:project_id => 1), :hours => 3)
+    TimeEntry.generate!(:issue => Issue.generate!(:project_id => 3), :hours => 4)
+
+    get :index, :params => {:sort => "spent_hours:desc", :c => ['subject','spent_hours']}
+    assert_response :success
+    assert_equal [4.0, 3.0, 0.0], issues_in_list.map(&:spent_hours)[0..2]
+
+    Project.find(3).disable_module!(:time_tracking)
+
+    get :index, :params => {:sort => "spent_hours:desc", :c => ['subject','spent_hours']}
+    assert_response :success
+    assert_equal [3.0, 0.0, 0.0], issues_in_list.map(&:spent_hours)[0..2]
+  end
+
   def test_index_sort_by_total_spent_hours
     get :index, :params => {
         :sort => 'total_spent_hours:desc'
@@ -1379,6 +1395,22 @@ class IssuesControllerTest < Redmine::ControllerTest
     assert_select ".total-for-cf-#{field.id} span.value", :text => '9'
   end
 
+  def test_index_with_spent_time_total_should_sum_visible_spent_time_only
+    TimeEntry.delete_all
+    TimeEntry.generate!(:issue => Issue.generate!(:project_id => 1), :hours => 3)
+    TimeEntry.generate!(:issue => Issue.generate!(:project_id => 3), :hours => 4)
+
+    get :index, :params => {:t => ["spent_hours"]}
+    assert_response :success
+    assert_select ".total-for-spent-hours span.value", :text => '7.00'
+
+    Project.find(3).disable_module!(:time_tracking)
+
+    get :index, :params => {:t => ["spent_hours"]}
+    assert_response :success
+    assert_select ".total-for-spent-hours span.value", :text => '3.00'
+  end
+
   def test_index_totals_should_default_to_settings
     with_settings :issue_list_default_totals => ['estimated_hours'] do
       get :index