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
|
# File 'lib/as_json_representations/collection.rb', line 7
def self.included(base)
base.class_eval do
def as_json(options={})
subject = self
if respond_to?(:klass) && klass.respond_to?(:representations)
QUERY_METHODS.each do |method|
next unless respond_to? method
args = klass.representations.dig(options[:representation], method)
subject = subject.public_send(method, args) if args
end
end
return super if respond_to? :super
subject.map do |item|
item.respond_to?(:as_json) ? item.as_json(options) : item
end
end
end
end
|