Skip to content

Add support for relationship-level links and meta attributes. #1454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 51 additions & 94 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class JsonApi < Base
autoload :PaginationLinks
autoload :FragmentCache
autoload :Link
autoload :Association
autoload :ResourceIdentifier
autoload :Deserialization

# TODO: if we like this abstraction and other API objects to it,
Expand Down Expand Up @@ -52,12 +54,13 @@ def initialize(serializer, options = {})
def serializable_hash(options = nil)
options ||= {}

hash =
if serializer.respond_to?(:each)
serializable_hash_for_collection(options)
else
serializable_hash_for_single_resource
end
is_collection = serializer.respond_to?(:each)
serializers = is_collection ? serializer : [serializer]
primary_data, included = resource_objects_for(serializers)

hash = {}
hash[:data] = is_collection ? primary_data : primary_data[0]
hash[:included] = included if included.any?

ApiObjects::JsonApi.add!(hash)

Expand All @@ -66,6 +69,11 @@ def serializable_hash(options = nil)
hash[:links].update(instance_options[:links])
end

if is_collection && serializer.paginated?
hash[:links] ||= {}
hash[:links].update(pagination_links_for(serializer, options))
end

hash
end

Expand All @@ -80,61 +88,45 @@ def fragment_cache(cached_hash, non_cached_hash)

private

def serializable_hash_for_collection(options)
hash = { data: [] }
included = []
serializer.each do |s|
result = self.class.new(s, instance_options.merge(fieldset: fieldset)).serializable_hash(options)
hash[:data] << result[:data]
next unless result[:included]

included |= result[:included]
end

included.delete_if { |resource| hash[:data].include?(resource) }
hash[:included] = included if included.any?

if serializer.paginated?
hash[:links] ||= {}
hash[:links].update(pagination_links_for(serializer, options))
end
def resource_objects_for(serializers)
@primary = []
@included = []
@resource_identifiers = Set.new
serializers.each { |serializer| process_resource(serializer, true) }
serializers.each { |serializer| process_relationships(serializer, @include_tree) }

hash
[@primary, @included]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason for the instance variables here?

end

def serializable_hash_for_single_resource
primary_data = resource_object_for(serializer)

hash = { data: primary_data }

included = included_resources(@include_tree, [primary_data])
hash[:included] = included if included.any?

hash
end
def process_resource(serializer, primary)
resource_identifier = JsonApi::ResourceIdentifier.new(serializer).as_json
return false unless @resource_identifiers.add?(resource_identifier)

def resource_identifier_type_for(serializer)
return serializer._type if serializer._type
if ActiveModelSerializers.config.jsonapi_resource_type == :singular
serializer.object.class.model_name.singular
resource_object = resource_object_for(serializer)
if primary
@primary << resource_object
else
serializer.object.class.model_name.plural
@included << resource_object
end

true
end

def resource_identifier_id_for(serializer)
if serializer.respond_to?(:id)
serializer.id
else
serializer.object.id
def process_relationships(serializer, include_tree)
serializer.associations(include_tree).each do |association|
process_relationship(association.serializer, include_tree[association.key])
end
end

def resource_identifier_for(serializer)
type = resource_identifier_type_for(serializer)
id = resource_identifier_id_for(serializer)
def process_relationship(serializer, include_tree)
if serializer.respond_to?(:each)
serializer.each { |s| process_relationship(s, include_tree) }
return
end
return unless serializer && serializer.object
return unless process_resource(serializer, false)

{ id: id.to_s, type: type }
process_relationships(serializer, include_tree)
end

def attributes_for(serializer, fields)
Expand All @@ -143,15 +135,16 @@ def attributes_for(serializer, fields)

def resource_object_for(serializer)
resource_object = cache_check(serializer) do
resource_object = resource_identifier_for(serializer)
resource_object = JsonApi::ResourceIdentifier.new(serializer).as_json

requested_fields = fieldset && fieldset.fields_for(resource_object[:type])
attributes = attributes_for(serializer, requested_fields)
resource_object[:attributes] = attributes if attributes.any?
resource_object
end

relationships = relationships_for(serializer)
requested_associations = fieldset.fields_for(resource_object[:type]) || '*'
relationships = relationships_for(serializer, requested_associations)
resource_object[:relationships] = relationships if relationships.any?

links = links_for(serializer)
Expand All @@ -160,51 +153,15 @@ def resource_object_for(serializer)
resource_object
end

def relationship_value_for(serializer, options = {})
if serializer.respond_to?(:each)
serializer.map { |s| resource_identifier_for(s) }
else
if options[:virtual_value]
options[:virtual_value]
elsif serializer && serializer.object
resource_identifier_for(serializer)
end
end
end

def relationships_for(serializer)
resource_type = resource_identifier_type_for(serializer)
requested_associations = fieldset.fields_for(resource_type) || '*'
def relationships_for(serializer, requested_associations)
include_tree = IncludeTree.from_include_args(requested_associations)
serializer.associations(include_tree).each_with_object({}) do |association, hash|
hash[association.key] = { data: relationship_value_for(association.serializer, association.options) }
end
end

