Module: Morpheus::Mixins::Associations::ClassMethods

Defined in:
lib/morpheus/mixins/associations.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to(association, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/morpheus/mixins/associations.rb', line 15

def belongs_to(association, options = {})
  attribute_id_sym  = "#{association}_id".to_sym

  property attribute_id_sym

  create_reflection(:belongs_to, association, options)

  define_method association do
    instance_variable_get("@#{association}") ||
      instance_variable_set("@#{association}", BelongsToAssociation.new(self, association, :options => options))
  end

  define_method "#{association}=" do |target|
    instance_variable_set("@#{association}", BelongsToAssociation.new(self, association, :target => target, :options => options))
    send("#{association}_id=", target.id)
  end
end

#has_many(association, options = {}) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/morpheus/mixins/associations.rb', line 33

def has_many(association, options = {})
  create_reflection(:has_many, association, options)

  define_method association do
    instance_variable_get("@#{association}") ||
      instance_variable_set("@#{association}", HasManyAssociation.new(self, association, :options => options))
  end
end

#has_one(association, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/morpheus/mixins/associations.rb', line 42

def has_one(association, options = {})
  create_reflection(:has_one, association, options)

  define_method association do
    instance_variable_get("@#{association}") ||
      instance_variable_set("@#{association}", HasOneAssociation.new(self, association, :options => options))
  end

  define_method "#{association}=" do |target|
    instance_variable_set("@#{association}", HasOneAssociation.new(self, association, :target => target, :options => options))
  end
end