Skip to content

Commit 6b649b4

Browse files
committed
Extract modules/classes to their own files.
1 parent 84104bd commit 6b649b4

File tree

5 files changed

+130
-102
lines changed

5 files changed

+130
-102
lines changed

lib/active_model/serializer/adapter/json_api.rb

Lines changed: 14 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -5,98 +5,10 @@ class JsonApi < Base
55
extend ActiveSupport::Autoload
66
autoload :PaginationLinks
77
autoload :FragmentCache
8-
9-
# TODO: if we like this abstraction and other API objects to it,
10-
# then extract to its own file and require it.
11-
module ApiObjects
12-
module JsonApi
13-
ActiveModel::Serializer.config.jsonapi_version = '1.0'
14-
ActiveModel::Serializer.config.jsonapi_toplevel_meta = {}
15-
# Make JSON API top-level jsonapi member opt-in
16-
# ref: http://jsonapi.org/format/#document-top-level
17-
ActiveModel::Serializer.config.jsonapi_include_toplevel_object = false
18-
19-
module_function
20-
21-
def add!(hash)
22-
hash.merge!(object) if include_object?
23-
end
24-
25-
def include_object?
26-
ActiveModel::Serializer.config.jsonapi_include_toplevel_object
27-
end
28-
29-
# TODO: see if we can cache this
30-
def object
31-
object = {
32-
jsonapi: {
33-
version: ActiveModel::Serializer.config.jsonapi_version,
34-
meta: ActiveModel::Serializer.config.jsonapi_toplevel_meta
35-
}
36-
}
37-
object[:jsonapi].reject! { |_, v| v.blank? }
38-
39-
object
40-
end
41-
end
42-
43-
ResourceIdentifier = Struct.new(:id, :type) do
44-
def self.type_for(serializer)
45-
return serializer._type if serializer._type
46-
if ActiveModel::Serializer.config.jsonapi_resource_type == :singular
47-
serializer.object.class.model_name.singular
48-
else
49-
serializer.object.class.model_name.plural
50-
end
51-
end
52-
53-
def self.id_for(serializer)
54-
if serializer.respond_to?(:id)
55-
serializer.id
56-
else
57-
serializer.object.id
58-
end
59-
end
60-
61-
def self.from_serializer(serializer)
62-
new(id_for(serializer), type_for(serializer))
63-
end
64-
65-
def to_h
66-
{ id: id.to_s, type: type }
67-
end
68-
end
69-
70-
Resource = Struct.new(:identifier, :attributes, :relationships) do
71-
def to_h
72-
hash = identifier.to_h
73-
hash[:attributes] = attributes if attributes.any?
74-
hash[:relationships] = Hash[relationships.map { |k, v| [k, v.to_h] }] if relationships.any?
75-
76-
hash
77-
end
78-
end
79-
80-
class Relationship
81-
# NOTE(beauby): Currently only `data` is used.
82-
attr_accessor :data, :meta, :links
83-
84-
def initialize(hash = {})
85-
hash.each { |k, v| send("#{k}=", v) }
86-
end
87-
88-
def to_h
89-
data_hash =
90-
if data.is_a?(Array)
91-
data.map { |ri| ri.respond_to?(:to_h) ? ri.to_h : ri }
92-
elsif data
93-
data.respond_to?(:to_h) ? data.to_h : data
94-
end
95-
96-
{ data: data_hash }
97-
end
98-
end
99-
end
8+
autoload :Resource
9+
autoload :ResourceIdentifier
10+
autoload :Relationship
11+
require 'active_model/serializer/adapter/json_api/json_api_object'
10012

10113
def initialize(serializer, options = {})
10214
super
@@ -119,7 +31,7 @@ def serializable_hash(options = nil)
11931

12032
hash[:included] = included.map(&:to_h) if included.any?
12133

122-
ApiObjects::JsonApi.add!(hash)
34+
JsonApiObject.add!(hash)
12335

12436
if is_collection && serializer.paginated?
12537
hash[:links] ||= {}
@@ -158,7 +70,7 @@ def resource_objects_for(serializer)
15870
end
15971

16072
# Recursively build all requested resource objects and flag them as primary when applicable.
161-
# @return [Hash<ApiObjects::ResourceIdentifier, Hash>]
73+
# @return [Hash<ResourceIdentifier, Hash>]
16274
# Hash of hashes each describing a resource object and whether it is primary or included.
16375
#
16476
# @api private
@@ -170,13 +82,13 @@ def _resource_objects_for(serializer, include_tree, is_primary, hashes = {})
17082

17183
return hashes unless serializer && serializer.object
17284

173-
resource_identifier = ApiObjects::ResourceIdentifier.from_serializer(serializer)
85+
resource_identifier = ResourceIdentifier.from_serializer(serializer)
17486
if hashes[resource_identifier]
17587
hashes[resource_identifier][:is_primary] ||= is_primary
17688
return hashes
17789
end
17890

