]> source.dussan.org Git - redmine.git/commitdiff
Workaround for spent time grouped by project (#1561).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Fri, 9 Oct 2015 09:30:14 +0000 (09:30 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Fri, 9 Oct 2015 09:30:14 +0000 (09:30 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@14667 e93f8b46-1217-0410-a6f0-8f06a7374b81

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

index 45aacdffa855ac86346e6a6028de03b1d074ab26..4528f4a9bef6168930ad693b24181adda9cd205a 100644 (file)
@@ -325,7 +325,15 @@ class IssueQuery < Query
 
   # Returns sum of all the issue's time entries hours
   def total_for_spent_hours(scope)
-    scope.joins(:time_entries).sum("#{TimeEntry.table_name}.hours")
+    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
   end
 
   # Returns the issues
index 679fc772db439694abfe7ee629231b57f32fda18..c76ad726534239ca0d7a7001ec8dd5f448b598e5 100644 (file)
@@ -1219,6 +1219,20 @@ class QueryTest < ActiveSupport::TestCase
     )
   end
 
+  def test_total_by_project_group_for_spent_hours
+    TimeEntry.delete_all
+    TimeEntry.generate!(:hours => 5.5, :issue_id => 1)
+    TimeEntry.generate!(:hours => 1.1, :issue_id => 2)
+    Issue.where(:id => 1).update_all(:assigned_to_id => 2)
+    Issue.where(:id => 2).update_all(:assigned_to_id => 3)
+
+    q = IssueQuery.new(:group_by => 'project')
+    assert_equal(
+      {Project.find(1) => 6.6},
+      q.total_by_group_for(:spent_hours)
+    )
+  end
+
   def test_total_for_int_custom_field
     field = IssueCustomField.generate!(:field_format => 'int', :is_for_all => true)
     CustomValue.create!(:customized => Issue.find(1), :custom_field => field, :value => '2')