Skip to content

Commit b4e2ac3

Browse files
committed
Merge pull request #1695 from beauby/meta-madness
Meta no longer handled in Base adapter.
2 parents f0fa743 + e804d37 commit b4e2ac3

File tree

5 files changed

+22
-29
lines changed

5 files changed

+22
-29
lines changed

lib/active_model_serializers/adapter/attributes.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ def relationship_value_for(association, options)
4848
Attributes.new(association.serializer, opts).serializable_hash(options)
4949
end
5050

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-
5651
# Set @cached_attributes
5752
def cache_attributes
5853
return if @cached_attributes.present?

lib/active_model_serializers/adapter/base.rb

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ def serializable_hash(_options = nil)
2626
end
2727

2828
def as_json(options = nil)
29-
hash = serializable_hash(options)
30-
include_meta(hash)
31-
hash
29+
serializable_hash(options)
3230
end
3331

3432
def fragment_cache(cached_hash, non_cached_hash)
@@ -49,23 +47,10 @@ def serialization_options(options)
4947
options ||= {} # rubocop:disable Lint/UselessAssignment
5048
end
5149

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-
6050
def root
6151
serializer.json_key.to_sym if serializer.json_key
6252
end
6353

64-
def include_meta(json)
65-
json[meta_key] = meta unless meta.blank?
66-
json
67-
end
68-
6954
class << self
7055
# Sets the default transform for the adapter.
7156
#

lib/active_model_serializers/adapter/json.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,18 @@ class Json < Base
44
def serializable_hash(options = nil)
55
options = serialization_options(options)
66
serialized_hash = { root => Attributes.new(serializer, instance_options).serializable_hash(options) }
7+
serialized_hash[meta_key] = meta unless meta.blank?
8+
79
self.class.transform_key_casing!(serialized_hash, instance_options)
810
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
919
end
1020
end
1121
end

lib/active_model_serializers/adapter/json_api.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def serializable_hash(*)
6767
# links: toplevel_links,
6868
# jsonapi: toplevel_jsonapi
6969
# }.reject! {|_,v| v.nil? }
70+
# rubocop:disable Metrics/CyclomaticComplexity
7071
def success_document
7172
is_collection = serializer.respond_to?(:each)
7273
serializers = is_collection ? serializer : [serializer]
@@ -130,8 +131,11 @@ def success_document
130131
hash[:links].update(pagination_links_for(serializer))
131132
end
132133

134+
hash[:meta] = instance_options[:meta] if instance_options[:meta].is_a?(Hash)
135+
133136
hash
134137
end
138+
# rubocop:enable Metrics/CyclomaticComplexity
135139

136140
# {http://jsonapi.org/format/#errors JSON API Errors}
137141
# TODO: look into caching

test/serializers/meta_test.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def test_meta_key_is_used
9292
assert_equal(expected, actual)
9393
end
9494

95-
def test_meta_key_is_used_with_json_api
95+
def test_meta_key_is_not_used_with_json_api
9696
actual = ActiveModelSerializers::SerializableResource.new(
9797
@blog,
9898
adapter: :json_api,
@@ -105,25 +105,25 @@ def test_meta_key_is_used_with_json_api
105105
type: 'blogs',
106106
attributes: { title: 'AMS Hints' }
107107
},
108-
'haha_meta' => { total: 10 }
108+
meta: { total: 10 }
109109
}
110110
assert_equal(expected, actual)
111111
end
112112

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
114114
actual = ActiveModelSerializers::SerializableResource.new(
115115
@blog,
116116
adapter: :json_api,
117117
serializer: AlternateBlogSerializer,
118-
meta: {},
119-
meta_key: 'haha_meta'
118+
meta: {}
120119
).as_json
121120
expected = {
122121
data: {
123122
id: '1',
124123
type: 'blogs',
125124
attributes: { title: 'AMS Hints' }
126-
}
125+
},
126+
meta: {}
127127
}
128128
assert_equal(expected, actual)
129129
end
@@ -133,8 +133,7 @@ def test_meta_key_is_not_present_when_empty_string_with_json_api
133133
@blog,
134134
adapter: :json_api,
135135
serializer: AlternateBlogSerializer,
136-
meta: '',
137-
meta_key: 'haha_meta'
136+
meta: ''
138137
).as_json
139138
expected = {
140139
data: {

0 commit comments

Comments
 (0)