From 7c7d29e33c6f98d0b81f0055be5d6417d677a207 Mon Sep 17 00:00:00 2001 From: Go MAEDA Date: Fri, 19 Mar 2021 05:20:48 +0000 Subject: [PATCH] Unnecessary database access when IssueQuery class is defined (#33290). Patch by Kevin Fischer. git-svn-id: http://svn.redmine.org/redmine/trunk@20830 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/query.rb | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/app/models/query.rb b/app/models/query.rb index dc610f41e..308d553c2 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -20,17 +20,15 @@ require 'redmine/sort_criteria' class QueryColumn - attr_accessor :name, :groupable, :totalable, :default_order - attr_writer :sortable + attr_accessor :name, :totalable, :default_order + attr_writer :sortable, :groupable + include Redmine::I18n def initialize(name, options={}) self.name = name self.sortable = options[:sortable] self.groupable = options[:groupable] || false - if groupable == true - self.groupable = name.to_s - end self.totalable = options[:totalable] || false self.default_order = options[:default_order] @inline = options.key?(:inline) ? options[:inline] : true @@ -49,6 +47,10 @@ class QueryColumn end end + def groupable? + @groupable + end + # Returns true if the column is sortable, otherwise false def sortable? @sortable.present? @@ -82,13 +84,19 @@ class QueryColumn def css_classes name end + + def group_by_statement + name.to_s + end end class TimestampQueryColumn < QueryColumn - def groupable - if @groupable - Redmine::Database.timestamp_to_date(sortable, User.current.time_zone) - end + def groupable? + group_by_statement.present? + end + + def group_by_statement + Redmine::Database.timestamp_to_date(sortable, User.current.time_zone) end def group_value(object) @@ -121,12 +129,19 @@ class QueryCustomFieldColumn < QueryColumn def initialize(custom_field, options={}) self.name = "cf_#{custom_field.id}".to_sym self.sortable = custom_field.order_statement || false - self.groupable = custom_field.group_statement || false self.totalable = options.key?(:totalable) ? !!options[:totalable] : custom_field.totalable? @inline = custom_field.full_width_layout? ? false : true @cf = custom_field end + def groupable? + group_by_statement.present? + end + + def group_by_statement + @cf.group_statement + end + def caption @cf.name end @@ -741,7 +756,7 @@ class Query < ActiveRecord::Base # Returns an array of columns that can be used to group the results def groupable_columns - available_columns.select {|c| c.groupable} + available_columns.select(&:groupable?) end # Returns a Hash of columns and the key for sorting @@ -889,11 +904,11 @@ class Query < ActiveRecord::Base end def group_by_column - groupable_columns.detect {|c| c.groupable && c.name.to_s == group_by} + groupable_columns.detect {|c| c.groupable? && c.name.to_s == group_by} end def group_by_statement - group_by_column.try(:groupable) + group_by_column.try(:group_by_statement) end def project_statement -- 2.39.5