class ActiveModelSerializers::Adapter::JsonApi::Relationship
Attributes
association[R]
parent_serializer[R]
serializable_resource_options[R]
Public Class Methods
new(parent_serializer, serializable_resource_options, association)
click to toggle source
{jsonapi.org/format/#document-resource-object-related-resource-links Document Resource Object
Related Resource Links} {jsonapi.org/format/#document-links Document Links} {jsonapi.org/format/#document-resource-object-linkage Document Resource Relationship
Linkage} {jsonapi.org/format/#document-meta Document Meta}
# File lib/active_model_serializers/adapter/json_api/relationship.rb, line 11 def initialize(parent_serializer, serializable_resource_options, association) @parent_serializer = parent_serializer @association = association @serializable_resource_options = serializable_resource_options end
Public Instance Methods
as_json()
click to toggle source
# File lib/active_model_serializers/adapter/json_api/relationship.rb, line 17 def as_json hash = {} hash[:data] = data_for(association) if association.include_data? links = links_for(association) hash[:links] = links if links.any? meta = meta_for(association) hash[:meta] = meta if meta hash[:meta] = {} if hash.empty? hash end
Private Instance Methods
belongs_to_id_on_self?(association)
click to toggle source
# File lib/active_model_serializers/adapter/json_api/relationship.rb, line 98 def belongs_to_id_on_self?(association) parent_serializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship && association.belongs_to? && parent_serializer.object.respond_to?(association.reflection.foreign_key) end
data_for(association)
click to toggle source
TODO(BF): Avoid db hit on belong_to_ releationship by using foreign_key on self
# File lib/active_model_serializers/adapter/json_api/relationship.rb, line 39 def data_for(association) if association.collection? data_for_many(association) else data_for_one(association) end end
data_for_many(association)
click to toggle source
# File lib/active_model_serializers/adapter/json_api/relationship.rb, line 72 def data_for_many(association) # TODO(BF): Process relationship without evaluating lazy_association collection_serializer = association.lazy_association.serializer if collection_serializer.respond_to?(:each) collection_serializer.map do |serializer| ResourceIdentifier.new(serializer, serializable_resource_options).as_json end elsif (virtual_value = association.virtual_value) virtual_value else [] end end
data_for_one(association)
click to toggle source
# File lib/active_model_serializers/adapter/json_api/relationship.rb, line 47 def data_for_one(association) if belongs_to_id_on_self?(association) id = parent_serializer.read_attribute_for_serialization(association.reflection.foreign_key) type = if association.polymorphic? # We can't infer resource type for polymorphic relationships from the serializer. # We can ONLY know a polymorphic resource type by inspecting each resource. association.lazy_association.serializer.json_key else association.reflection.type.to_s end ResourceIdentifier.for_type_with_id(type, id, serializable_resource_options) else # TODO(BF): Process relationship without evaluating lazy_association serializer = association.lazy_association.serializer if (virtual_value = association.virtual_value) virtual_value elsif serializer && association.object ResourceIdentifier.new(serializer, serializable_resource_options).as_json else nil end end end
links_for(association)
click to toggle source
# File lib/active_model_serializers/adapter/json_api/relationship.rb, line 86 def links_for(association) association.links.each_with_object({}) do |(key, value), hash| result = Link.new(parent_serializer, value).as_json hash[key] = result if result end end
meta_for(association)
click to toggle source
# File lib/active_model_serializers/adapter/json_api/relationship.rb, line 93 def meta_for(association) meta = association.meta meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : meta end