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, scope = nil, options = {}) ⇒ Object

Allows :alias option to alias belongs_to association



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

def belongs_to(name, scope = nil, options = {})
  options = scope if scope.is_a?(Hash)

  alias_name = options.delete(:alias)
  reflection = super(name, scope, options)
  alias_association(alias_name, name) if alias_name
  reflection
end

#has_many(name, scope = nil, options = {}, &extension) ⇒ Object

Allows :alias option to alias has_many association



43
44
45
46
47
48
49
50
# File 'lib/moribus/alias_association.rb', line 43

def has_many(name, scope = nil, options = {}, &extension)
  options = scope if scope.is_a?(Hash)

  alias_name = options.delete(:alias)
  reflection = super(name, scope, options, &extension)
  alias_association(alias_name, name) if alias_name
  reflection
end

#has_one(name, scope = nil, options = {}) ⇒ Object

Allows :alias option to alias has_one association



53
54
55
56
57
58
59
60
# File 'lib/moribus/alias_association.rb', line 53

def has_one(name, scope = nil, options = {})
  options = scope if scope.is_a?(Hash)

  alias_name = options.delete(:alias)
  reflection = super(name, scope, options)
  alias_association(alias_name, name) if alias_name
  reflection
end