class ActiveReporting::ReportingDimension

Constants

DATETIME_HIERARCHIES

Values for the Postgres `date_trunc` method. See www.postgresql.org/docs/10/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC

JOIN_METHODS
SUPPORTED_DBS

Attributes

join_method[R]
label[R]

Public Class Methods

build_from_dimensions(fact_model, dimensions) click to toggle source
# 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) click to toggle source

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

@param [Symbol|Hash] options

# 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
new(dimension, label: nil, label_name: nil, join_method: nil, datetime_drill: nil) click to toggle source

@param dimension [ActiveReporting::Dimension] @option label [Maybe<Symbol>] Hierarchical dimension to be used as a label @option label_name [Maybe<Symbol|String>] Hierarchical dimension custom name

# 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

Public Instance Methods

foreign_key() click to toggle source

The foreign key to use in queries

@return [String]

# File lib/active_reporting/reporting_dimension.rb, line 69
def foreign_key
  association ? association.foreign_key : name
end
group_by_statement(with_identifier: true) click to toggle source

Fragments of a group by clause for queries that use the dimension

@return [Array]

# 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() click to toggle source

Looks up the dimension label callback for the label

@return [Lambda, NilClass]

# File lib/active_reporting/reporting_dimension.rb, line 103
def label_callback
  klass.fact_model.dimension_label_callbacks[@label]
end
order_by_statement(direction:) click to toggle source

Fragment of an order by clause for queries that sort by the dimension

@return [String]

# 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) click to toggle source

Fragments of a select statement for queries that use the dimension

@return [Array]

# 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