Module: JsonRepresentations::Collection

Defined in:
lib/json_representations/collection.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
# File 'lib/json_representations/collection.rb', line 3

def self.included(base)
  base.class_eval do
    def representation(name=nil, options={})
      subject = self

      if respond_to?(:klass) && klass.respond_to?(:representations)
        # call supported methods of ActiveRecord::QueryMethods
        QUERY_METHODS.each do |method|
          next unless respond_to? method

          args = klass.representations.dig(name, method)

          # we need to reassign because ActiveRecord returns new object
          subject = subject.public_send(method, args) if args
        end
      end

      return super if respond_to? :super

      subject.map do |item|
        item.respond_to?(:representation) ? item.representation(name, options) : item
      end
    end
  end
end