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.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#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, options = {}) ⇒ 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, options = {}) ⇒ Base
Returns a new instance of Base.
8 9 10 11 12 13 |
# File 'lib/active_reporter/dimension/base.rb', line 8 def initialize(name, report, = {}) @name = name @report = report @options = validate_params! end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/active_reporter/dimension/base.rb', line 6 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/active_reporter/dimension/base.rb', line 6 def @options end |
#report ⇒ Object (readonly)
Returns the value of attribute report.
6 7 8 |
# File 'lib/active_reporter/dimension/base.rb', line 6 def report @report end |
Instance Method Details
#attribute ⇒ Object
19 20 21 |
# File 'lib/active_reporter/dimension/base.rb', line 19 def attribute .fetch(:attribute, name) end |
#expression ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/active_reporter/dimension/base.rb', line 23 def expression @expression ||= if .include?(:expression) [:expression] elsif .include?(:_alias) "'#{[:_alias]}'" else "#{table_name}.#{column}" end end |
#extract_sql_value(row) ⇒ Object
Given a single (hashified) row of the SQL result, return the Ruby object representing this dimension’s value
55 56 57 |
# File 'lib/active_reporter/dimension/base.rb', line 55 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
39 40 41 |
# File 'lib/active_reporter/dimension/base.rb', line 39 def filter(relation) raise NotImplementedError end |
#filter_values ⇒ Object
59 60 61 |
# File 'lib/active_reporter/dimension/base.rb', line 59 def filter_values array_param(:only).uniq end |
#filtering? ⇒ Boolean
Return whether the report should filter by this dimension
64 65 66 |
# File 'lib/active_reporter/dimension/base.rb', line 64 def filtering? filter_values.present? end |
#group(relation) ⇒ Object
Group the relation by the expression – ensure this is ordered, too.
44 45 46 |
# File 'lib/active_reporter/dimension/base.rb', line 44 def group(relation) raise NotImplementedError end |
#group_values ⇒ Object
Return an ordered array of all values that should appear in ‘Report#data`
49 50 51 |
# File 'lib/active_reporter/dimension/base.rb', line 49 def group_values raise NotImplementedError end |
#grouping? ⇒ Boolean
68 69 70 |
# File 'lib/active_reporter/dimension/base.rb', line 68 def grouping? report.groupers.include?(self) end |
#model ⇒ Object
15 16 17 |
# File 'lib/active_reporter/dimension/base.rb', line 15 def model @model ||= [:model].to_s.classify.safe_constantize || [:model] || report.report_model end |
#null_order ⇒ Object
94 95 96 97 98 |
# File 'lib/active_reporter/dimension/base.rb', line 94 def null_order return unless ActiveReporter.database_type == :postgres nulls_last? ? "NULLS LAST" : "NULLS FIRST" end |
#nulls_last? ⇒ Boolean
88 89 90 91 92 |
# File 'lib/active_reporter/dimension/base.rb', line 88 def nulls_last? value = dimension_or_root_param(:nulls_last) value = !value if sort_desc? value end |
#order(relation) ⇒ Object
76 77 78 |
# File 'lib/active_reporter/dimension/base.rb', line 76 def order(relation) relation.order(Arel.sql("#{order_expression} #{sort_order} #{null_order}")) end |
#order_expression ⇒ Object
72 73 74 |
# File 'lib/active_reporter/dimension/base.rb', line 72 def order_expression sql_value_name end |
#params ⇒ Object
100 101 102 |
# File 'lib/active_reporter/dimension/base.rb', line 100 def params report.params.fetch(:dimensions, {})[name].presence || {} end |
#relate(relation) ⇒ Object
Do any joins/selects necessary to filter or group the relation.
34 35 36 |
# File 'lib/active_reporter/dimension/base.rb', line 34 def relate(relation) .fetch(:relation, ->(r) { r }).call(relation) end |
#sort_desc? ⇒ Boolean
80 81 82 |
# File 'lib/active_reporter/dimension/base.rb', line 80 def sort_desc? dimension_or_root_param(:sort_desc) end |
#sort_order ⇒ Object
84 85 86 |
# File 'lib/active_reporter/dimension/base.rb', line 84 def sort_order sort_desc? ? "DESC" : "ASC" end |