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
|
# File 'lib/apigee-platform/associations.rb', line 10
def has_many(*args)
klass_sym = args.first.to_sym
options = args.
prefix = self.name
klass_name = prefix + klass_sym.to_s.classify
instance_eval do
define_method "#{klass_sym}" do |scope = :all|
klass = klass_name.constantize
prefix_params = klass.prefix_options.inject({}) do |res,pair|
res[pair.first] = self.send(options[pair.first] || pair.last)
res
end
if options[:through].present?
self.send(options[:through]).map do |el|
klass.instantiate_record(el.attributes, prefix_params)
end
else
klass.find scope, :params => prefix_params
end
end
define_method "#{klass_sym.to_s.singularize}" do |id = nil|
if id
method(klass_sym).call(id)
else
method("create_#{klass_sym.to_s.singularize}").call
end
end
define_method "create_#{klass_sym.to_s.singularize}" do |params={}|
klass = klass_name.constantize
prefix_params = klass.prefix_options.inject({}){|res,pair| res[pair.last] = self.send(options[pair.first] || pair.last); res }
klass.new params.merge(prefix_params)
end
end
end
|