Class: ActiveReporting::ReportingDimension
- Inherits:
-
Object
- Object
- ActiveReporting::ReportingDimension
- Extended by:
- Forwardable
- Defined in:
- lib/active_reporting/reporting_dimension.rb
Constant Summary collapse
- SUPPORTED_DBS =
%w[PostgreSQL PostGIS Mysql2].freeze
- DATETIME_HIERARCHIES =
Values for the Postgres ‘date_trunc` method. See www.postgresql.org/docs/10/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
%i[microseconds milliseconds second minute hour day week month quarter year decade century millennium date].freeze
- JOIN_METHODS =
{ joins: :joins, left_outer_joins: :left_outer_joins }.freeze
Instance Attribute Summary collapse
-
#join_method ⇒ Object
readonly
Returns the value of attribute join_method.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
Class Method Summary collapse
- .build_from_dimensions(fact_model, dimensions) ⇒ Object
-
.label_config(options) ⇒ Object
If you pass a symbol it means you just indicate the field on that dimension.
Instance Method Summary collapse
-
#foreign_key ⇒ String
The foreign key to use in queries.
-
#group_by_statement(with_identifier: true) ⇒ Array
Fragments of a group by clause for queries that use the dimension.
-
#initialize(dimension, label: nil, label_name: nil, join_method: nil, datetime_drill: nil) ⇒ ReportingDimension
constructor
A new instance of ReportingDimension.
-
#label_callback ⇒ Lambda, NilClass
Looks up the dimension label callback for the label.
-
#order_by_statement(direction:) ⇒ String
Fragment of an order by clause for queries that sort by the dimension.
-
#select_statement(with_identifier: true) ⇒ Array
Fragments of a select statement for queries that use the dimension.
Constructor Details
#initialize(dimension, label: nil, label_name: nil, join_method: nil, datetime_drill: nil) ⇒ ReportingDimension
Returns a new instance of ReportingDimension.
57 58 59 60 61 62 63 64 |
# File 'lib/active_reporting/reporting_dimension.rb', line 57 def initialize(dimension, label: nil, label_name: nil, join_method: nil, datetime_drill: nil) @dimension = dimension determine_label_field(label) determine_datetime_drill(datetime_drill) determine_label_name(label_name) determine_join_method(join_method) end |
Instance Attribute Details
#join_method ⇒ Object (readonly)
Returns the value of attribute join_method.
13 14 15 |
# File 'lib/active_reporting/reporting_dimension.rb', line 13 def join_method @join_method end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
13 14 15 |
# File 'lib/active_reporting/reporting_dimension.rb', line 13 def label @label end |
Class Method Details
.build_from_dimensions(fact_model, dimensions) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/active_reporting/reporting_dimension.rb', line 17 def self.build_from_dimensions(fact_model, dimensions) Array(dimensions).map do |dim| dimension_name, = dim.is_a?(Hash) ? Array(dim).flatten : [dim, nil] found_dimension = fact_model.dimensions[dimension_name.to_sym] raise(UnknownDimension, "Dimension '#{dimension_name}' not found on fact model '#{fact_model}'") if found_dimension.nil? # Ambiguous behavior with string option for degenerate and standard dimension if !.is_a?(Hash) && found_dimension.type == Dimension::TYPES[:degenerate] ActiveSupport::Deprecation.warn <<~EOS direct use of implict hierarchies is deprecated and will be removed in future versions. \ Please use `:datetime_drill` option instead. EOS = { datetime_drill: } end new(found_dimension, **label_config()) end end |
.label_config(options) ⇒ Object
If you pass a symbol it means you just indicate the field on that dimension. With a hash you can customize the name of the label
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/active_reporting/reporting_dimension.rb', line 41 def self.label_config() unless .is_a?(Hash) return { label: } end { label: [:field], label_name: [:name], join_method: [:join_method], datetime_drill: [:datetime_drill] } end |
Instance Method Details
#foreign_key ⇒ String
The foreign key to use in queries
69 70 71 |
# File 'lib/active_reporting/reporting_dimension.rb', line 69 def foreign_key association ? association.foreign_key : name end |
#group_by_statement(with_identifier: true) ⇒ Array
Fragments of a group by clause for queries that use the dimension
85 86 87 88 89 |
# File 'lib/active_reporting/reporting_dimension.rb', line 85 def group_by_statement(with_identifier: true) group = [label_fragment] group << identifier_fragment if with_identifier && type == Dimension::TYPES[:standard] group end |
#label_callback ⇒ Lambda, NilClass
Looks up the dimension label callback for the label
103 104 105 |
# File 'lib/active_reporting/reporting_dimension.rb', line 103 def label_callback klass.fact_model.dimension_label_callbacks[@label] end |
#order_by_statement(direction:) ⇒ String
Fragment of an order by clause for queries that sort by the dimension
94 95 96 97 98 |
# File 'lib/active_reporting/reporting_dimension.rb', line 94 def order_by_statement(direction:) direction = direction.to_s.upcase raise "Ording direction should be 'asc' or 'desc'" unless %w[ASC DESC].include?(direction) "#{label_fragment} #{direction}" end |
#select_statement(with_identifier: true) ⇒ Array
Fragments of a select statement for queries that use the dimension
76 77 78 79 80 |
# File 'lib/active_reporting/reporting_dimension.rb', line 76 def select_statement(with_identifier: true) ss = ["#{label_fragment} AS #{label_fragment_alias}"] ss << "#{identifier_fragment} AS #{identifier_fragment_alias}" if with_identifier && type == Dimension::TYPES[:standard] ss end |