forked from rails/rails
/
associations.rb
154 lines (129 loc) · 4.87 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
module ActiveResource
module Associations
def self.included(klass)
klass.send :include, InstanceMethods
klass.extend ClassMethods
end
module InstanceMethods
def set_resource_instance_variable(resource, default_value = nil)
if !instance_variable_defined?("@#{resource}")
instance_variable_set("@#{resource}", yield)
end
instance_variable_get("@#{resource}")
end
end
module ClassMethods
def options(association, resource)
o = { :klass => klass_for(association, resource) }
o[:host_klass] = self
case association
when :has_many
o[:association_col] = o[:host_klass].to_s.singularize
when :belongs_to
o[:association_col] = o[:klass]
when :has_one
o[:association_col] = o[:host_klass].to_s
end
o[:association_col] = "#{o[:association_col].underscore}_id".to_sym
o
end
def klass_for(association, resource)
resource = resource.to_s
resource = resource.singularize if association == :has_many
resource.camelize
end
#######################################################################
# has_one association
#
def has_one(resource, opts = {})
o = options(:has_one, resource)
#----------------------------------------------------------------------#
# Define accessor method for resource
#
#----------------------------------------------------------------------#
define_method(resource) do
set_resource_instance_variable(resource) do
o[:klass].constantize.find(:first, :params => { o[:association_col] => id })
end
end
#----------------------------------------------------------------------#
# Define writter method for resource
#
#----------------------------------------------------------------------#
define_method("#{resource}=") do |new_resource|
if send(resource).blank?
new_resource.send("#{o[:association_col]}=", id)
instance_variable_set("@#{resource}", new_resource.save)
else
instance_variable_get("@#{resource}").send(:update_attribute, o[:association_col], id)
end
end
end
#######################################################################
# belongs_to association
#
def belongs_to(resource, opts = {})
o = options(:belongs_to, resource)
#----------------------------------------------------------------------#
# Define accessor method for resource
#
#----------------------------------------------------------------------#
define_method(resource) do
association_col = send o[:association_col]
return nil if association_col.nil?
set_resource_instance_variable(resource){ o[:klass].constantize.find(association_col) }
end
#----------------------------------------------------------------------#
# Define writter method for resource
#
#----------------------------------------------------------------------#
define_method("#{resource}=") do |new_resource|
if send(o[:association_col]) != new_resource.id
send "#{o[:association_col]}=", new_resource.id
end
instance_variable_set("@#{resource}", new_resource)
end
end
#######################################################################
# has_many association
#
def has_many(resource, opts = {})
o = options(:has_many, resource)
#----------------------------------------------------------------------#
# Define accessor method for resource
#
#----------------------------------------------------------------------#
define_method(resource) do
collection = set_resource_instance_variable(resource) {
o[:klass].constantize.find(:all,
:params => { o[:association_col] => id }) || []
}
instance_eval "
def collection.<<(member)
member.#{o[:association_col]} = #{id}
member.save
super(member)
end"
instance_eval "
def collection.delete(member)
member.#{o[:association_col]} = nil
member.save
super(member)
end"
instance_eval "
def collection.clear
self.each{|member| delete(member)}
super
end"
collection
end
define_method("#{resource}=") do |new_collection|
collection = send(resource)
to_remove = collection - new_collection
to_remove.each{|m| collection.delete(m)}
instance_variable_set("@#{resource}", new_collection)
end
end
end
end
end