/
association_proxy.rb
45 lines (37 loc) · 1.24 KB
/
association_proxy.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
module Her
module Model
module Associations
class AssociationProxy < (ActiveSupport.const_defined?('ProxyObject') ? ActiveSupport::ProxyObject : ActiveSupport::BasicObject)
# @private
def self.install_proxy_methods(target_name, *names)
names.each do |name|
module_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{name}(*args, &block)
#{target_name}.send(#{name.inspect}, *args, &block)
end
RUBY
end
end
install_proxy_methods :association,
:build, :create, :where, :find, :all, :assign_nested_attributes
# @private
def initialize(association)
@_her_association = association
end
def association
@_her_association
end
# @private
def method_missing(name, *args, &block)
if :object_id == name # avoid redefining object_id
return association.fetch.object_id
end
# create a proxy to the fetched object's method
AssociationProxy.install_proxy_methods 'association.fetch', name
# resend message to fetched object
__send__(name, *args, &block)
end
end
end
end
end