@@ -5,8 +5,6 @@ class ActiveModel::Serializer::Adapter::JsonApi < ActiveModel::Serializer::Adapt
5
5
6
6
def initialize ( serializer , options = { } )
7
7
super
8
- @hash = { data : [ ] }
9
-
10
8
@included = ActiveModel ::Serializer ::Utils . include_args_to_hash ( @options [ :include ] )
11
9
fields = options . delete ( :fields )
12
10
if fields
@@ -19,26 +17,10 @@ def initialize(serializer, options = {})
19
17
def serializable_hash ( options = nil )
20
18
options ||= { }
21
19
if serializer . respond_to? ( :each )
22
- serializer . each do |s |
23
- result = self . class . new ( s , @options . merge ( fieldset : @fieldset ) ) . serializable_hash ( options )
24
- @hash [ :data ] << result [ :data ]
25
-
26
- if result [ :included ]
27
- @hash [ :included ] ||= [ ]
28
- @hash [ :included ] |= result [ :included ]
29
- end
30
- end
31
-
32
- add_links ( options )
20
+ serializable_hash_for_collection ( serializer , options )
33
21
else
34
- primary_data = primary_data_for ( serializer , options )
35
- relationships = relationships_for ( serializer )
36
- included = included_for ( serializer )
37
- @hash [ :data ] = primary_data
38
- @hash [ :data ] [ :relationships ] = relationships if relationships . any?
39
- @hash [ :included ] = included if included . any?
22
+ serializable_hash_for_single_resource ( serializer , options )
40
23
end
41
- @hash
42
24
end
43
25
44
26
def fragment_cache ( cached_hash , non_cached_hash )
@@ -48,6 +30,37 @@ def fragment_cache(cached_hash, non_cached_hash)
48
30
49
31
private
50
32
33
+ def serializable_hash_for_collection ( serializer , options )
34
+ hash = { data : [ ] }
35
+ serializer . each do |s |
36
+ result = self . class . new ( s , @options . merge ( fieldset : @fieldset ) ) . serializable_hash ( options )
37
+ hash [ :data ] << result [ :data ]
38
+
39
+ if result [ :included ]
40
+ hash [ :included ] ||= [ ]
41
+ hash [ :included ] |= result [ :included ]
42
+ end
43
+ end
44
+
45
+ if serializer . paginated?
46
+ hash [ :links ] ||= { }
47
+ hash [ :links ] . update ( links_for ( serializer , options ) )
48
+ end
49
+
50
+ hash
51
+ end
52
+
53
+ def serializable_hash_for_single_resource ( serializer , options )
54
+ primary_data = primary_data_for ( serializer , options )
55
+ relationships = relationships_for ( serializer )
56
+ included = included_for ( serializer )
57
+ hash = { data : primary_data }
58
+ hash [ :data ] [ :relationships ] = relationships if relationships . any?
59
+ hash [ :included ] = included if included . any?
60
+
61
+ hash
62
+ end
63
+
51
64
def resource_identifier_type_for ( serializer )
52
65
if ActiveModel ::Serializer . config . jsonapi_resource_type == :singular
53
66
serializer . object . class . model_name . singular
@@ -139,20 +152,7 @@ def _included_for(serializer, includes)
139
152
end
140
153
end
141
154
142
- def add_links ( options )
143
- links = @hash . fetch ( :links ) { { } }
144
- collection = serializer . object
145
- @hash [ :links ] = add_pagination_links ( links , collection , options ) if paginated? ( collection )
146
- end
147
-
148
- def add_pagination_links ( links , resources , options )
149
- pagination_links = ActiveModel ::Serializer ::Adapter ::JsonApi ::PaginationLinks . new ( resources , options [ :context ] ) . serializable_hash ( options )
150
- links . update ( pagination_links )
151
- end
152
-
153
- def paginated? ( collection )
154
- collection . respond_to? ( :current_page ) &&
155
- collection . respond_to? ( :total_pages ) &&
156
- collection . respond_to? ( :size )
155
+ def links_for ( serializer , options )
156
+ JsonApi ::PaginationLinks . new ( serializer . object , options [ :context ] ) . serializable_hash ( options )
157
157
end
158
158
end
0 commit comments