Class: ActiveReporting::FactModel
- Inherits:
-
Object
- Object
- ActiveReporting::FactModel
- Defined in:
- lib/active_reporting/fact_model.rb
Class Attribute Summary collapse
-
.dimension_filters ⇒ Object
readonly
Returns the value of attribute dimension_filters.
-
.dimensions ⇒ Object
readonly
Returns the value of attribute dimensions.
-
.measure ⇒ Symbol
The default measure used when this fact model is reported on.
Class Method Summary collapse
-
.default_dimension_label(label) ⇒ Symbol
When this fact model is used as a dimension, this is the label it will use by default.
-
.dimension(name) ⇒ Object
Declares a dimension for this fact model.
-
.dimension_filter(name, lambda_or_type = :scope) ⇒ Object
Declares a dimension filter for this fact model.
-
.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.
-
.dimension_label ⇒ Symbol
Returns the dimension label used when this fact model is used as a dimension.
-
.dimension_label_callback(column, body) ⇒ Object
Sets a call back for a given dimension label.
-
.dimension_label_callbacks ⇒ Hash
Returns a hash of dimension label to callback mappings.
-
.find_dimension_filter(name) ⇒ ActiveReporting::DimensionFilter
Finds a dimension filter defined on a fact model given a name.
-
.hierarchical_levels ⇒ Array
The (in order) hierarchical levels of the fact model when used as a dimension.
-
.model ⇒ Class
Returns the ActiveRecord model linked to the FactModel.
-
.model=(model) ⇒ Class
Explicitly sets which ActiveRecord model to to link to this fact model.
-
.use_ransack_for_unknown_dimension_filters ⇒ Object
Invoke this method to make all dimension filters fallback to use ransack if they are not defined as scopes on the model.
Class Attribute Details
.dimension_filters ⇒ Object (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 |
.dimensions ⇒ Object (readonly)
Returns the value of attribute dimensions.
6 7 8 |
# File 'lib/active_reporting/fact_model.rb', line 6 def dimensions @dimensions end |
.measure ⇒ Symbol
The default measure used when this fact model is reported on
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
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
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
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
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.
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_label ⇒ Symbol
Returns the dimension label used when this fact model is used as a dimension
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.
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_callbacks ⇒ Hash
Returns a hash of dimension label to callback mappings
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
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_levels ⇒ Array
The (in order) hierarchical levels of the fact model when used as a dimension.
61 62 63 |
# File 'lib/active_reporting/fact_model.rb', line 61 def self.hierarchical_levels @hierarchical_levels ||= [] end |
.model ⇒ Class
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
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
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.
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_filters ⇒ Object
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 |