Skip to content

Commit 2c41938

Browse files
author
Yohan Robert
committed
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
1 parent fe6d2da commit 2c41938

File tree

9 files changed

+372
-96
lines changed

9 files changed

+372
-96
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
## 0.10.x
22

33
Breaking changes:
4+
45
Features:
6+
- [#1504](https://github.com/rails-api/active_model_serializers/pull/1504) Adds the changes missing from #1454
7+
and add more tests for resource identifier and relationship objects. (@groyoh)
58
- [#1018](https://github.com/rails-api/active_model_serializers/pull/1018) Add more tests and docs for top-level links. (@leandrocp)
69
- [#1454](https://github.com/rails-api/active_model_serializers/pull/1454) Add support for
710
relationship-level links and meta attributes. (@beauby)
811
- [#1340](https://github.com/rails-api/active_model_serializers/pull/1340) Add support for resource-level meta. (@beauby)
12+
913
Fixes:
1014
- [#1501](https://github.com/rails-api/active_model_serializers/pull/1501) Adds tests for SerializableResource::use_adapter?,doc typos (@domitian)
1115
- [#1488](https://github.com/rails-api/active_model_serializers/pull/1488) Require ActiveSupport's string inflections (@nate00)
16+
1217
Misc:
1318

1419
### v0.10.0.rc4 (2016/01/27 11:00 +00:00)

lib/active_model/serializer/adapter/json_api.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ class JsonApi < Base
66
autoload :PaginationLinks
77
autoload :FragmentCache
88
autoload :Link
9-
autoload :Association
10-
autoload :ResourceIdentifier
119
autoload :Meta
1210
autoload :Deserialization
11+
require 'active_model/serializer/adapter/json_api/api_objects'
1312

1413
# TODO: if we like this abstraction and other API objects to it,
1514
# then extract to its own file and require it.
@@ -100,7 +99,7 @@ def resource_objects_for(serializers)
10099
end
101100

102101
def process_resource(serializer, primary)
103-
resource_identifier = JsonApi::ResourceIdentifier.new(serializer).as_json
102+
resource_identifier = ApiObjects::ResourceIdentifier.new(serializer).as_json
104103
return false unless @resource_identifiers.add?(resource_identifier)
105104

106105
resource_object = resource_object_for(serializer)
@@ -136,7 +135,7 @@ def attributes_for(serializer, fields)
136135

137136
def resource_object_for(serializer)
138137
resource_object = cache_check(serializer) do
139-
resource_object = JsonApi::ResourceIdentifier.new(serializer).as_json
138+
resource_object = ApiObjects::ResourceIdentifier.new(serializer).as_json
140139

141140
requested_fields = fieldset && fieldset.fields_for(resource_object[:type])
142141
attributes = attributes_for(serializer, requested_fields)
@@ -160,12 +159,13 @@ def resource_object_for(serializer)
160159
def relationships_for(serializer, requested_associations)
161160
include_tree = IncludeTree.from_include_args(requested_associations)
162161
serializer.associations(include_tree).each_with_object({}) do |association, hash|
163-
hash[association.key] = JsonApi::Association.new(serializer,
162+
hash[association.key] = ApiObjects::Relationship.new(
163+
serializer,
164164
association.serializer,
165165
association.options,
166166
association.links,
167-
association.meta)
168-
.as_json
167+
association.meta
168+
).as_json
169169
end
170170
end
171171

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module ActiveModel
2+
class Serializer
3+
module Adapter
4+
class JsonApi
5+
module ApiObjects
6+
extend ActiveSupport::Autoload
7+
autoload :Relationship
8+
autoload :ResourceIdentifier
9+
end
10+
end
11+
end
12+
end
13+
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
module ActiveModel
2+
class Serializer
3+
module Adapter
4+
class JsonApi
5+
module ApiObjects
6+
class Relationship
7+
def initialize(parent_serializer, serializer, options = {}, links = {}, meta = nil)
8+
@object = parent_serializer.object
9+
@scope = parent_serializer.scope
10+
11+
@options = options
12+
@data = data_for(serializer, options)
13+
@links = links.each_with_object({}) do |(key, value), hash|
14+
hash[key] = Link.new(parent_serializer, value).as_json
15+
end
16+
@meta = meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : meta
17+
end
18+
19+
def as_json
20+
hash = {}
21+
hash[:data] = data if options[:include_data]
22+
links = self.links
23+
hash[:links] = links if links.any?
24+
meta = self.meta
25+
hash[:meta] = meta if meta
26+
27+
hash
28+
end
29+
30+
protected
31+
32+
attr_reader :object, :scope, :data, :options, :links, :meta
33+
34+
private
35+
36+
def data_for(serializer, options)
37+
if serializer.respond_to?(:each)
38+
serializer.map { |s| ResourceIdentifier.new(s).as_json }
39+
else
40+
if options[:virtual_value]
41+
options[:virtual_value]
42+
elsif serializer && serializer.object
43+
ResourceIdentifier.new(serializer).as_json
44+
end
45+
end
46+
end
47+
end
48+
end
49+
end
50+
end
51+
end
52+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module ActiveModel
2+
class Serializer
3+
module Adapter
4+
class JsonApi
5+
module ApiObjects
6+
class ResourceIdentifier
7+
def initialize(serializer)
8+
@id = id_for(serializer)
9+
@type = type_for(serializer)
10+
end
11+
12+
def as_json
13+
{ id: id, type: type }
14+
end
15+
16+
protected
17+
18+
attr_reader :id, :type
19+
20+
private
21+
22+
def type_for(serializer)
23+
return serializer._type if serializer._type
24+
if ActiveModelSerializers.config.jsonapi_resource_type == :singular
25+
serializer.object.class.model_name.singular
26+
else
27+
serializer.object.class.model_name.plural
28+
end
29+
end
30+
31+
def id_for(serializer)
32+
serializer.read_attribute_for_serialization(:id).to_s
33+
end
34+
end
35+
end
36+
end
37+
end
38+
end
39+
end

lib/active_model/serializer/adapter/json_api/association.rb

Lines changed: 0 additions & 48 deletions
This file was deleted.

lib/active_model/serializer/adapter/json_api/resource_identifier.rb

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)