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
|
# File 'lib/kasket/active_record_patches.rb', line 3
def association_accessor_methods(reflection, association_proxy_class)
define_method(reflection.name) do |*params|
force_reload = params.first unless params.empty?
association = association_instance_get(reflection.name)
if association.nil? || force_reload
association = association_proxy_class.new(self, reflection)
retval = force_reload ? association.reload : association.__send__(:load_target)
if retval.nil? and association_proxy_class == ActiveRecord::Associations::BelongsToAssociation
association_instance_set(reflection.name, nil)
return nil
end
association_instance_set(reflection.name, association)
end
association.target.nil? ? nil : association
end
define_method("loaded_#{reflection.name}?") do
association = association_instance_get(reflection.name)
association && association.loaded?
end
define_method("#{reflection.name}=") do |new_value|
association = association_instance_get(reflection.name)
if association.nil? || association.target != new_value
association = association_proxy_class.new(self, reflection)
end
if association_proxy_class == ActiveRecord::Associations::HasOneThroughAssociation
association.create_through_record(new_value)
if new_record?
association_instance_set(reflection.name, new_value.nil? ? nil : association)
else
self.send(reflection.name, new_value)
end
else
association.replace(new_value)
association_instance_set(reflection.name, new_value.nil? ? nil : association)
end
end
define_method("set_#{reflection.name}_target") do |target|
return if target.nil? and association_proxy_class == ActiveRecord::Associations::BelongsToAssociation
association = association_proxy_class.new(self, reflection)
association.target = target
association_instance_set(reflection.name, association)
end
end
|