Module: Moribus::Extensions

Extended by:
ActiveSupport::Autoload, ActiveSupport::Concern
Defined in:
lib/moribus/extensions.rb,
lib/moribus/extensions/delegate_associated.rb,
lib/moribus/extensions/has_current_extension.rb,
lib/moribus/extensions/has_aggregated_extension.rb

Overview

Extends the default Rails has_one and belongs_to associations to enable tracked and aggregated behaviors.

Defined Under Namespace

Modules: ClassMethods, DelegateAssociated, HasAggregatedExtension, HasCurrentExtension

Instance Method Summary collapse

Instance Method Details

#association(name) ⇒ Object

Overrides Rails’ default #association method to extend resulting association objects by custom behaviors.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/moribus/extensions.rb', line 24

def association(name)
  association = super
  reflection = self.class.reflect_on_association(name)
  case reflection.macro
  when :belongs_to
    association.extend(HasAggregatedExtension) if reflection.options[:aggregated]
  when :has_one
    association.extend(HasCurrentExtension) if reflection.options[:is_current]
  else # do nothing
  end
  association
end