@@ -325,7 +325,15 class IssueQuery < Query | |||||
325 |
|
325 | |||
326 | # Returns sum of all the issue's time entries hours |
|
326 | # Returns sum of all the issue's time entries hours | |
327 | def total_for_spent_hours(scope) |
|
327 | def total_for_spent_hours(scope) | |
328 | scope.joins(:time_entries).sum("#{TimeEntry.table_name}.hours") |
|
328 | if group_by_column.try(:name) == :project | |
|
329 | # TODO: remove this when https://github.com/rails/rails/issues/21922 is fixed | |||
|
330 | # We have to do a custom join without the time_entries.project_id column | |||
|
331 | # that would trigger a ambiguous column name error | |||
|
332 | 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"). | |||
|
333 | sum("joined_time_entries.hours") | |||
|
334 | else | |||
|
335 | scope.joins(:time_entries).sum("#{TimeEntry.table_name}.hours") | |||
|
336 | end | |||
329 | end |
|
337 | end | |
330 |
|
338 | |||
331 | # Returns the issues |
|
339 | # Returns the issues |
@@ -1219,6 +1219,20 class QueryTest < ActiveSupport::TestCase | |||||
1219 | ) |
|
1219 | ) | |
1220 | end |
|
1220 | end | |
1221 |
|
1221 | |||
|
1222 | def test_total_by_project_group_for_spent_hours | |||
|
1223 | TimeEntry.delete_all | |||
|
1224 | TimeEntry.generate!(:hours => 5.5, :issue_id => 1) | |||
|
1225 | TimeEntry.generate!(:hours => 1.1, :issue_id => 2) | |||
|
1226 | Issue.where(:id => 1).update_all(:assigned_to_id => 2) | |||
|
1227 | Issue.where(:id => 2).update_all(:assigned_to_id => 3) | |||
|
1228 | ||||
|
1229 | q = IssueQuery.new(:group_by => 'project') | |||
|
1230 | assert_equal( | |||
|
1231 | {Project.find(1) => 6.6}, | |||
|
1232 | q.total_by_group_for(:spent_hours) | |||
|
1233 | ) | |||
|
1234 | end | |||
|
1235 | ||||
1222 | def test_total_for_int_custom_field |
|
1236 | def test_total_for_int_custom_field | |
1223 | field = IssueCustomField.generate!(:field_format => 'int', :is_for_all => true) |
|
1237 | field = IssueCustomField.generate!(:field_format => 'int', :is_for_all => true) | |
1224 | CustomValue.create!(:customized => Issue.find(1), :custom_field => field, :value => '2') |
|
1238 | CustomValue.create!(:customized => Issue.find(1), :custom_field => field, :value => '2') |
General Comments 0
You need to be logged in to leave comments.
Login now