def included_resources(include_tree, primary_data)
included = []

serializer.associations(include_tree).each do |association|
add_included_resources_for(association.serializer, include_tree[association.key], primary_data, included)
end

included
end

def add_included_resources_for(serializer, include_tree, primary_data, included)
if serializer.respond_to?(:each)
serializer.each { |s| add_included_resources_for(s, include_tree, primary_data, included) }
else
return unless serializer && serializer.object

resource_object = resource_object_for(serializer)

return if included.include?(resource_object) || primary_data.include?(resource_object)
included.push(resource_object)

serializer.associations(include_tree).each do |association|
add_included_resources_for(association.serializer, include_tree[association.key], primary_data, included)
end
hash[association.key] = JsonApi::Association.new(serializer,
association.serializer,
association.options,
association.links,
association.meta)
.as_json
end
end

Expand Down
48 changes: 48 additions & 0 deletions lib/active_model/serializer/adapter/json_api/association.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module ActiveModel
class Serializer
module Adapter
class JsonApi
class Association
def initialize(parent_serializer, serializer, options, links, meta)
@object = parent_serializer.object
@scope = parent_serializer.scope

@options = options
@data = data_for(serializer, options)
@links = links
.map { |key, value| { key => Link.new(parent_serializer, value).as_json } }
.reduce({}, :merge)
@meta = meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : meta
end

def as_json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this an APIObject? If so, it should be Relationship.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bf4 should this be namespaced under module ApiObjects?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we follow that pattern, yes. It was always about how we feel about it making sense #1004 (comment) and #1482

hash = {}
hash[:data] = @data if @options[:include_data]
hash[:links] = @links if @links.any?
hash[:meta] = @meta if @meta

hash
end

protected

attr_reader :object, :scope

private

def data_for(serializer, options)
if serializer.respond_to?(:each)
serializer.map { |s| ResourceIdentifier.new(s).as_json }
else
if options[:virtual_value]
options[:virtual_value]
elsif serializer && serializer.object
ResourceIdentifier.new(serializer).as_json
end
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module ActiveModel
class Serializer
module Adapter
class JsonApi
class ResourceIdentifier
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ApiObject?

def initialize(serializer)
@id = id_for(serializer)
@type = type_for(serializer)
end

def as_json
{ id: @id.to_s, type: @type }
end

protected

attr_reader :object, :scope

private

def type_for(serializer)
return serializer._type if serializer._type
if ActiveModelSerializers.config.jsonapi_resource_type == :singular
serializer.object.class.model_name.singular
else
serializer.object.class.model_name.plural
end
end

def id_for(serializer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be serializer.read_attribute_for_serialization(:id)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be on the same page here, that would change the current behaviour, right? (see https://github.com/beauby/active_model_serializers/blob/master/lib/active_model/serializer/adapter/json_api.rb#L125)
And I guess you meant serializer.object.read_attribute_for_serialization(:id), right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, I did indeedy mean to call it on the serializer, but @groyoh thanks for pointing out my error that this was just a direct port of the method

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you're right, I didn't realized that read_attribute_for_serialization was now defined on the serializer.

if serializer.respond_to?(:id)
serializer.id
else
serializer.object.id
end
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/active_model/serializer/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Serializer
# @example
# Association.new(:comments, CommentSummarySerializer)
#
Association = Struct.new(:name, :serializer, :options) do
Association = Struct.new(:name, :serializer, :options, :links, :meta) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON API concerns creeping into the serializer.. hmm

# @return [Symbol]
#
def key
Expand Down
39 changes: 38 additions & 1 deletion lib/active_model/serializer/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,38 @@ class Serializer
# So you can inspect reflections in your Adapters.
#
class Reflection < Field
def initialize(*)
super
@_links = {}
@_include_data = true
end

def link(name, value = nil, &block)
@_links[name] = block || value
nil
end

def meta(value = nil, &block)
@_meta = block || value
nil
end

def include_data(value = true)
@_include_data = value
nil
end

def value(serializer)
@object = serializer.object
@scope = serializer.scope

if block
instance_eval(&block)
else
serializer.read_attribute_for_serialization(name)
end
end

# Build association. This method is used internally to
# build serializer's association by its reflection.
#
Expand All @@ -59,6 +91,7 @@ def build_association(subject, parent_serializer_options)
association_value = value(subject)
reflection_options = options.dup
serializer_class = subject.class.serializer_for(association_value, reflection_options)
reflection_options[:include_data] = @_include_data

if serializer_class
begin
Expand All @@ -73,9 +106,13 @@ def build_association(subject, parent_serializer_options)
reflection_options[:virtual_value] = association_value
end

Association.new(name, serializer, reflection_options)
Association.new(name, serializer, reflection_options, @_links, @_meta)
end

protected

attr_accessor :object, :scope

private

def serializer_options(subject, parent_serializer_options, reflection_options)
Expand Down
Loading