class Graphiti::Serializer
Public Class Methods
Source
# 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
Calls superclass method
Source
# File lib/graphiti/serializer.rb, line 27 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
Calls superclass method
Public Instance Methods
Source
# File lib/graphiti/serializer.rb, line 66 def as_jsonapi(kwargs = {}) super(**kwargs).tap do |hash| strip_relationships!(hash) if strip_relationships? add_links!(hash) end end
Calls superclass method
Source
# File lib/graphiti/serializer.rb, line 49 def cursor starting_offset = 0 page_param = @proxy.query.pagination if (page_number = page_param[:number]) page_size = page_param[:size] || @resource.default_page_size starting_offset = (page_number - 1) * page_size end if (cursor = page_param[:after]) starting_offset = cursor[:offset] end current_offset = @object.instance_variable_get(:@__graphiti_index) offset = starting_offset + current_offset + 1 # (+ 1 b/c o-base index) Base64.encode64({offset: offset}.to_json).chomp end
Source
# File lib/graphiti/serializer.rb, line 74 def method_missing(id, ...) if @resource.respond_to?(id, true) @resource.send(id, ...) else super end end
Allow access to resource methods
Calls superclass method
Source
# File lib/graphiti/serializer.rb, line 41 def requested_relationships(fields) @_relationships.select do |k, _| _conditionally_included?(self.class.relationship_condition_blocks, k) end end
NB - avoid clobbering includes when sparse fieldset 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.
Source
# File lib/graphiti/serializer.rb, line 82 def respond_to_missing?(method_name, include_private = true) @resource.respond_to?(method_name, include_private) || super end
Calls superclass method
Private Instance Methods
Source
# File lib/graphiti/serializer.rb, line 88 def add_links!(hash) return unless @resource.respond_to?(:links?) hash[:links] = @resource.links(@object) if @resource.links? end
Source
# File lib/graphiti/serializer.rb, line 94 def strip_relationships!(hash) hash[:relationships]&.select! do |name, payload| payload.key?(:data) end end
Source
# File lib/graphiti/serializer.rb, line 100 def strip_relationships? return false unless Graphiti.config.links_on_demand params = Graphiti.context[:object]&.params || {} [false, nil, "false"].include?(params[:links]) end