mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Move SerializableResource to ActiveModelSerializers namespace
Ref. https://github.com/rails-api/active_model_serializers/pull/1310
This commit is contained in:
@@ -39,37 +39,37 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_uses_ams_as_tag
|
||||
ActiveModel::SerializableResource.new(@post).serializable_hash
|
||||
ActiveModelSerializers::SerializableResource.new(@post).serializable_hash
|
||||
assert_match(/\[active_model_serializers\]/, @logger.messages)
|
||||
end
|
||||
|
||||
def test_logs_when_call_serializable_hash
|
||||
ActiveModel::SerializableResource.new(@post).serializable_hash
|
||||
ActiveModelSerializers::SerializableResource.new(@post).serializable_hash
|
||||
assert_match(/Rendered/, @logger.messages)
|
||||
end
|
||||
|
||||
def test_logs_when_call_as_json
|
||||
ActiveModel::SerializableResource.new(@post).as_json
|
||||
ActiveModelSerializers::SerializableResource.new(@post).as_json
|
||||
assert_match(/Rendered/, @logger.messages)
|
||||
end
|
||||
|
||||
def test_logs_when_call_to_json
|
||||
ActiveModel::SerializableResource.new(@post).to_json
|
||||
ActiveModelSerializers::SerializableResource.new(@post).to_json
|
||||
assert_match(/Rendered/, @logger.messages)
|
||||
end
|
||||
|
||||
def test_logs_correct_serializer
|
||||
ActiveModel::SerializableResource.new(@post).serializable_hash
|
||||
ActiveModelSerializers::SerializableResource.new(@post).serializable_hash
|
||||
assert_match(/PostSerializer/, @logger.messages)
|
||||
end
|
||||
|
||||
def test_logs_correct_adapter
|
||||
ActiveModel::SerializableResource.new(@post).serializable_hash
|
||||
ActiveModelSerializers::SerializableResource.new(@post).serializable_hash
|
||||
assert_match(/ActiveModelSerializers::Adapter::Attributes/, @logger.messages)
|
||||
end
|
||||
|
||||
def test_logs_the_duration
|
||||
ActiveModel::SerializableResource.new(@post).serializable_hash
|
||||
ActiveModelSerializers::SerializableResource.new(@post).serializable_hash
|
||||
assert_match(/\(\d+\.\d+ms\)/, @logger.messages)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -57,10 +57,10 @@ module ActiveModelSerializers
|
||||
end
|
||||
|
||||
def test_limiting_fields
|
||||
actual = ActiveModel::SerializableResource.new(
|
||||
actual = ActiveModelSerializers::SerializableResource.new(
|
||||
[@first_post, @second_post], adapter: :json_api,
|
||||
fields: { posts: %w(title comments blog author) })
|
||||
.serializable_hash
|
||||
.serializable_hash
|
||||
expected = [
|
||||
{
|
||||
id: '1',
|
||||
|
||||
@@ -18,7 +18,7 @@ module ActiveModelSerializers
|
||||
|
||||
@resource.errors.add(:name, 'cannot be nil')
|
||||
|
||||
serializable_resource = ActiveModel::SerializableResource.new(@resource, options)
|
||||
serializable_resource = ActiveModelSerializers::SerializableResource.new(@resource, options)
|
||||
assert_equal serializable_resource.serializer_instance.attributes, {}
|
||||
assert_equal serializable_resource.serializer_instance.object, @resource
|
||||
|
||||
@@ -44,7 +44,7 @@ module ActiveModelSerializers
|
||||
@resource.errors.add(:name, 'must be longer')
|
||||
@resource.errors.add(:id, 'must be a uuid')
|
||||
|
||||
serializable_resource = ActiveModel::SerializableResource.new(@resource, options)
|
||||
serializable_resource = ActiveModelSerializers::SerializableResource.new(@resource, options)
|
||||
assert_equal serializable_resource.serializer_instance.attributes, {}
|
||||
assert_equal serializable_resource.serializer_instance.object, @resource
|
||||
|
||||
|
||||
@@ -312,9 +312,9 @@ module ActiveModelSerializers
|
||||
end
|
||||
|
||||
def test_no_duplicates
|
||||
hash = ActiveModel::SerializableResource.new(@post1, adapter: :json_api,
|
||||
include: '*.*')
|
||||
.serializable_hash
|
||||
hash = ActiveModelSerializers::SerializableResource.new(@post1, adapter: :json_api,
|
||||
include: '*.*')
|
||||
.serializable_hash
|
||||
expected = [
|
||||
{
|
||||
type: 'authors', id: '1',
|
||||
@@ -340,10 +340,10 @@ module ActiveModelSerializers
|
||||
end
|
||||
|
||||
def test_no_duplicates_collection
|
||||
hash = ActiveModel::SerializableResource.new(
|
||||
hash = ActiveModelSerializers::SerializableResource.new(
|
||||
[@post1, @post2], adapter: :json_api,
|
||||
include: '*.*')
|
||||
.serializable_hash
|
||||
.serializable_hash
|
||||
expected = [
|
||||
{
|
||||
type: 'authors', id: '1',
|
||||
@@ -361,7 +361,7 @@ module ActiveModelSerializers
|
||||
end
|
||||
|
||||
def test_no_duplicates_global
|
||||
hash = ActiveModel::SerializableResource.new(
|
||||
hash = ActiveModelSerializers::SerializableResource.new(
|
||||
@nestedpost1,
|
||||
adapter: :json_api,
|
||||
include: '*').serializable_hash
|
||||
@@ -380,7 +380,7 @@ module ActiveModelSerializers
|
||||
end
|
||||
|
||||
def test_no_duplicates_collection_global
|
||||
hash = ActiveModel::SerializableResource.new(
|
||||
hash = ActiveModelSerializers::SerializableResource.new(
|
||||
[@nestedpost1, @nestedpost2],
|
||||
adapter: :json_api,
|
||||
include: '*').serializable_hash
|
||||
|
||||
@@ -30,7 +30,7 @@ module ActiveModelSerializers
|
||||
end
|
||||
|
||||
def test_toplevel_links
|
||||
hash = ActiveModel::SerializableResource.new(
|
||||
hash = ActiveModelSerializers::SerializableResource.new(
|
||||
@post,
|
||||
adapter: :json_api,
|
||||
links: {
|
||||
@@ -53,7 +53,7 @@ module ActiveModelSerializers
|
||||
end
|
||||
|
||||
def test_nil_toplevel_links
|
||||
hash = ActiveModel::SerializableResource.new(
|
||||
hash = ActiveModelSerializers::SerializableResource.new(
|
||||
@post,
|
||||
adapter: :json_api,
|
||||
links: nil
|
||||
@@ -62,7 +62,7 @@ module ActiveModelSerializers
|
||||
end
|
||||
|
||||
def test_nil_toplevel_links_json_adapter
|
||||
hash = ActiveModel::SerializableResource.new(
|
||||
hash = ActiveModelSerializers::SerializableResource.new(
|
||||
@post,
|
||||
adapter: :json,
|
||||
links: nil
|
||||
|
||||
@@ -32,7 +32,7 @@ module ActiveModelSerializers
|
||||
|
||||
def load_adapter(paginated_collection, options = {})
|
||||
options = options.merge(adapter: :json_api)
|
||||
ActiveModel::SerializableResource.new(paginated_collection, options)
|
||||
ActiveModelSerializers::SerializableResource.new(paginated_collection, options)
|
||||
end
|
||||
|
||||
def using_kaminari(page = 2)
|
||||
|
||||
@@ -36,7 +36,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_meta_hash_object_resource
|
||||
hash = ActiveModel::SerializableResource.new(
|
||||
hash = ActiveModelSerializers::SerializableResource.new(
|
||||
@post,
|
||||
serializer: MetaHashPostSerializer,
|
||||
adapter: :json_api
|
||||
@@ -48,7 +48,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_meta_block_object_resource
|
||||
hash = ActiveModel::SerializableResource.new(
|
||||
hash = ActiveModelSerializers::SerializableResource.new(
|
||||
@post,
|
||||
serializer: MetaBlockPostSerializer,
|
||||
adapter: :json_api
|
||||
@@ -62,7 +62,7 @@ module ActiveModel
|
||||
def test_meta_object_resource_in_array
|
||||
post2 = Post.new(id: 1339, comments: [Comment.new])
|
||||
posts = [@post, post2]
|
||||
hash = ActiveModel::SerializableResource.new(
|
||||
hash = ActiveModelSerializers::SerializableResource.new(
|
||||
posts,
|
||||
each_serializer: MetaBlockPostSerializer,
|
||||
adapter: :json_api
|
||||
@@ -77,7 +77,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_meta_object_blank_omitted
|
||||
hash = ActiveModel::SerializableResource.new(
|
||||
hash = ActiveModelSerializers::SerializableResource.new(
|
||||
@post,
|
||||
serializer: MetaBlockPostBlankMetaSerializer,
|
||||
adapter: :json_api
|
||||
@@ -86,7 +86,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_meta_object_empty_string_omitted
|
||||
hash = ActiveModel::SerializableResource.new(
|
||||
hash = ActiveModelSerializers::SerializableResource.new(
|
||||
@post,
|
||||
serializer: MetaBlockPostEmptyStringSerializer,
|
||||
adapter: :json_api
|
||||
|
||||
@@ -257,7 +257,7 @@ module ActiveModelSerializers
|
||||
private
|
||||
|
||||
def render_object_with_cache(obj, options = {})
|
||||
ActiveModel::SerializableResource.new(obj, options).serializable_hash
|
||||
SerializableResource.new(obj, options).serializable_hash
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
module ActiveModelSerializers
|
||||
class SerializableResourceTest < ActiveSupport::TestCase
|
||||
def setup
|
||||
@resource = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
@serializer = ProfileSerializer.new(@resource)
|
||||
@adapter = ActiveModelSerializers::Adapter.create(@serializer)
|
||||
@serializable_resource = ActiveModel::SerializableResource.new(@resource)
|
||||
@serializable_resource = SerializableResource.new(@resource)
|
||||
end
|
||||
|
||||
def test_deprecation
|
||||
assert_output(nil, /deprecated/) do
|
||||
deprecated_serializable_resource = ActiveModel::SerializableResource.new(@resource)
|
||||
assert_equal(@serializable_resource.as_json, deprecated_serializable_resource.as_json)
|
||||
end
|
||||
end
|
||||
|
||||
def test_serializable_resource_delegates_serializable_hash_to_the_adapter
|
||||
@@ -25,11 +32,11 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_use_adapter_with_adapter_option
|
||||
assert ActiveModel::SerializableResource.new(@resource, { adapter: 'json' }).use_adapter?
|
||||
assert SerializableResource.new(@resource, { adapter: 'json' }).use_adapter?
|
||||
end
|
||||
|
||||
def test_use_adapter_with_adapter_option_as_false
|
||||
refute ActiveModel::SerializableResource.new(@resource, { adapter: false }).use_adapter?
|
||||
refute SerializableResource.new(@resource, { adapter: false }).use_adapter?
|
||||
end
|
||||
|
||||
class SerializableResourceErrorsTest < Minitest::Test
|
||||
@@ -37,7 +44,7 @@ module ActiveModel
|
||||
options = nil
|
||||
resource = ModelWithErrors.new
|
||||
resource.errors.add(:name, 'must be awesome')
|
||||
serializable_resource = ActiveModel::SerializableResource.new(
|
||||
serializable_resource = ActiveModelSerializers::SerializableResource.new(
|
||||
resource, {
|
||||
serializer: ActiveModel::Serializer::ErrorSerializer,
|
||||
adapter: :json_api
|
||||
@@ -57,7 +64,7 @@ module ActiveModel
|
||||
resources << resource = ModelWithErrors.new
|
||||
resource.errors.add(:title, 'must be amazing')
|
||||
resources << ModelWithErrors.new
|
||||
serializable_resource = ActiveModel::SerializableResource.new(
|
||||
serializable_resource = SerializableResource.new(
|
||||
resources, {
|
||||
serializer: ActiveModel::Serializer::ErrorsSerializer,
|
||||
each_serializer: ActiveModel::Serializer::ErrorSerializer,
|
||||
|
||||
@@ -76,7 +76,7 @@ module ActiveModel
|
||||
attribute :id
|
||||
end
|
||||
|
||||
hash = ActiveModel::SerializableResource.new(@blog, adapter: :json, serializer: serializer).serializable_hash
|
||||
hash = ActiveModelSerializers::SerializableResource.new(@blog, adapter: :json, serializer: serializer).serializable_hash
|
||||
|
||||
assert_equal('custom', hash[:blog][:id])
|
||||
end
|
||||
|
||||
@@ -11,7 +11,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_meta_is_present_with_root
|
||||
actual = ActiveModel::SerializableResource.new(
|
||||
actual = ActiveModelSerializers::SerializableResource.new(
|
||||
@blog,
|
||||
adapter: :json,
|
||||
serializer: AlternateBlogSerializer,
|
||||
@@ -29,7 +29,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_meta_is_not_included_when_blank
|
||||
actual = ActiveModel::SerializableResource.new(
|
||||
actual = ActiveModelSerializers::SerializableResource.new(
|
||||
@blog,
|
||||
adapter: :json,
|
||||
serializer: AlternateBlogSerializer,
|
||||
@@ -45,7 +45,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_meta_is_not_included_when_empty_string
|
||||
actual = ActiveModel::SerializableResource.new(
|
||||
actual = ActiveModelSerializers::SerializableResource.new(
|
||||
@blog,
|
||||
adapter: :json,
|
||||
serializer: AlternateBlogSerializer,
|
||||
@@ -61,7 +61,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_meta_is_not_included_when_root_is_missing
|
||||
actual = ActiveModel::SerializableResource.new(
|
||||
actual = ActiveModelSerializers::SerializableResource.new(
|
||||
@blog,
|
||||
adapter: :attributes,
|
||||
serializer: AlternateBlogSerializer,
|
||||
@@ -74,7 +74,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_meta_key_is_used
|
||||
actual = ActiveModel::SerializableResource.new(
|
||||
actual = ActiveModelSerializers::SerializableResource.new(
|
||||
@blog,
|
||||
adapter: :json,
|
||||
serializer: AlternateBlogSerializer,
|
||||
@@ -93,7 +93,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_meta_key_is_used_with_json_api
|
||||
actual = ActiveModel::SerializableResource.new(
|
||||
actual = ActiveModelSerializers::SerializableResource.new(
|
||||
@blog,
|
||||
adapter: :json_api,
|
||||
serializer: AlternateBlogSerializer,
|
||||
@@ -111,7 +111,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_meta_key_is_not_present_when_blank_object_with_json_api
|
||||
actual = ActiveModel::SerializableResource.new(
|
||||
actual = ActiveModelSerializers::SerializableResource.new(
|
||||
@blog,
|
||||
adapter: :json_api,
|
||||
serializer: AlternateBlogSerializer,
|
||||
@@ -129,7 +129,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_meta_key_is_not_present_when_empty_string_with_json_api
|
||||
actual = ActiveModel::SerializableResource.new(
|
||||
actual = ActiveModelSerializers::SerializableResource.new(
|
||||
@blog,
|
||||
adapter: :json_api,
|
||||
serializer: AlternateBlogSerializer,
|
||||
@@ -147,7 +147,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_meta_is_not_present_on_arrays_without_root
|
||||
actual = ActiveModel::SerializableResource.new(
|
||||
actual = ActiveModelSerializers::SerializableResource.new(
|
||||
[@blog],
|
||||
adapter: :attributes,
|
||||
meta: { total: 10 }).as_json
|
||||
@@ -168,7 +168,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_meta_is_present_on_arrays_with_root
|
||||
actual = ActiveModel::SerializableResource.new(
|
||||
actual = ActiveModelSerializers::SerializableResource.new(
|
||||
[@blog],
|
||||
adapter: :json,
|
||||
meta: { total: 10 },
|
||||
|
||||
@@ -21,7 +21,7 @@ module ActiveModel
|
||||
@authors = [Author.new(id: 1, name: 'Blog Author')]
|
||||
@blog = Blog.new(id: 2, name: 'The Blog', authors: @authors)
|
||||
@serializer_instance = BlogSerializer.new(@blog)
|
||||
@serializable = ActiveModel::SerializableResource.new(@blog, serializer: BlogSerializer, adapter: :attributes)
|
||||
@serializable = ActiveModelSerializers::SerializableResource.new(@blog, serializer: BlogSerializer, adapter: :attributes)
|
||||
@expected_hash = { id: 2, title: 'The Blog', authors: [{ id: 1, name: 'Blog Author' }] }
|
||||
@expected_json = '{"id":2,"title":"The Blog","authors":[{"id":1,"name":"Blog Author"}]}'
|
||||
end
|
||||
|
||||
@@ -6,14 +6,14 @@ module SerializationTesting
|
||||
private
|
||||
|
||||
def generate_cached_serializer(obj)
|
||||
ActiveModel::SerializableResource.new(obj).to_json
|
||||
ActiveModelSerializers::SerializableResource.new(obj).to_json
|
||||
end
|
||||
|
||||
# Aliased as :with_configured_adapter to clarify that
|
||||
# this method tests the configured adapter.
|
||||
# When not testing configuration, it may be preferable
|
||||
# to pass in the +adapter+ option to <tt>ActiveModel::SerializableResource</tt>.
|
||||
# e.g ActiveModel::SerializableResource.new(resource, adapter: :json_api)
|
||||
# to pass in the +adapter+ option to <tt>ActiveModelSerializers::SerializableResource</tt>.
|
||||
# e.g ActiveModelSerializers::SerializableResource.new(resource, adapter: :json_api)
|
||||
def with_adapter(adapter)
|
||||
old_adapter = ActiveModelSerializers.config.adapter
|
||||
ActiveModelSerializers.config.adapter = adapter
|
||||
@@ -40,7 +40,7 @@ module SerializationTesting
|
||||
end
|
||||
|
||||
def serializable(resource, options = {})
|
||||
ActiveModel::SerializableResource.new(resource, options)
|
||||
ActiveModelSerializers::SerializableResource.new(resource, options)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user