179-
resource_object = ApiObjects::Resource.new(
91+
resource_object = Resource.new(
18092
resource_identifier,
18193
attributes_for(serializer),
18294
relationships_for(serializer))
@@ -196,7 +108,7 @@ def _resource_objects_for(serializer, include_tree, is_primary, hashes = {})
196108
def attributes_for(serializer)
197109
hash = cache_check(serializer) do
198110
attributes = serializer.attributes.except(:id)
199-
resource_type = ApiObjects::ResourceIdentifier.type_for(serializer)
111+
resource_type = ResourceIdentifier.type_for(serializer)
200112
requested_fields = @fieldset.fields_for(resource_type)
201113
attributes.slice!(*requested_fields) if requested_fields
202114

@@ -214,26 +126,26 @@ def attributes_for(serializer)
214126
# @api private
215127
def linkage_for(serializer, options = {})
216128
if serializer.respond_to?(:each)
217-
serializer.map { |s| ApiObjects::ResourceIdentifier.from_serializer(s) }
129+
serializer.map { |s| ResourceIdentifier.from_serializer(s) }
218130
else
219131
if options[:virtual_value]
220132
options[:virtual_value]
221133
elsif serializer && serializer.object
222-
ApiObjects::ResourceIdentifier.from_serializer(serializer)
134+
ResourceIdentifier.from_serializer(serializer)
223135
end
224136
end
225137
end
226138

227139
# Get resource relationships.
228-
# @return [Hash<String, ApiObjects::Relationship>] relationships
140+
# @return [Hash<String, Relationship>] relationships
229141
#
230142
# @api private
231143
def relationships_for(serializer)
232-
resource_type = ApiObjects::ResourceIdentifier.type_for(serializer)
144+
resource_type = ResourceIdentifier.type_for(serializer)
233145
requested_associations = @fieldset.fields_for(resource_type) || '*'
234146
include_tree = IncludeTree.from_include_args(requested_associations)
235147
serializer.associations(include_tree).each_with_object({}) do |association, hash|
236-
hash[association.key] = ApiObjects::Relationship.new(data: linkage_for(association.serializer, association.options))
148+
hash[association.key] = Relationship.new(data: linkage_for(association.serializer, association.options))
237149
end
238150
end
239151

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module ActiveModel
2+
class Serializer
3+
module Adapter
4+
class JsonApi
5+
module JsonApiObject
6+
ActiveModel::Serializer.config.jsonapi_version = '1.0'
7+
ActiveModel::Serializer.config.jsonapi_toplevel_meta = {}
8+
# Make JSON API top-level jsonapi member opt-in
9+
# ref: http://jsonapi.org/format/#document-top-level
10+
ActiveModel::Serializer.config.jsonapi_include_toplevel_object = false
11+
12+
module_function
13+
14+
def add!(hash)
15+
hash.merge!(object) if include_object?
16+
end
17+
18+
def include_object?
19+
ActiveModel::Serializer.config.jsonapi_include_toplevel_object
20+
end
21+
22+
# TODO: see if we can cache this
23+
def object
24+
object = {
25+
jsonapi: {
26+
version: ActiveModel::Serializer.config.jsonapi_version,
27+
meta: ActiveModel::Serializer.config.jsonapi_toplevel_meta
28+
}
29+
}
30+
object[:jsonapi].reject! { |_, v| v.blank? }
31+
32+
object
33+
end
34+
end
35+
end
36+
end
37+
end
38+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module ActiveModel
2+
class Serializer
3+
module Adapter
4+
class JsonApi
5+
class Relationship
6+
# NOTE(beauby): Currently only `data` is used.
7+
attr_accessor :data, :meta, :links
8+
9+
def initialize(hash = {})
10+
hash.each { |k, v| send("#{k}=", v) }
11+
end
12+
13+
def to_h
14+
data_hash =
15+
if data.is_a?(Array)
16+
data.map { |ri| ri.respond_to?(:to_h) ? ri.to_h : ri }
17+
elsif data
18+
data.respond_to?(:to_h) ? data.to_h : data
19+
end
20+
21+
{ data: data_hash }
22+
end
23+
end
24+
end
25+
end
26+
end
27+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module ActiveModel
2+
class Serializer
3+
module Adapter
4+
class JsonApi
5+
Resource = Struct.new(:identifier, :attributes, :relationships) do
6+
def to_h
7+
hash = identifier.to_h
8+
hash[:attributes] = attributes if attributes.any?
9+
hash[:relationships] = Hash[relationships.map { |k, v| [k, v.to_h] }] if relationships.any?
10+
11+
hash
12+
end
13+
end
14+
end
15+
end
16+
end
17+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module ActiveModel
2+
class Serializer
3+
module Adapter
4+
class JsonApi
5+
ResourceIdentifier = Struct.new(:id, :type) do
6+
def self.type_for(serializer)
7+
return serializer._type if serializer._type
8+
if ActiveModel::Serializer.config.jsonapi_resource_type == :singular
9+
serializer.object.class.model_name.singular
10+
else
11+
serializer.object.class.model_name.plural
12+
end
13+
end
14+
15+
def self.id_for(serializer)
16+
if serializer.respond_to?(:id)
17+
serializer.id
18+
else
19+
serializer.object.id
20+
end
21+
end
22+
23+
def self.from_serializer(serializer)
24+
new(id_for(serializer), type_for(serializer))
25+
end
26+
27+
def to_h
28+
{ id: id.to_s, type: type }
29+
end
30+
end
31+
end
32+
end
33+
end
34+
end

0 commit comments

Comments
 (0)