File tree Expand file tree Collapse file tree 5 files changed +22
-29
lines changed
lib/active_model_serializers/adapter Expand file tree Collapse file tree 5 files changed +22
-29
lines changed Original file line number Diff line number Diff line change @@ -48,11 +48,6 @@ def relationship_value_for(association, options)
48
48
Attributes . new ( association . serializer , opts ) . serializable_hash ( options )
49
49
end
50
50
51
- # no-op: Attributes adapter does not include meta data, because it does not support root.
52
- def include_meta ( json )
53
- json
54
- end
55
-
56
51
# Set @cached_attributes
57
52
def cache_attributes
58
53
return if @cached_attributes . present?
Original file line number Diff line number Diff line change @@ -26,9 +26,7 @@ def serializable_hash(_options = nil)
26
26
end
27
27
28
28
def as_json ( options = nil )
29
- hash = serializable_hash ( options )
30
- include_meta ( hash )
31
- hash
29
+ serializable_hash ( options )
32
30
end
33
31
34
32
def fragment_cache ( cached_hash , non_cached_hash )
@@ -49,23 +47,10 @@ def serialization_options(options)
49
47
options ||= { } # rubocop:disable Lint/UselessAssignment
50
48
end
51
49
52
- def meta
53
- instance_options . fetch ( :meta , nil )
54
- end
55
-
56
- def meta_key
57
- instance_options . fetch ( :meta_key , 'meta' . freeze )
58
- end
59
-
60
50
def root
61
51
serializer . json_key . to_sym if serializer . json_key
62
52
end
63
53
64
- def include_meta ( json )
65
- json [ meta_key ] = meta unless meta . blank?
66
- json
67
- end
68
-
69
54
class << self
70
55
# Sets the default transform for the adapter.
71
56
#
Original file line number Diff line number Diff line change @@ -4,8 +4,18 @@ class Json < Base
4
4
def serializable_hash ( options = nil )
5
5
options = serialization_options ( options )
6
6
serialized_hash = { root => Attributes . new ( serializer , instance_options ) . serializable_hash ( options ) }
7
+ serialized_hash [ meta_key ] = meta unless meta . blank?
8
+
7
9
self . class . transform_key_casing! ( serialized_hash , instance_options )
8
10
end
11
+
12
+ def meta
13
+ instance_options . fetch ( :meta , nil )
14
+ end
15
+
16
+ def meta_key
17
+ instance_options . fetch ( :meta_key , 'meta' . freeze )
18
+ end
9
19
end
10
20
end
11
21
end
Original file line number Diff line number Diff line change @@ -67,6 +67,7 @@ def serializable_hash(*)
67
67
# links: toplevel_links,
68
68
# jsonapi: toplevel_jsonapi
69
69
# }.reject! {|_,v| v.nil? }
70
+ # rubocop:disable Metrics/CyclomaticComplexity
70
71
def success_document
71
72
is_collection = serializer . respond_to? ( :each )
72
73
serializers = is_collection ? serializer : [ serializer ]
@@ -130,8 +131,11 @@ def success_document
130
131
hash [ :links ] . update ( pagination_links_for ( serializer ) )
131
132
end
132
133
134
+ hash [ :meta ] = instance_options [ :meta ] if instance_options [ :meta ] . is_a? ( Hash )
135
+
133
136
hash
134
137
end
138
+ # rubocop:enable Metrics/CyclomaticComplexity
135
139
136
140
# {http://jsonapi.org/format/#errors JSON API Errors}
137
141
# TODO: look into caching
Original file line number Diff line number Diff line change @@ -92,7 +92,7 @@ def test_meta_key_is_used
92
92
assert_equal ( expected , actual )
93
93
end
94
94
95
- def test_meta_key_is_used_with_json_api
95
+ def test_meta_key_is_not_used_with_json_api
96
96
actual = ActiveModelSerializers ::SerializableResource . new (
97
97
@blog ,
98
98
adapter : :json_api ,
@@ -105,25 +105,25 @@ def test_meta_key_is_used_with_json_api
105
105
type : 'blogs' ,
106
106
attributes : { title : 'AMS Hints' }
107
107
} ,
108
- 'haha_meta' => { total : 10 }
108
+ meta : { total : 10 }
109
109
}
110
110
assert_equal ( expected , actual )
111
111
end
112
112
113
- def test_meta_key_is_not_present_when_blank_object_with_json_api
113
+ def test_meta_key_is_present_when_empty_hash_with_json_api
114
114
actual = ActiveModelSerializers ::SerializableResource . new (
115
115
@blog ,
116
116
adapter : :json_api ,
117
117
serializer : AlternateBlogSerializer ,
118
- meta : { } ,
119
- meta_key : 'haha_meta'
118
+ meta : { }
120
119
) . as_json
121
120
expected = {
122
121
data : {
123
122
id : '1' ,
124
123
type : 'blogs' ,
125
124
attributes : { title : 'AMS Hints' }
126
- }
125
+ } ,
126
+ meta : { }
127
127
}
128
128
assert_equal ( expected , actual )
129
129
end
@@ -133,8 +133,7 @@ def test_meta_key_is_not_present_when_empty_string_with_json_api
133
133
@blog ,
134
134
adapter : :json_api ,
135
135
serializer : AlternateBlogSerializer ,
136
- meta : '' ,
137
- meta_key : 'haha_meta'
136
+ meta : ''
138
137
) . as_json
139
138
expected = {
140
139
data : {
You can’t perform that action at this time.
0 commit comments