Class: ActiveReporting::FactModel

Inherits:
Object
  • Object
show all
Defined in:
lib/active_reporting/fact_model.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dimension_filtersObject (readonly)

Returns the value of attribute dimension_filters.



6
7
8
# File 'lib/active_reporting/fact_model.rb', line 6

def dimension_filters
  @dimension_filters
end

.dimensionsObject (readonly)

Returns the value of attribute dimensions.



6
7
8
# File 'lib/active_reporting/fact_model.rb', line 6

def dimensions
  @dimensions
end

.measureSymbol

The default measure used when this fact model is reported on

Returns:

  • (Symbol)


53
54
55
# File 'lib/active_reporting/fact_model.rb', line 53

def self.measure
  @measure ||= Configuration.default_measure
end

Class Method Details

.default_dimension_label(label) ⇒ Symbol

When this fact model is used as a dimension, this is the label it will use by default

Returns:

  • (Symbol)


80
81
82
# File 'lib/active_reporting/fact_model.rb', line 80

def self.default_dimension_label(label)
  @dimension_label = label.to_sym
end

.dimension(name) ⇒ Object

Declares a dimension for this fact model

Parameters:

  • name (String, Symbol)

    The name of the dimension



95
96
97
98
# File 'lib/active_reporting/fact_model.rb', line 95

def self.dimension(name)
  @dimensions ||= {}
  @dimensions[name.to_sym] = Dimension.new(self, name: name)
end

.dimension_filter(name, lambda_or_type = :scope) ⇒ Object

Note:

If not provided, the type of the dimension filter will be a scope. Meaning the ActiveReporting is assuming there’s a scope on the fact model’s model named the same. You may pass in ‘:ransack` to say this dimension filter is a ransack search term. Finally, you may pass in a callable object similar to defining a scope on ActiveRecord

Declares a dimension filter for this fact model

Examples:

class PostFactModel < ActiveReporting::FactModel
  # Assumes there's an `active` scope on the model
  dimension_filter :active

  # Uses the ransack search term `joined_at_gte`
  dimension_filter :joined_at_gte, :ransack

  # Implements a dimension filter like an ActiveRecord scope
  dimension_filter :some_filter, ->(input) { where(foo: input) }
end

Parameters:

  • name (Stirng, Symbol)

    The name of the dimension filter

  • lambda_or_type (Symbol, Lambda) (defaults to: :scope)


141
142
143
144
# File 'lib/active_reporting/fact_model.rb', line 141

def self.dimension_filter(name, lambda_or_type = :scope)
  @dimension_filters ||= {}
  @dimension_filters[name.to_sym] = DimensionFilter.build(name, lambda_or_type)
end

.dimension_hierarchy(levels) ⇒ Object

Specifies an in order array of columns which describes a series of columns that may be used as dimensions in a hierarchy.

For example, a fact model of tablets may have a hierarchy of name -> manufacturer -> operating system.

Parameters:

  • levels (Array)

    An array of symbols or strings of columns



72
73
74
# File 'lib/active_reporting/fact_model.rb', line 72

def self.dimension_hierarchy(levels)
  @hierarchical_levels = Array(levels).map(&:to_sym)
end

.dimension_labelSymbol

Returns the dimension label used when this fact model is used as a dimension

Returns:

  • (Symbol)


87
88
89
90
# File 'lib/active_reporting/fact_model.rb', line 87

def self.dimension_label
  @dimension_label ||= nil
  @dimension_label || Configuration.default_dimension_label
end

.dimension_label_callback(column, body) ⇒ Object

Sets a call back for a given dimension label. The returned value of the callable body will be used as the label value when used in a report. The label’s raw database value is passed to the callback.

Parameters:

  • column (Symbol, String)
  • body (Lambda)

Raises:

  • (ArgumentError)


113
114
115
116
117
# File 'lib/active_reporting/fact_model.rb', line 113

def self.dimension_label_callback(column, body)
  @dimension_label_callbacks ||= {}
  raise ArgumentError, 'Dimension label callback body must be a callable object' unless body.respond_to?(:call)
  @dimension_label_callbacks[column.to_sym] = body
end

.dimension_label_callbacksHash

Returns a hash of dimension label to callback mappings

Returns:

  • (Hash)


103
104
105
# File 'lib/active_reporting/fact_model.rb', line 103

def self.dimension_label_callbacks
  @dimension_label_callbacks ||= {}
end

.find_dimension_filter(name) ⇒ ActiveReporting::DimensionFilter

Finds a dimension filter defined on a fact model given a name

Parameters:

  • name (Symbol)

Returns:

Raises:



168
169
170
171
172
173
174
# File 'lib/active_reporting/fact_model.rb', line 168

def self.find_dimension_filter(name)
  @dimension_filters ||= {}
  dm = @dimension_filters[name.to_sym]
  return dm if dm.present?
  return @dimension_filters[name.to_sym] = DimensionFilter.build(name, :ransack) if ransack_fallback
  raise UnknownDimensionFilter, "Dimension filter '#{name}' not found on fact model '#{self.name}'"
end

.hierarchical_levelsArray

The (in order) hierarchical levels of the fact model when used as a dimension.

Returns:

  • (Array)


61
62
63
# File 'lib/active_reporting/fact_model.rb', line 61

def self.hierarchical_levels
  @hierarchical_levels ||= []
end

.modelClass

Returns the ActiveRecord model linked to the FactModel.

If not already set, FactModel assumes the model is based off of the name of the class

Examples:

class PostFactModel < ActiveReporting::FactModel
end

PostFactModel.model
=> Post

Returns:

  • (Class)

    the ActiveRecord model



46
47
48
# File 'lib/active_reporting/fact_model.rb', line 46

def self.model
  @model ||= name.gsub(/FactModel\z/, '').constantize
end

.model=(model) ⇒ Class

Note:

You should only need to set this if the name of your fact model does not follow the pattern of [MyModel]FactModel

Explicitly sets which ActiveRecord model to to link to this fact model.

Examples:

class PostFactModel < ActiveReporting::FactModel
  self.model= :post
  self.model= Post
  self.model= 'post'
end

Parameters:

  • model (String, Symbol, Class)

Returns:

  • (Class)

    the ActiveRecord model



24
25
26
27
28
29
30
31
32
# File 'lib/active_reporting/fact_model.rb', line 24

def self.model=(model)
  @model = if model.is_a?(String) || model.is_a?(Symbol)
             model.to_s.classify.constantize
           else
             model
           end
  @model.instance_variable_set('@fact_model', self)
  @model
end

.use_ransack_for_unknown_dimension_filtersObject

Invoke this method to make all dimension filters fallback to use ransack if they are not defined as scopes on the model



148
149
150
151
152
153
# File 'lib/active_reporting/fact_model.rb', line 148

def self.use_ransack_for_unknown_dimension_filters
  unless Configuration.ransack_available
    raise RansackNotAvailable, 'Ransack not available. Please include it in your Gemfile.'
  end
  @ransack_fallback = true
end