From b714c714024796d3cd3bd215b07b4d8798a35406 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Thu, 9 Mar 2017 20:01:01 +0000 Subject: [PATCH] Get the count by group from the query directly. git-svn-id: http://svn.redmine.org/redmine/trunk@16387 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/issues_controller.rb | 1 - app/helpers/issues_helper.rb | 4 ++-- app/helpers/queries_helper.rb | 5 +++-- app/models/issue_query.rb | 7 ------- app/models/query.rb | 7 +++++++ app/views/issues/_list.html.erb | 2 +- app/views/issues/index.html.erb | 2 +- app/views/timelog/_list.html.erb | 2 +- lib/redmine/export/pdf/issues_pdf_helper.rb | 4 +++- test/unit/query_test.rb | 8 ++++---- 10 files changed, 22 insertions(+), 20 deletions(-) diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 906130e1a..a04c1a6b0 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -69,7 +69,6 @@ class IssuesController < ApplicationController @issues = @query.issues(:order => sort_clause, :offset => @offset, :limit => @limit) - @issue_count_by_group = @query.issue_count_by_group respond_to do |format| format.html { render :template => 'issues/index', :layout => !request.xhr? } diff --git a/app/helpers/issues_helper.rb b/app/helpers/issues_helper.rb index f95338284..703607999 100644 --- a/app/helpers/issues_helper.rb +++ b/app/helpers/issues_helper.rb @@ -32,9 +32,9 @@ module IssuesHelper end end - def grouped_issue_list(issues, query, issue_count_by_group, &block) + def grouped_issue_list(issues, query, &block) ancestors = [] - grouped_query_results(issues, query, issue_count_by_group) do |issue, group_name, group_count, group_totals| + grouped_query_results(issues, query) do |issue, group_name, group_count, group_totals| while (ancestors.any? && !issue.is_descendant_of?(ancestors.last)) ancestors.pop end diff --git a/app/helpers/queries_helper.rb b/app/helpers/queries_helper.rb index c356ca0d4..197bbb998 100644 --- a/app/helpers/queries_helper.rb +++ b/app/helpers/queries_helper.rb @@ -115,7 +115,8 @@ module QueriesHelper render :partial => 'queries/columns', :locals => {:query => query, :tag_name => tag_name} end - def grouped_query_results(items, query, item_count_by_group, &block) + def grouped_query_results(items, query, &block) + result_count_by_group = query.result_count_by_group previous_group, first = false, true totals_by_group = query.totalable_columns.inject({}) do |h, column| h[column] = query.total_by_group_for(column) @@ -132,7 +133,7 @@ module QueriesHelper group_name = format_object(group) end group_name ||= "" - group_count = item_count_by_group ? item_count_by_group[group] : nil + group_count = result_count_by_group ? result_count_by_group[group] : nil group_totals = totals_by_group.map {|column, t| total_tag(column, t[group] || 0)}.join(" ").html_safe end end diff --git a/app/models/issue_query.rb b/app/models/issue_query.rb index 7f399f469..71fe8d288 100644 --- a/app/models/issue_query.rb +++ b/app/models/issue_query.rb @@ -245,13 +245,6 @@ class IssueQuery < Query raise StatementInvalid.new(e.message) end - # Returns the issue count by group or nil if query is not grouped - def issue_count_by_group - grouped_query do |scope| - scope.count - end - end - # Returns sum of all the issue's estimated_hours def total_for_estimated_hours(scope) map_total(scope.sum(:estimated_hours)) {|t| t.to_f.round(2)} diff --git a/app/models/query.rb b/app/models/query.rb index 321821a93..7a43ca6fc 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -846,6 +846,13 @@ class Query < ActiveRecord::Base filters_clauses.any? ? filters_clauses.join(' AND ') : nil end + # Returns the result count by group or nil if query is not grouped + def result_count_by_group + grouped_query do |scope| + scope.count + end + end + # Returns the sum of values for the given column def total_for(column) total_with_scope(column, base_scope) diff --git a/app/views/issues/_list.html.erb b/app/views/issues/_list.html.erb index ed97d046f..1e7d01ef7 100644 --- a/app/views/issues/_list.html.erb +++ b/app/views/issues/_list.html.erb @@ -14,7 +14,7 @@ - <% grouped_issue_list(issues, query, issue_count_by_group) do |issue, level, group_name, group_count, group_totals| -%> + <% grouped_issue_list(issues, query) do |issue, level, group_name, group_count, group_totals| -%> <% if group_name %> <% reset_cycle %> diff --git a/app/views/issues/index.html.erb b/app/views/issues/index.html.erb index 7828075cf..80210814e 100644 --- a/app/views/issues/index.html.erb +++ b/app/views/issues/index.html.erb @@ -16,7 +16,7 @@

