From 71bc44a89dceb4b6caacb4982849a2b65f02edf8 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Mon, 16 Nov 2009 18:07:30 +0000 Subject: [PATCH] Allow issue grouping by custom field (#2679). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3071 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/issues_controller.rb | 4 ++-- app/helpers/queries_helper.rb | 9 +++++++++ app/models/query.rb | 11 +++++++++++ app/views/issues/_list.rhtml | 6 +++--- test/fixtures/queries.yml | 19 +++++++++++++++++++ test/functional/issues_controller_test.rb | 18 ++++++++++++++++-- test/unit/query_test.rb | 5 +++++ 7 files changed, 65 insertions(+), 7 deletions(-) diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 2fa465f4e..2578b784b 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -64,7 +64,7 @@ class IssuesController < ApplicationController @issue_count = Issue.count(:include => [:status, :project], :conditions => @query.statement) @issue_pages = Paginator.new self, @issue_count, limit, params['page'] @issues = Issue.find :all, :order => [@query.group_by_sort_order, sort_clause].compact.join(','), - :include => [ :assigned_to, :status, :tracker, :project, :priority, :category, :fixed_version ], + :include => [ :assigned_to, :status, :tracker, :project, :priority, :category, :fixed_version ], :conditions => @query.statement, :limit => limit, :offset => @issue_pages.current.offset @@ -73,7 +73,7 @@ class IssuesController < ApplicationController if @query.grouped? # Retrieve the issue count by group @issue_count_by_group = begin - Issue.count(:group => @query.group_by, :include => [:status, :project], :conditions => @query.statement) + Issue.count(:group => @query.group_by_statement, :include => [:status, :project], :conditions => @query.statement) # Rails will raise an (unexpected) error if there's only a nil group value rescue ActiveRecord::RecordNotFound {nil => @issue_count} diff --git a/app/helpers/queries_helper.rb b/app/helpers/queries_helper.rb index 165308efd..518226c8f 100644 --- a/app/helpers/queries_helper.rb +++ b/app/helpers/queries_helper.rb @@ -27,6 +27,15 @@ module QueriesHelper content_tag('th', column.caption) end + def column_value(column, issue) + if column.is_a?(QueryCustomFieldColumn) + cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id} + show_value(cv) + else + value = issue.send(column.name) + end + end + def column_content(column, issue) if column.is_a?(QueryCustomFieldColumn) cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id} diff --git a/app/models/query.rb b/app/models/query.rb index 7ce95f0ea..816c5c5a0 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -23,6 +23,9 @@ class QueryColumn self.name = name self.sortable = options[:sortable] self.groupable = options[:groupable] || false + if groupable == true + self.groupable = name.to_s + end self.default_order = options[:default_order] end @@ -41,6 +44,10 @@ class QueryCustomFieldColumn < QueryColumn def initialize(custom_field) self.name = "cf_#{custom_field.id}".to_sym self.sortable = custom_field.order_statement || false + if %w(list date bool int).include?(custom_field.field_format) + self.groupable = custom_field.order_statement + end + self.groupable ||= false @cf = custom_field end @@ -312,6 +319,10 @@ class Query < ActiveRecord::Base groupable_columns.detect {|c| c.name.to_s == group_by} end + def group_by_statement + group_by_column.groupable + end + def project_statement project_clauses = [] if project && !@project.descendants.active.empty? diff --git a/app/views/issues/_list.rhtml b/app/views/issues/_list.rhtml index 9d4ef7dd8..f51a70883 100644 --- a/app/views/issues/_list.rhtml +++ b/app/views/issues/_list.rhtml @@ -10,11 +10,10 @@ <%= column_header(column) %> <% end %> - <% group = false %> + <% previous_group = false %> <% issues.each do |issue| -%> - <% if @query.grouped? && issue.send(@query.group_by) != group %> - <% group = issue.send(@query.group_by) %> + <% if @query.grouped? && (group = column_value(@query.group_by_column, issue) || '') != previous_group %> <% reset_cycle %> @@ -22,6 +21,7 @@ <%= group.blank? ? 'None' : group %> (<%= @issue_count_by_group[group] %>) + <% previous_group = group %> <% end %> <%= check_box_tag("ids[]", issue.id, false, :id => nil) %> diff --git a/test/fixtures/queries.yml b/test/fixtures/queries.yml index 3299f96f7..a49f82fbb 100644 --- a/test/fixtures/queries.yml +++ b/test/fixtures/queries.yml @@ -134,4 +134,23 @@ queries_008: user_id: 2 column_names: +queries_009: + id: 9 + project_id: + is_public: true + name: Open issues grouped by list custom field + filters: | + --- + status_id: + :values: + - "1" + :operator: o + + user_id: 1 + column_names: + group_by: cf_1 + sort_criteria: | + --- + - - priority + - desc diff --git a/test/functional/issues_controller_test.rb b/test/functional/issues_controller_test.rb index 75da3c0c3..7356b130b 100644 --- a/test/functional/issues_controller_test.rb +++ b/test/functional/issues_controller_test.rb @@ -171,12 +171,26 @@ class IssuesControllerTest < ActionController::TestCase assert_nil assigns(:issue_count_by_group) end - def test_index_with_grouped_query + def test_index_with_query_grouped_by_tracker get :index, :project_id => 1, :query_id => 6 assert_response :success assert_template 'index.rhtml' assert_not_nil assigns(:issues) - assert_not_nil assigns(:issue_count_by_group) + count_by_group = assigns(:issue_count_by_group) + assert_kind_of Hash, count_by_group + assert_kind_of Tracker, count_by_group.keys.first + assert_not_nil count_by_group[Tracker.find(1)] + end + + def test_index_with_query_grouped_by_list_custom_field + get :index, :project_id => 1, :query_id => 9 + assert_response :success + assert_template 'index.rhtml' + assert_not_nil assigns(:issues) + count_by_group = assigns(:issue_count_by_group) + assert_kind_of Hash, count_by_group + assert_kind_of String, count_by_group.keys.first + assert_not_nil count_by_group['MySQL'] end def test_index_csv_with_project diff --git a/test/unit/query_test.rb b/test/unit/query_test.rb index f1f9397c6..27aed0ce8 100644 --- a/test/unit/query_test.rb +++ b/test/unit/query_test.rb @@ -197,6 +197,11 @@ class QueryTest < ActiveSupport::TestCase assert q.has_column?(c) end + def test_groupable_columns_should_include_custom_fields + q = Query.new + assert q.groupable_columns.detect {|c| c.is_a? QueryCustomFieldColumn} + end + def test_default_sort q = Query.new assert_equal [], q.sort_criteria