forked from mongodb/mongoid
/
associations.rb
108 lines (102 loc) · 3.28 KB
/
associations.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
require "mongoid/associations/decorator"
require "mongoid/associations/accessor"
require "mongoid/associations/belongs_to"
require "mongoid/associations/has_many"
require "mongoid/associations/has_one"
module Mongoid # :nodoc:
module Associations #:nodoc:
def self.included(base)
base.class_eval do
include InstanceMethods
extend ClassMethods
end
end
module InstanceMethods
def associations
self.class.associations
end
end
module ClassMethods
def associations
@associations ||= HashWithIndifferentAccess.new
end
# Adds the association back to the parent document. This macro is
# necessary to set the references from the child back to the parent
# document. If a child does not define this association calling
# persistence methods on the child object will cause a save to fail.
#
# Options:
#
# association_name: A +Symbol+ that matches the name of the parent class.
#
# Example:
#
# class Person < Mongoid::Document
# has_many :addresses
# end
#
# class Address < Mongoid::Document
# belongs_to :person
# end
def belongs_to(association_name, options = {})
@embedded = true
add_association(Associations::BelongsTo, association_name, options)
end
# Adds the association from a parent document to its children. The name
# of the association needs to be a pluralized form of the child class
# name.
#
# Options:
#
# association_name: A +Symbol+ that is the plural child class name.
#
# Example:
#
# class Person < Mongoid::Document
# has_many :addresses
# end
#
# class Address < Mongoid::Document
# belongs_to :person
# end
def has_many(association_name, options = {})
add_association(Associations::HasMany, association_name, options)
end
# Adds the association from a parent document to its child. The name
# of the association needs to be a singular form of the child class
# name.
#
# Options:
#
# association_name: A +Symbol+ that is the plural child class name.
#
# Example:
#
# class Person < Mongoid::Document
# has_many :addresses
# end
#
# class Address < Mongoid::Document
# belongs_to :person
# end
def has_one(association_name, options = {})
add_association(Associations::HasOne, association_name, options)
end
private
# Adds the association to the associations hash with the type as the key,
# then adds the accessors for the association.
def add_association(type, name, options = {})
associations[name] = type
define_method(name) do
return instance_variable_get("@#{name}") if instance_variable_defined?("@#{name}")
proxy = Associations::Accessor.get(type, name, self, options)
instance_variable_set("@#{name}", proxy)
end
define_method("#{name}=") do |object|
proxy = Associations::Accessor.set(type, name, self, object, options)
instance_variable_set("@#{name}", proxy)
end
end
end
end
end