@@ -5,98 +5,10 @@ class JsonApi < Base
5
5
extend ActiveSupport ::Autoload
6
6
autoload :PaginationLinks
7
7
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'
100
12
101
13
def initialize ( serializer , options = { } )
102
14
super
@@ -119,7 +31,7 @@ def serializable_hash(options = nil)
119
31
120
32
hash [ :included ] = included . map ( &:to_h ) if included . any?
121
33
122
- ApiObjects :: JsonApi . add! ( hash )
34
+ JsonApiObject . add! ( hash )
123
35
124
36
if is_collection && serializer . paginated?
125
37
hash [ :links ] ||= { }
@@ -158,7 +70,7 @@ def resource_objects_for(serializer)
158
70
end
159
71
160
72
# Recursively build all requested resource objects and flag them as primary when applicable.
161
- # @return [Hash<ApiObjects:: ResourceIdentifier, Hash>]
73
+ # @return [Hash<ResourceIdentifier, Hash>]
162
74
# Hash of hashes each describing a resource object and whether it is primary or included.
163
75
#
164
76
# @api private
@@ -170,13 +82,13 @@ def _resource_objects_for(serializer, include_tree, is_primary, hashes = {})
170
82
171
83
return hashes unless serializer && serializer . object
172
84
173
- resource_identifier = ApiObjects :: ResourceIdentifier . from_serializer ( serializer )
85
+ resource_identifier = ResourceIdentifier . from_serializer ( serializer )
174
86
if hashes [ resource_identifier ]
175
87
hashes [ resource_identifier ] [ :is_primary ] ||= is_primary
176
88
return hashes
177
89
end
178
90
179
- resource_object = ApiObjects :: Resource . new (
91
+ resource_object = Resource . new (
180
92
resource_identifier ,
181
93
attributes_for ( serializer ) ,
182
94
relationships_for ( serializer ) )
@@ -196,7 +108,7 @@ def _resource_objects_for(serializer, include_tree, is_primary, hashes = {})
196
108
def attributes_for ( serializer )
197
109
hash = cache_check ( serializer ) do
198
110
attributes = serializer . attributes . except ( :id )
199
- resource_type = ApiObjects :: ResourceIdentifier . type_for ( serializer )
111
+ resource_type = ResourceIdentifier . type_for ( serializer )
200
112
requested_fields = @fieldset . fields_for ( resource_type )
201
113
attributes . slice! ( *requested_fields ) if requested_fields
202
114
@@ -214,26 +126,26 @@ def attributes_for(serializer)
214
126
# @api private
215
127
def linkage_for ( serializer , options = { } )
216
128
if serializer . respond_to? ( :each )
217
- serializer . map { |s | ApiObjects :: ResourceIdentifier . from_serializer ( s ) }
129
+ serializer . map { |s | ResourceIdentifier . from_serializer ( s ) }
218
130
else
219
131
if options [ :virtual_value ]
220
132
options [ :virtual_value ]
221
133
elsif serializer && serializer . object
222
- ApiObjects :: ResourceIdentifier . from_serializer ( serializer )
134
+ ResourceIdentifier . from_serializer ( serializer )
223
135
end
224
136
end
225
137
end
226
138
227
139
# Get resource relationships.
228
- # @return [Hash<String, ApiObjects:: Relationship>] relationships
140
+ # @return [Hash<String, Relationship>] relationships
229
141
#
230
142
# @api private
231
143
def relationships_for ( serializer )
232
- resource_type = ApiObjects :: ResourceIdentifier . type_for ( serializer )
144
+ resource_type = ResourceIdentifier . type_for ( serializer )
233
145
requested_associations = @fieldset . fields_for ( resource_type ) || '*'
234
146
include_tree = IncludeTree . from_include_args ( requested_associations )
235
147
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 ) )
237
149
end
238
150
end
239
151
0 commit comments