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].freeze
DATETIME_HIERARCHIES =
i[microseconds milliseconds second minute hour day week month quarter year decade
century millennium].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dimension, label: nil, label_name: 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



42
43
44
45
46
47
# File 'lib/active_reporting/reporting_dimension.rb', line 42

def initialize(dimension, label: nil, label_name: nil)
  @dimension = dimension

  determine_label_field(label)
  determine_label_name(label_name)
end

Class Method Details

.build_from_dimensions(fact_model, dimensions) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/active_reporting/reporting_dimension.rb', line 14

def self.build_from_dimensions(fact_model, dimensions)
  Array(dimensions).map do |dim|
    dimension_name, label = dim.is_a?(Hash) ? Array(dim).flatten : [dim, nil]
    found_dimension = fact_model.dimensions[dimension_name.to_sym]

    raise(UnknownDimension, "Dimension '#{dim}' not found on fact model '#{fact_model}'") if found_dimension.nil?

    new(found_dimension, label_config(label))
  end
end

.label_config(label) ⇒ 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:

  • label (Symbol|Hash)


30
31
32
33
34
35
36
37
# File 'lib/active_reporting/reporting_dimension.rb', line 30

def self.label_config(label)
  return { label: label } unless label.is_a?(Hash)

  {
    label: label[:field],
    label_name: label[:name]
  }
end

Instance Method Details

#foreign_keyString

The foreign key to use in queries

Returns:

  • (String)


52
53
54
# File 'lib/active_reporting/reporting_dimension.rb', line 52

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)


70
71
72
73
74
75
76
# File 'lib/active_reporting/reporting_dimension.rb', line 70

def group_by_statement(with_identifier: true)
  return [degenerate_fragment] if type == Dimension::TYPES[:degenerate]

  group = [label_fragment]
  group << identifier_fragment if with_identifier
  group
end

#label_callbackLambda, NilClass

Looks up the dimension label callback for the label

Returns:

  • (Lambda, NilClass)


91
92
93
# File 'lib/active_reporting/reporting_dimension.rb', line 91

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)


81
82
83
84
85
86
# File 'lib/active_reporting/reporting_dimension.rb', line 81

def order_by_statement(direction:)
  direction = direction.to_s.upcase
  raise "Ording direction should be 'asc' or 'desc'" unless %w[ASC DESC].include?(direction)
  return "#{degenerate_fragment} #{direction}" if type == Dimension::TYPES[:degenerate]
  "#{label_fragment} #{direction}"
end

#select_statement(with_identifier: true) ⇒ Array

Fragments of a select statement for queries that use the dimension

Returns:

  • (Array)


59
60
61
62
63
64
65
# File 'lib/active_reporting/reporting_dimension.rb', line 59

def select_statement(with_identifier: true)
  return [degenerate_select_fragment] if type == Dimension::TYPES[:degenerate]

  ss = ["#{label_fragment} AS #{@label_name}"]
  ss << "#{identifier_fragment} AS #{name}_identifier" if with_identifier
  ss
end