mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #1233 from beauby/move-root-to-adapter2
Move `meta`/`meta_key` handling inside adapter.
This commit is contained in:
commit
d02cd30fe5
@ -24,6 +24,7 @@ Fixes:
|
|||||||
- [#1214](https://github.com/rails-api/active_model_serializers/pull/1214) retrieve the key from the reflection options when building associations (@NullVoxPopuli, @hut8)
|
- [#1214](https://github.com/rails-api/active_model_serializers/pull/1214) retrieve the key from the reflection options when building associations (@NullVoxPopuli, @hut8)
|
||||||
|
|
||||||
Misc:
|
Misc:
|
||||||
|
- [#1233](https://github.com/rails-api/active_model_serializers/pull/1233) Top-level meta and meta_key options no longer handled at serializer level (@beauby)
|
||||||
- [#1232](https://github.com/rails-api/active_model_serializers/pull/1232) fields option no longer handled at serializer level (@beauby)
|
- [#1232](https://github.com/rails-api/active_model_serializers/pull/1232) fields option no longer handled at serializer level (@beauby)
|
||||||
- [#1178](https://github.com/rails-api/active_model_serializers/pull/1178) env CAPTURE_STDERR=false lets devs see hard failures (@bf4)
|
- [#1178](https://github.com/rails-api/active_model_serializers/pull/1178) env CAPTURE_STDERR=false lets devs see hard failures (@bf4)
|
||||||
- [#1177](https://github.com/rails-api/active_model_serializers/pull/1177) Remove Adapter autoloads in favor of require (@bf4)
|
- [#1177](https://github.com/rails-api/active_model_serializers/pull/1177) Remove Adapter autoloads in favor of require (@bf4)
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
require 'set'
|
require 'set'
|
||||||
module ActiveModel
|
module ActiveModel
|
||||||
class SerializableResource
|
class SerializableResource
|
||||||
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter])
|
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key])
|
||||||
|
|
||||||
# Primary interface to composing a resource with a serializer and adapter.
|
# Primary interface to composing a resource with a serializer and adapter.
|
||||||
# @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
|
# @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
|
||||||
|
|||||||
@ -121,15 +121,13 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_accessor :object, :root, :meta, :meta_key, :scope
|
attr_accessor :object, :root, :scope
|
||||||
class_attribute :_type, instance_writer: false
|
class_attribute :_type, instance_writer: false
|
||||||
|
|
||||||
def initialize(object, options = {})
|
def initialize(object, options = {})
|
||||||
self.object = object
|
self.object = object
|
||||||
self.instance_options = options
|
self.instance_options = options
|
||||||
self.root = instance_options[:root]
|
self.root = instance_options[:root]
|
||||||
self.meta = instance_options[:meta]
|
|
||||||
self.meta_key = instance_options[:meta_key]
|
|
||||||
self.scope = instance_options[:scope]
|
self.scope = instance_options[:scope]
|
||||||
|
|
||||||
scope_name = instance_options[:scope_name]
|
scope_name = instance_options[:scope_name]
|
||||||
|
|||||||
@ -37,11 +37,11 @@ module ActiveModel
|
|||||||
private
|
private
|
||||||
|
|
||||||
def meta
|
def meta
|
||||||
serializer.meta if serializer.respond_to?(:meta)
|
instance_options.fetch(:meta, nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
def meta_key
|
def meta_key
|
||||||
serializer.meta_key || 'meta'.freeze
|
instance_options.fetch(:meta_key, 'meta'.freeze)
|
||||||
end
|
end
|
||||||
|
|
||||||
def root
|
def root
|
||||||
|
|||||||
@ -5,7 +5,7 @@ module ActiveModel
|
|||||||
include Enumerable
|
include Enumerable
|
||||||
delegate :each, to: :@serializers
|
delegate :each, to: :@serializers
|
||||||
|
|
||||||
attr_reader :object, :root, :meta, :meta_key
|
attr_reader :object, :root
|
||||||
|
|
||||||
def initialize(resources, options = {})
|
def initialize(resources, options = {})
|
||||||
@root = options[:root]
|
@root = options[:root]
|
||||||
@ -21,8 +21,6 @@ module ActiveModel
|
|||||||
serializer_class.new(resource, options.except(:serializer))
|
serializer_class.new(resource, options.except(:serializer))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@meta = options[:meta]
|
|
||||||
@meta_key = options[:meta_key]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def json_key
|
def json_key
|
||||||
|
|||||||
@ -41,14 +41,6 @@ module ActiveModel
|
|||||||
refute serializers.first.custom_options.key?(:serializer)
|
refute serializers.first.custom_options.key?(:serializer)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_meta_and_meta_key_attr_readers
|
|
||||||
meta_content = { meta: 'the meta', meta_key: 'the meta key' }
|
|
||||||
@serializer = ArraySerializer.new([@comment, @post], meta_content)
|
|
||||||
|
|
||||||
assert_equal @serializer.meta, 'the meta'
|
|
||||||
assert_equal @serializer.meta_key, 'the meta key'
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_root_default
|
def test_root_default
|
||||||
@serializer = ArraySerializer.new([@comment, @post])
|
@serializer = ArraySerializer.new([@comment, @post])
|
||||||
assert_equal @serializer.root, nil
|
assert_equal @serializer.root, nil
|
||||||
|
|||||||
@ -4,7 +4,6 @@ module ActiveModel
|
|||||||
class Serializer
|
class Serializer
|
||||||
class MetaTest < Minitest::Test
|
class MetaTest < Minitest::Test
|
||||||
def setup
|
def setup
|
||||||
ActionController::Base.cache_store.clear
|
|
||||||
@blog = Blog.new(id: 1,
|
@blog = Blog.new(id: 1,
|
||||||
name: 'AMS Hints',
|
name: 'AMS Hints',
|
||||||
writer: Author.new(id: 2, name: 'Steve'),
|
writer: Author.new(id: 2, name: 'Steve'),
|
||||||
@ -12,8 +11,11 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_meta_is_present_with_root
|
def test_meta_is_present_with_root
|
||||||
serializer = AlternateBlogSerializer.new(@blog, meta: { total: 10 })
|
actual = ActiveModel::SerializableResource.new(
|
||||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
@blog,
|
||||||
|
adapter: :json,
|
||||||
|
serializer: AlternateBlogSerializer,
|
||||||
|
meta: { total: 10 }).as_json
|
||||||
expected = {
|
expected = {
|
||||||
blog: {
|
blog: {
|
||||||
id: 1,
|
id: 1,
|
||||||
@ -23,22 +25,29 @@ module ActiveModel
|
|||||||
total: 10
|
total: 10
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert_equal expected, adapter.as_json
|
assert_equal(expected, actual)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_meta_is_not_included_when_root_is_missing
|
def test_meta_is_not_included_when_root_is_missing
|
||||||
# load_adapter uses Attributes Adapter
|
actual = ActiveModel::SerializableResource.new(
|
||||||
adapter = load_adapter(meta: { total: 10 })
|
@blog,
|
||||||
|
adapter: :attributes,
|
||||||
|
serializer: AlternateBlogSerializer,
|
||||||
|
meta: { total: 10 }).as_json
|
||||||
expected = {
|
expected = {
|
||||||
id: 1,
|
id: 1,
|
||||||
title: 'AMS Hints'
|
title: 'AMS Hints'
|
||||||
}
|
}
|
||||||
assert_equal expected, adapter.as_json
|
assert_equal(expected, actual)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_meta_key_is_used
|
def test_meta_key_is_used
|
||||||
serializer = AlternateBlogSerializer.new(@blog, meta: { total: 10 }, meta_key: 'haha_meta')
|
actual = ActiveModel::SerializableResource.new(
|
||||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
@blog,
|
||||||
|
adapter: :json,
|
||||||
|
serializer: AlternateBlogSerializer,
|
||||||
|
meta: { total: 10 },
|
||||||
|
meta_key: 'haha_meta').as_json
|
||||||
expected = {
|
expected = {
|
||||||
blog: {
|
blog: {
|
||||||
id: 1,
|
id: 1,
|
||||||
@ -48,12 +57,16 @@ module ActiveModel
|
|||||||
total: 10
|
total: 10
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert_equal expected, adapter.as_json
|
assert_equal(expected, actual)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_meta_key_is_used_with_json_api
|
def test_meta_key_is_used_with_json_api
|
||||||
serializer = AlternateBlogSerializer.new(@blog, meta: { total: 10 }, meta_key: 'haha_meta')
|
actual = ActiveModel::SerializableResource.new(
|
||||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
@blog,
|
||||||
|
adapter: :json_api,
|
||||||
|
serializer: AlternateBlogSerializer,
|
||||||
|
meta: { total: 10 },
|
||||||
|
meta_key: 'haha_meta').as_json
|
||||||
expected = {
|
expected = {
|
||||||
data: {
|
data: {
|
||||||
id: '1',
|
id: '1',
|
||||||
@ -62,13 +75,14 @@ module ActiveModel
|
|||||||
},
|
},
|
||||||
'haha_meta' => { total: 10 }
|
'haha_meta' => { total: 10 }
|
||||||
}
|
}
|
||||||
assert_equal expected, adapter.as_json
|
assert_equal(expected, actual)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_meta_is_not_present_on_arrays_without_root
|
def test_meta_is_not_present_on_arrays_without_root
|
||||||
serializer = ArraySerializer.new([@blog], meta: { total: 10 })
|
actual = ActiveModel::SerializableResource.new(
|
||||||
# Attributes doesn't have support to root
|
[@blog],
|
||||||
adapter = ActiveModel::Serializer::Adapter::Attributes.new(serializer)
|
adapter: :attributes,
|
||||||
|
meta: { total: 10 }).as_json
|
||||||
expected = [{
|
expected = [{
|
||||||
id: 1,
|
id: 1,
|
||||||
name: 'AMS Hints',
|
name: 'AMS Hints',
|
||||||
@ -82,13 +96,15 @@ module ActiveModel
|
|||||||
body: nil
|
body: nil
|
||||||
}]
|
}]
|
||||||
}]
|
}]
|
||||||
assert_equal expected, adapter.as_json
|
assert_equal(expected, actual)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_meta_is_present_on_arrays_with_root
|
def test_meta_is_present_on_arrays_with_root
|
||||||
serializer = ArraySerializer.new([@blog], meta: { total: 10 }, meta_key: 'haha_meta')
|
actual = ActiveModel::SerializableResource.new(
|
||||||
# JSON adapter adds root by default
|
[@blog],
|
||||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
adapter: :json,
|
||||||
|
meta: { total: 10 },
|
||||||
|
meta_key: 'haha_meta').as_json
|
||||||
expected = {
|
expected = {
|
||||||
blogs: [{
|
blogs: [{
|
||||||
id: 1,
|
id: 1,
|
||||||
@ -107,14 +123,7 @@ module ActiveModel
|
|||||||
total: 10
|
total: 10
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert_equal expected, adapter.as_json
|
assert_equal(expected, actual)
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def load_adapter(options)
|
|
||||||
options = options.merge(adapter: :attributes, serializer: AlternateBlogSerializer)
|
|
||||||
ActiveModel::SerializableResource.new(@blog, options)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user