Follow up to #1454

PR #1454 was merged with some missing fixes. All these fixes are
addressed by this commit:
- Rename ActiveModel::Serializer::Adapter::JsonApi::Association to
ActiveModel::Serializer::Adapter::JsonApi::Relationship
- Move ActiveModel::Serializer::Adapter:: JsonApi::Relationship and
ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier to
ActiveModel::Serializer::Adapter::JsonApi::ApiObjects module
- Add unit test for
ActiveModel::Serializer::Adapter::JsonApi::Relationship
- Add unit test for
ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier
This commit is contained in:
Yohan Robert
2016-02-10 01:02:48 +01:00
parent fe6d2da46f
commit 2c4193851b
9 changed files with 372 additions and 96 deletions

View File

@@ -6,10 +6,9 @@ module ActiveModel
autoload :PaginationLinks
autoload :FragmentCache
autoload :Link
autoload :Association
autoload :ResourceIdentifier
autoload :Meta
autoload :Deserialization
require 'active_model/serializer/adapter/json_api/api_objects'
# TODO: if we like this abstraction and other API objects to it,
# then extract to its own file and require it.
@@ -100,7 +99,7 @@ module ActiveModel
end
def process_resource(serializer, primary)
resource_identifier = JsonApi::ResourceIdentifier.new(serializer).as_json
resource_identifier = ApiObjects::ResourceIdentifier.new(serializer).as_json
return false unless @resource_identifiers.add?(resource_identifier)
resource_object = resource_object_for(serializer)
@@ -136,7 +135,7 @@ module ActiveModel
def resource_object_for(serializer)
resource_object = cache_check(serializer) do
resource_object = JsonApi::ResourceIdentifier.new(serializer).as_json
resource_object = ApiObjects::ResourceIdentifier.new(serializer).as_json
requested_fields = fieldset && fieldset.fields_for(resource_object[:type])
attributes = attributes_for(serializer, requested_fields)
@@ -160,12 +159,13 @@ module ActiveModel
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] = JsonApi::Association.new(serializer,
hash[association.key] = ApiObjects::Relationship.new(
serializer,
association.serializer,
association.options,
association.links,
association.meta)
.as_json
association.meta
).as_json
end
end

View File

@@ -0,0 +1,13 @@
module ActiveModel
class Serializer
module Adapter
class JsonApi
module ApiObjects
extend ActiveSupport::Autoload
autoload :Relationship
autoload :ResourceIdentifier
end
end
end
end
end

View File

@@ -0,0 +1,52 @@
module ActiveModel
class Serializer
module Adapter
class JsonApi
module ApiObjects
class Relationship
def initialize(parent_serializer, serializer, options = {}, links = {}, meta = nil)
@object = parent_serializer.object
@scope = parent_serializer.scope
@options = options
@data = data_for(serializer, options)
@links = links.each_with_object({}) do |(key, value), hash|
hash[key] = Link.new(parent_serializer, value).as_json
end
@meta = meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : meta
end
def as_json
hash = {}
hash[:data] = data if options[:include_data]
links = self.links
hash[:links] = links if links.any?
meta = self.meta
hash[:meta] = meta if meta
hash
end
protected
attr_reader :object, :scope, :data, :options, :links, :meta
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
end

View File

@@ -0,0 +1,39 @@
module ActiveModel
class Serializer
module Adapter
class JsonApi
module ApiObjects
class ResourceIdentifier
def initialize(serializer)
@id = id_for(serializer)
@type = type_for(serializer)
end
def as_json
{ id: id, type: type }
end
protected
attr_reader :id, :type
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)
serializer.read_attribute_for_serialization(:id).to_s
end
end
end
end
end
end
end

View File

@@ -1,48 +0,0 @@
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
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

View File

@@ -1,41 +0,0 @@
module ActiveModel
class Serializer
module Adapter
class JsonApi
class ResourceIdentifier
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)
if serializer.respond_to?(:id)
serializer.id
else
serializer.object.id
end
end
end
end
end
end
end