<%= l(:label_no_data) %>

<% else %> <%= render_query_totals(@query) %> -<%= render :partial => 'issues/list', :locals => {:issues => @issues, :query => @query, :issue_count_by_group => @issue_count_by_group} %> +<%= render :partial => 'issues/list', :locals => {:issues => @issues, :query => @query} %> <%= pagination_links_full @issue_pages, @issue_count %> <% end %> diff --git a/app/views/timelog/_list.html.erb b/app/views/timelog/_list.html.erb index b39eb5cc3..d4836056a 100644 --- a/app/views/timelog/_list.html.erb +++ b/app/views/timelog/_list.html.erb @@ -15,7 +15,7 @@ -<% grouped_query_results(entries, @query, @entry_count_by_group) do |entry, group_name, group_count, group_totals| -%> +<% grouped_query_results(entries, @query) do |entry, group_name, group_count, group_totals| -%> <% if group_name %> <% reset_cycle %> diff --git a/lib/redmine/export/pdf/issues_pdf_helper.rb b/lib/redmine/export/pdf/issues_pdf_helper.rb index cf8a79d3c..c72ff6657 100644 --- a/lib/redmine/export/pdf/issues_pdf_helper.rb +++ b/lib/redmine/export/pdf/issues_pdf_helper.rb @@ -299,12 +299,14 @@ module Redmine totals_by_group = query.totals_by_group render_table_header(pdf, query, col_width, row_height, table_width) previous_group = false + result_count_by_group = query.result_count_by_group + issue_list(issues) do |issue, level| if query.grouped? && (group = query.group_by_column.value(issue)) != previous_group pdf.SetFontStyle('B',10) group_label = group.blank? ? 'None' : group.to_s.dup - group_label << " (#{query.issue_count_by_group[group]})" + group_label << " (#{result_count_by_group[group]})" pdf.bookmark group_label, 0, -1 pdf.RDMCell(table_width, row_height * 2, group_label, 'LR', 1, 'L') pdf.SetFontStyle('',8) diff --git a/test/unit/query_test.rb b/test/unit/query_test.rb index 7f2ef3d1c..d691f66be 100644 --- a/test/unit/query_test.rb +++ b/test/unit/query_test.rb @@ -1653,7 +1653,7 @@ class QueryTest < ActiveSupport::TestCase def test_issue_count_by_association_group q = IssueQuery.new(:name => '_', :group_by => 'assigned_to') - count_by_group = q.issue_count_by_group + count_by_group = q.result_count_by_group assert_kind_of Hash, count_by_group assert_equal %w(NilClass User), count_by_group.keys.collect {|k| k.class.name}.uniq.sort assert_equal %W(#{INTEGER_KLASS}), count_by_group.values.collect {|k| k.class.name}.uniq @@ -1662,7 +1662,7 @@ class QueryTest < ActiveSupport::TestCase def test_issue_count_by_list_custom_field_group q = IssueQuery.new(:name => '_', :group_by => 'cf_1') - count_by_group = q.issue_count_by_group + count_by_group = q.result_count_by_group assert_kind_of Hash, count_by_group assert_equal %w(NilClass String), count_by_group.keys.collect {|k| k.class.name}.uniq.sort assert_equal %W(#{INTEGER_KLASS}), count_by_group.values.collect {|k| k.class.name}.uniq @@ -1671,7 +1671,7 @@ class QueryTest < ActiveSupport::TestCase def test_issue_count_by_date_custom_field_group q = IssueQuery.new(:name => '_', :group_by => 'cf_8') - count_by_group = q.issue_count_by_group + count_by_group = q.result_count_by_group assert_kind_of Hash, count_by_group assert_equal %w(Date NilClass), count_by_group.keys.collect {|k| k.class.name}.uniq.sort assert_equal %W(#{INTEGER_KLASS}), count_by_group.values.collect {|k| k.class.name}.uniq @@ -1681,7 +1681,7 @@ class QueryTest < ActiveSupport::TestCase Issue.update_all("assigned_to_id = NULL") q = IssueQuery.new(:name => '_', :group_by => 'assigned_to') - count_by_group = q.issue_count_by_group + count_by_group = q.result_count_by_group assert_kind_of Hash, count_by_group assert_equal 1, count_by_group.keys.size assert_nil count_by_group.keys.first -- 2.39.5