Module: Moribus::AliasAssociation::ClassMethods

Defined in:
lib/moribus/alias_association.rb

Overview

Class methods for ActiveRecord::Base

Instance Method Summary collapse

Instance Method Details

#alias_association(alias_name, association_name) ⇒ Object

Aliases association reflection in reflections hash and association-specific methods. See module description for example



24
25
26
27
28
29
30
# File 'lib/moribus/alias_association.rb', line 24

def alias_association(alias_name, association_name)
  if reflection = reflect_on_association(association_name)
    reflections[alias_name] = reflections[association_name]
    alias_association_methods(alias_name, reflection)
    reflection
  end
end

#belongs_to(name, opts = {}) ⇒ Object

Allows :alias option to alias belongs_to association



33
34
35
36
37
38
# File 'lib/moribus/alias_association.rb', line 33

def belongs_to(name, opts = {})
  alias_name = opts.delete(:alias)
  reflection = super(name, opts)
  alias_association(alias_name, name) if alias_name
  reflection
end

#has_many(name, opts = {}) ⇒ Object

Allows :alias option to alias has_many association



41
42
43
44
45
46
# File 'lib/moribus/alias_association.rb', line 41

def has_many(name, opts = {})
  alias_name = opts.delete(:alias)
  reflection = super(name, opts)
  alias_association(alias_name, name) if alias_name
  reflection
end

#has_one(name, opts = {}) ⇒ Object

Allows :alias option to alias has_one association



49
50
51
52
53
54
# File 'lib/moribus/alias_association.rb', line 49

def has_one(name, opts = {})
  alias_name = opts.delete(:alias)
  reflection = super(name, opts)
  alias_association(alias_name, name) if alias_name
  reflection
end