class ActiveReporter::Dimension::Base
Attributes
name[R]
opts[R]
report[R]
Public Class Methods
new(name, report, opts={})
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 6 def initialize(name, report, opts={}) @name = name @report = report @opts = opts validate_params! end
Public Instance Methods
attribute()
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 22 def attribute opts.fetch(:attribute, name) end
expression()
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 26 def expression @expression ||= opts[:expression] || opts[:_expression] || "#{table_name}.#{column}" end
extract_sql_value(row)
click to toggle source
Given a single (hashified) row of the SQL result, return the Ruby object representing this dimension's value
# File lib/active_reporter/dimension/base.rb, line 52 def extract_sql_value(row) sanitize_sql_value(row[sql_value_name]) end
filter(relation)
click to toggle source
Filter the relation based on any constraints in the params
# File lib/active_reporter/dimension/base.rb, line 36 def filter(relation) raise NotImplementedError end
filter_values()
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 56 def filter_values array_param(:only).uniq end
filtering?()
click to toggle source
Return whether the report should filter by this dimension
# File lib/active_reporter/dimension/base.rb, line 61 def filtering? filter_values.present? end
group(relation)
click to toggle source
Group the relation by the expression – ensure this is ordered, too.
# File lib/active_reporter/dimension/base.rb, line 41 def group(relation) raise NotImplementedError end
group_values()
click to toggle source
Return an ordered array of all values that should appear in `Report#data`
# File lib/active_reporter/dimension/base.rb, line 46 def group_values raise NotImplementedError end
grouping?()
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 65 def grouping? report.groupers.include?(self) end
model()
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 13 def model return @model unless @model.nil? @model = opts[:model].to_s.classify.constantize rescue opts[:model] @model = report.report_model if @model.nil? @model end
null_order()
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 91 def null_order return unless ActiveReporter.database_type == :postgres nulls_last? ? 'NULLS LAST' : 'NULLS FIRST' end
nulls_last?()
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 85 def nulls_last? value = dimension_or_root_param(:nulls_last) value = !value if sort_desc? value end
order(relation)
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 73 def order(relation) relation.order("#{order_expression} #{sort_order} #{null_order}") end
order_expression()
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 69 def order_expression sql_value_name end
params()
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 96 def params report.params.fetch(:dimensions, {})[name].presence || {} end
relate(relation)
click to toggle source
Do any joins/selects necessary to filter or group the relation.
# File lib/active_reporter/dimension/base.rb, line 31 def relate(relation) opts.fetch(:relation, ->(r) { r }).call(relation) end
sort_desc?()
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 77 def sort_desc? dimension_or_root_param(:sort_desc) end
sort_order()
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 81 def sort_order sort_desc? ? 'DESC' : 'ASC' end
Private Instance Methods
array_param(key)
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 139 def array_param(key) return [] unless params.key?(key) return [nil] if params[key].nil? Array.wrap(params[key]) end
column()
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 123 def column opts.fetch(:column, attribute) end
dimension_or_root_param(key)
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 135 def dimension_or_root_param(key) params.fetch(key, report.params[key]) end
enum?()
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 145 def enum? false # Hash(model&.defined_enums).include?(attribute.to_s) end
invalid_param!(param_key, message)
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 108 def invalid_param!(param_key, message) raise InvalidParamsError, "Invalid value for params[:dimensions][:#{name}][:#{param_key}]\n :#{param_key} #{message}" end
sanitize_sql_value(value)
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 131 def sanitize_sql_value(value) value end
sql_value_name()
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 127 def sql_value_name "_active_reporter_dimension_#{name}" end
table_name()
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 112 def table_name return @table_name unless @table_name.nil? @table_name = opts[:table_name] @table_name = model.try(:table_name) if @table_name.nil? @table_name = model.to_s.constantize.try(:table_name) rescue nil if @table_name.nil? @table_name = report.table_name if @table_name.nil? @table_name end
validate_params!()
click to toggle source
# File lib/active_reporter/dimension/base.rb, line 102 def validate_params! if opts.include?(:expression) ActiveSupport::Deprecation.warn("passing an :expression option will be deprecated in version 1.0\n please use :attribute, and, when required, :model or :table_name") end end