Class: ActiveReporter::Dimension::Base
- Inherits:
-
Object
- Object
- ActiveReporter::Dimension::Base
- Defined in:
- lib/active_reporter/dimension/base.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#report ⇒ Object
readonly
Returns the value of attribute report.
Instance Method Summary collapse
- #attribute ⇒ Object
- #expression ⇒ Object
-
#extract_sql_value(row) ⇒ Object
Given a single (hashified) row of the SQL result, return the Ruby object representing this dimension’s value.
-
#filter(relation) ⇒ Object
Filter the relation based on any constraints in the params.
- #filter_values ⇒ Object
-
#filtering? ⇒ Boolean
Return whether the report should filter by this dimension.
-
#group(relation) ⇒ Object
Group the relation by the expression – ensure this is ordered, too.
-
#group_values ⇒ Object
Return an ordered array of all values that should appear in ‘Report#data`.
- #grouping? ⇒ Boolean
-
#initialize(name, report, opts = {}) ⇒ Base
constructor
A new instance of Base.
- #model ⇒ Object
- #null_order ⇒ Object
- #nulls_last? ⇒ Boolean
- #order(relation) ⇒ Object
- #order_expression ⇒ Object
- #params ⇒ Object
-
#relate(relation) ⇒ Object
Do any joins/selects necessary to filter or group the relation.
- #sort_desc? ⇒ Boolean
- #sort_order ⇒ Object
Constructor Details
#initialize(name, report, opts = {}) ⇒ Base
Returns a new instance of Base.
6 7 8 9 10 11 |
# File 'lib/active_reporter/dimension/base.rb', line 6 def initialize(name, report, opts={}) @name = name @report = report @opts = opts validate_params! end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/active_reporter/dimension/base.rb', line 4 def name @name end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
4 5 6 |
# File 'lib/active_reporter/dimension/base.rb', line 4 def opts @opts end |
#report ⇒ Object (readonly)
Returns the value of attribute report.
4 5 6 |
# File 'lib/active_reporter/dimension/base.rb', line 4 def report @report end |
Instance Method Details
#attribute ⇒ Object
22 23 24 |
# File 'lib/active_reporter/dimension/base.rb', line 22 def attribute opts.fetch(:attribute, name) end |
#expression ⇒ Object
26 27 28 |
# File 'lib/active_reporter/dimension/base.rb', line 26 def expression @expression ||= opts[:expression] || opts[:_expression] || "#{table_name}.#{column}" end |
#extract_sql_value(row) ⇒ Object
Given a single (hashified) row of the SQL result, return the Ruby object representing this dimension’s value
52 53 54 |
# File 'lib/active_reporter/dimension/base.rb', line 52 def extract_sql_value(row) sanitize_sql_value(row[sql_value_name]) end |
#filter(relation) ⇒ Object
Filter the relation based on any constraints in the params
36 37 38 |
# File 'lib/active_reporter/dimension/base.rb', line 36 def filter(relation) raise NotImplementedError end |
#filter_values ⇒ Object
56 57 58 |
# File 'lib/active_reporter/dimension/base.rb', line 56 def filter_values array_param(:only).uniq end |
#filtering? ⇒ Boolean
Return whether the report should filter by this dimension
61 62 63 |
# File 'lib/active_reporter/dimension/base.rb', line 61 def filtering? filter_values.present? end |
#group(relation) ⇒ Object
Group the relation by the expression – ensure this is ordered, too.
41 42 43 |
# File 'lib/active_reporter/dimension/base.rb', line 41 def group(relation) raise NotImplementedError end |
#group_values ⇒ Object
Return an ordered array of all values that should appear in ‘Report#data`
46 47 48 |
# File 'lib/active_reporter/dimension/base.rb', line 46 def group_values raise NotImplementedError end |
#grouping? ⇒ Boolean
65 66 67 |
# File 'lib/active_reporter/dimension/base.rb', line 65 def grouping? report.groupers.include?(self) end |
#model ⇒ Object
13 14 15 16 17 18 19 20 |
# 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 ⇒ Object
91 92 93 94 |
# 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? ⇒ Boolean
85 86 87 88 89 |
# 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) ⇒ Object
73 74 75 |
# File 'lib/active_reporter/dimension/base.rb', line 73 def order(relation) relation.order("#{order_expression} #{sort_order} #{null_order}") end |
#order_expression ⇒ Object
69 70 71 |
# File 'lib/active_reporter/dimension/base.rb', line 69 def order_expression sql_value_name end |
#params ⇒ Object
96 97 98 |
# File 'lib/active_reporter/dimension/base.rb', line 96 def params report.params.fetch(:dimensions, {})[name].presence || {} end |
#relate(relation) ⇒ Object
Do any joins/selects necessary to filter or group the relation.
31 32 33 |
# File 'lib/active_reporter/dimension/base.rb', line 31 def relate(relation) opts.fetch(:relation, ->(r) { r }).call(relation) end |
#sort_desc? ⇒ Boolean
77 78 79 |
# File 'lib/active_reporter/dimension/base.rb', line 77 def sort_desc? dimension_or_root_param(:sort_desc) end |
#sort_order ⇒ Object
81 82 83 |
# File 'lib/active_reporter/dimension/base.rb', line 81 def sort_order sort_desc? ? 'DESC' : 'ASC' end |