Class: Moribus::Extensions::HasAggregatedExtension::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/moribus/extensions/has_aggregated_extension.rb

Overview

This helper class is used to effectively extend has_aggregated association by adding attribute delegation of attributes and enum readers to the effective reader of the association owner. Behaves much like ActiveRecord::Associations::Builder classes.

Constant Summary collapse

EXCLUDE_METHODS_REGEXP =

Among all attribute methods, we’re interested only in reader and writers - discard the rest

/^_|\?$|^reset|_cast$|_was$|_change!?$|lock_version|created_at|updated_at/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, reflection) ⇒ Helper

Save association owner and reflection for subsequent processing.



26
27
28
# File 'lib/moribus/extensions/has_aggregated_extension.rb', line 26

def initialize(model, reflection)
  @model, @reflection = model, reflection
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



23
24
25
# File 'lib/moribus/extensions/has_aggregated_extension.rb', line 23

def model
  @model
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



23
24
25
# File 'lib/moribus/extensions/has_aggregated_extension.rb', line 23

def reflection
  @reflection
end

Instance Method Details

#extendObject

Extend association: add the delegation module to the reflection, fill it with the delegation methods and include it into the model.



32
33
34
35
36
# File 'lib/moribus/extensions/has_aggregated_extension.rb', line 32

def extend
  define_delegation_module(reflection)
  add_delegated_methods(reflection)
  include_delegation_module(reflection)
end