From 1c0988cad384852011beecf6c29eae0d0133aa55 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Fri, 2 Mar 2012 16:26:06 +0000 Subject: [PATCH] Use a single query to retrieve issues_count, open_issues_count and closed_issues_count. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9056 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/version.rb | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/app/models/version.rb b/app/models/version.rb index 24048a242..2d2176764 100644 --- a/app/models/version.rb +++ b/app/models/version.rb @@ -123,17 +123,20 @@ class Version < ActiveRecord::Base # Returns assigned issues count def issues_count - @issue_count ||= fixed_issues.count + load_issue_counts + @issue_count end # Returns the total amount of open issues for this version. def open_issues_count - @open_issues_count ||= Issue.open.count(:all, :conditions => ["fixed_version_id = ?", self.id]) + load_issue_counts + @open_issues_count end # Returns the total amount of closed issues for this version. def closed_issues_count - @closed_issues_count ||= Issue.open(false).count(:all, :conditions => ["fixed_version_id = ?", self.id]) + load_issue_counts + @closed_issues_count end def wiki_page @@ -194,6 +197,21 @@ class Version < ActiveRecord::Base private + def load_issue_counts + unless @issue_count + @open_issues_count = 0 + @closed_issues_count = 0 + fixed_issues.count(:all, :group => :status).each do |status, count| + if status.is_closed? + @closed_issues_count += count + else + @open_issues_count += count + end + end + @issue_count = @open_issues_count + @closed_issues_count + end + end + # Update the issue's fixed versions. Used if a version's sharing changes. def update_issues_from_sharing_change if sharing_changed?