]> source.dussan.org Git - redmine.git/commitdiff
Merged r14407 from trunk to 3.1-stable (#11253, #20456)
authorToshi MARUYAMA <marutosijp2@yahoo.co.jp>
Mon, 31 Aug 2015 03:38:23 +0000 (03:38 +0000)
committerToshi MARUYAMA <marutosijp2@yahoo.co.jp>
Mon, 31 Aug 2015 03:38:23 +0000 (03:38 +0000)
Preload total spent time on the issue list with 1 query.

git-svn-id: http://svn.redmine.org/redmine/branches/3.1-stable@14534 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/issue.rb
app/models/issue_query.rb

index e7c02ea2e79a592152ad9de99b53d68d95c7d175..5c9f81efae46266b2f9a5047a75b721846ff2899 100644 (file)
@@ -918,11 +918,10 @@ class Issue < ActiveRecord::Base
 
   # Returns the total number of hours spent on this issue and its descendants
   def total_spent_hours
-    if leaf?
+    @total_spent_hours ||= if leaf?
       spent_hours
     else
-      @total_spent_hours ||=
-        self_and_descendants.joins(:time_entries).sum("#{TimeEntry.table_name}.hours").to_f || 0.0
+      self_and_descendants.joins(:time_entries).sum("#{TimeEntry.table_name}.hours").to_f || 0.0
     end
   end
 
@@ -948,7 +947,7 @@ class Issue < ActiveRecord::Base
     end
   end
 
-  # Preloads visible spent time for a collection of issues
+  # Preloads visible total spent time for a collection of issues
   def self.load_visible_spent_hours(issues, user=User.current)
     if issues.any?
       hours_by_issue_id = TimeEntry.visible(user).group(:issue_id).sum(:hours)
@@ -958,6 +957,16 @@ class Issue < ActiveRecord::Base
     end
   end
 
+  def self.load_visible_total_spent_hours(issues, user=User.current)
+    if issues.any?
+      hours_by_issue_id = TimeEntry.visible(user).joins(:issue).joins("JOIN #{Issue.table_name} parent ON parent.root_id = #{Issue.table_name}.root_id" +
+        " AND parent.lft <= #{Issue.table_name}.lft AND parent.rgt >= #{Issue.table_name}.rgt").group("parent.id").sum(:hours)
+      issues.each do |issue|
+        issue.instance_variable_set "@total_spent_hours", (hours_by_issue_id[issue.id] || 0)
+      end
+    end
+  end
+
   # Preloads visible relations for a collection of issues
   def self.load_visible_relations(issues, user=User.current)
     if issues.any?
index edc1290fe4959890ed03dd94b594032b01ecd55d..8316f6b4bfe18784cef925a919fdca6840c49253 100644 (file)
@@ -353,6 +353,9 @@ class IssueQuery < Query
     if has_column?(:spent_hours)
       Issue.load_visible_spent_hours(issues)
     end
+    if has_column?(:total_spent_hours)
+      Issue.load_visible_total_spent_hours(issues)
+    end
     if has_column?(:relations)
       Issue.load_visible_relations(issues)
     end