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

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

Overview

Extension for handling conditional fields in serializable resources.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 24

def self.extended(klass)
  klass.class_eval do
    include InstanceMethods
    class << self
      attr_accessor :field_condition_blocks
      attr_accessor :link_condition_blocks
    end
    self.field_condition_blocks ||= {}
    self.link_condition_blocks  ||= {}
  end
end

.prepended(klass) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 15

def self.prepended(klass)
  warn "  DERPRECATION WARNING (called from \#{caller_locations(1...2).first}):\n  Prepending `\#{name}' is deprecated and will be removed in future releases. Use `Object#extend' instead.\n  EOT\n\n  klass.extend self\nend\n"

Instance Method Details

#_register_condition(condition_blocks, 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.



82
83
84
85
86
87
88
89
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 82

def _register_condition(condition_blocks, 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? }


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

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

#inherited(klass) ⇒ Object



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

def inherited(klass)
  super
  klass.field_condition_blocks = field_condition_blocks.dup
  klass.link_condition_blocks  = link_condition_blocks.dup
end

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

Examples:


link :self, if: -> { @object.render_self_link? } do
  "..."
end


70
71
72
73
# File 'lib/jsonapi/serializable/resource/conditional_fields.rb', line 70

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

#relationship(name, options = {}, &block) ⇒ Object Also known as: has_many, has_one, belongs_to

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

belongs_to, has_many).

Examples:

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


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

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