forked from runtastic/active_model_serializers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR rails-api#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
- Loading branch information
Yohan Robert
committed
Feb 10, 2016
1 parent
061f1c0
commit a12615e
Showing
8 changed files
with
370 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
lib/active_model/serializer/adapter/json_api/api_objects.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
52 changes: 52 additions & 0 deletions
52
lib/active_model/serializer/adapter/json_api/api_objects/relationship.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
39 changes: 39 additions & 0 deletions
39
lib/active_model/serializer/adapter/json_api/api_objects/resource_identifier.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
48 changes: 0 additions & 48 deletions
48
lib/active_model/serializer/adapter/json_api/association.rb
This file was deleted.
Oops, something went wrong.
41 changes: 0 additions & 41 deletions
41
lib/active_model/serializer/adapter/json_api/resource_identifier.rb
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.