Method: Graphiti::Serializer.inherited

Defined in:
lib/graphiti/serializer.rb

.inherited(klass) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/graphiti/serializer.rb', line 21

def self.inherited(klass)
  super
  klass.class_eval do
    extend JSONAPI::Serializable::Resource::ConditionalFields

    # See #requested_relationships
    def self.relationship(name, options = {}, &block)
      prev = Util::Hash.deep_dup(field_condition_blocks)
      super
      self.field_condition_blocks = prev
      _register_condition(relationship_condition_blocks, name, options)
    end

    # NB - avoid clobbering includes when sparse fieldset
    # https://github.com/jsonapi-rb/jsonapi-serializable/pull/102
    #
    # We also override this method to ensure attributes and relationships
    # have separate condition blocks. This way an attribute and
    # relationship can have the same name, and the attribute can be
    # conditional without affecting the relationship.
    def requested_relationships(fields)
      @_relationships.select do |k, _|
        _conditionally_included?(self.class.relationship_condition_blocks, k)
      end
    end
  end
end