Module: JSONAPI::Serializable::Resource::ConditionalFields::DSL

Defined in:
lib/jsonapi/serializable/resource/conditional_fields.rb

Overview

DSL extensions for conditional fields.

Instance Method Summary collapse

Instance Method Details

#_register_condition(name, options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



54
55
56
57
58
59
60
61
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 54

def _register_condition(name, options)
  condition_blocks[name.to_sym] =
    if options.key?(:if)
      options[:if]
    elsif options.key?(:unless)
      proc { !instance_exec(&options[:unless]) }
    end
end

#attribute(name, options = {}, &block) ⇒ Object

Handle the ‘if` and `unless` options for attributes.

Examples:

attribute :email, if: -> { @current_user.admin? }


37
38
39
40
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 37

def attribute(name, options = {}, &block)
  super
  _register_condition(name, options)
end

#inherited(klass) ⇒ Object



27
28
29
30
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 27

def inherited(klass)
  super
  klass.condition_blocks = condition_blocks.dup
end

#relationship(name, options = {}, &block) ⇒ Object

Handle the ‘if` and `unless` options for relationships (has_one,

belongs_to, has_many).

Examples:

has_many :friends, unless: -> { @object.private_profile? }


48
49
50
51
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 48

def relationship(name, options = {}, &block)
  super
  _register_condition(name, options)
end