refactored differentitation between select and group_by fields in Report::GroupBy::Base

pull/6827/head
Philipp Tessenow 14 years ago
parent a10f55029e
commit 6eef4a8faf
  1. 38
      app/models/report/group_by/base.rb
  2. 1
      app/models/report/group_by/singleton_value.rb

@ -32,8 +32,35 @@ class Report::GroupBy
end.uniq
end
def self.select_fields(*fields)
unless fields.empty?
@select_fields ||= []
@select_fields += fields
end
@select_fields
end
def select_fields
if self.class.select_fields
(parent ? parent.select_fields : []) + self.class.select_fields
else
group_fields
end
end
##
# @param [FalseClass, TrueClass] prefix Whether or not add a table prefix the field names
# @return [Array<String,Symbol>] List of select fields corresponding to self and all parents'
def all_select_fields(prefix = true)
@all_select_fields ||= []
@all_select_fields[prefix ? 0 : 1] ||= begin
fields = select_fields.reject { |c| c.blank? or c == 'base' }
(parent ? parent.all_select_fields(prefix) : []) + (prefix ? with_table(fields) : fields)
end.uniq
end
def clear
@all_group_fields = nil
@all_group_fields = @all_select_fields = nil
super
end
@ -58,13 +85,8 @@ class Report::GroupBy
end
def define_group(sql)
fields = all_group_fields
# fields usually are Strings which we want select and group_by
# sometimes fields are arrays of the form [String,String], where
# the fields.first is to select and where we have to group on field.last
#TODO: differenciate between all_group_fields and all_select_fields
sql.select fields.map {|field| field.is_a?(String) ? field : field.first}
sql.group_by fields.map {|field| field.is_a?(String) ? field : field.last}
sql.select all_select_fields
sql.group_by all_group_fields
end
end
end

@ -3,6 +3,7 @@ class Report::GroupBy
dont_display!
put_sql_table_names "singleton_value" => false
select_fields "1 as singleton_value"
def define_group(sql)
sql.select "1 as singleton_value"

Loading…
Cancel
Save