Module: Representable::Expandable

Defined in:
lib/representable/expandable.rb,
lib/representable/expandable/version.rb

Constant Summary collapse

VERSION =
'1.0.0'

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

def self.included(base)
  base.class_eval do
    def self.collection(name, options = {}, &block)
      add_expandable(name, options)
      super
    end

    def self.property(name, options = {}, &block)
      add_expandable(name, options)
      super
    end

    def self.add_expandable(name, options = {})
      if options.delete :expandable
        # if collection already had if, and it with expandable if condition
        existing_if = options[:if] || ->(_opts) { true }
        options[:if] = ->(opts) { opts[:expand] && opts[:expand].include?(name.to_s) && existing_if.call(opts) }
      end
    end
  end
end