Class: ActiveReporting::ReportingDimension

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dimension, label: nil, label_name: nil, join_method: nil, datetime_drill: nil) ⇒ ReportingDimension

Returns a new instance of ReportingDimension.

Parameters:

  • dimension (ActiveReporting::Dimension)
  • label (Hash) (defaults to: nil)

    a customizable set of options

  • label_name (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (label:):

  • Hierarchical (Maybe<Symbol>)

    dimension to be used as a label

Options Hash (label_name:):

  • Hierarchical (Maybe<Symbol|String>)

    dimension custom name



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_methodObject (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

#labelObject (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, options = 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 !options.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
      options = { datetime_drill: options }
    end
    new(found_dimension, **label_config(options))
  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

Parameters:

  • options (Symbol|Hash)


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(options)
  unless options.is_a?(Hash)
    return { label: options }
  end

  {
    label: options[:field],
    label_name: options[:name],
    join_method: options[:join_method],
    datetime_drill: options[:datetime_drill]
  }
end

Instance Method Details

#foreign_keyString

The foreign key to use in queries

Returns:

  • (String)


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

Returns:

  • (Array)


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_callbackLambda, NilClass

Looks up the dimension label callback for the label

Returns:

  • (Lambda, NilClass)


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

Returns:

  • (String)


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

Returns:

  • (Array)


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