mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Idea per remear (Ben Mills) in the slack: https://amserializers.slack.com/archives/general/p1455140474000171 remear: just so i understand, the adapter in `render json: resource, status: 422, adapter: 'json_api/error', serializer: ActiveModel::Serializer::ErrorSerializer` is a different one than, say what i’ve specified in a base serializer with `ActiveModel::Serializer.config.adapter = :json_api`. correct? and a followup question of, why not same adapter but different serializer? me: With the way the code is written now, it might be possible to not require a special jsonapi adapter. However, the behavior is pretty different from the jsonapi adapter. this first draft of the PR had it automatically set the adapter if there were errors. since that requires more discussion, I took a step back and made it explicit for this PR If I were to re-use the json api adapter and remove the error one, it think the serializable hash method would look like ``` def serializable_hash(options = nil) return { errors: JsonApi::Error.collection_errors } if serializer.is_a?(ErrorsSerializer) return { errors: JsonApi::Error.resource_errors(serializer) } if serializer.is_a?(ErrorSerializer) options ||= {} ``` I suppose it could be something more duckish like ``` def serializable_hash(options = nil) if serializer.errors? # object.errors.any? || object.any? {|o| o.errors.any? } JsonApi::Error.new(serializer).serializable_hash else # etc ```
77 lines
2.8 KiB
Ruby
77 lines
2.8 KiB
Ruby
require 'test_helper'
|
|
|
|
module ActiveModel
|
|
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)
|
|
end
|
|
|
|
def test_serializable_resource_delegates_serializable_hash_to_the_adapter
|
|
options = nil
|
|
assert_equal @adapter.serializable_hash(options), @serializable_resource.serializable_hash(options)
|
|
end
|
|
|
|
def test_serializable_resource_delegates_to_json_to_the_adapter
|
|
options = nil
|
|
assert_equal @adapter.to_json(options), @serializable_resource.to_json(options)
|
|
end
|
|
|
|
def test_serializable_resource_delegates_as_json_to_the_adapter
|
|
options = nil
|
|
assert_equal @adapter.as_json(options), @serializable_resource.as_json(options)
|
|
end
|
|
|
|
def test_use_adapter_with_adapter_option
|
|
assert ActiveModel::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?
|
|
end
|
|
|
|
class SerializableResourceErrorsTest < Minitest::Test
|
|
def test_serializable_resource_with_errors
|
|
options = nil
|
|
resource = ModelWithErrors.new
|
|
resource.errors.add(:name, 'must be awesome')
|
|
serializable_resource = ActiveModel::SerializableResource.new(
|
|
resource, {
|
|
serializer: ActiveModel::Serializer::ErrorSerializer,
|
|
adapter: :json_api
|
|
})
|
|
expected_response_document =
|
|
{ :errors =>
|
|
[
|
|
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'must be awesome' }
|
|
]
|
|
}
|
|
assert_equal serializable_resource.as_json(options), expected_response_document
|
|
end
|
|
|
|
def test_serializable_resource_with_collection_containing_errors
|
|
options = nil
|
|
resources = []
|
|
resources << resource = ModelWithErrors.new
|
|
resource.errors.add(:title, 'must be amazing')
|
|
resources << ModelWithErrors.new
|
|
serializable_resource = ActiveModel::SerializableResource.new(
|
|
resources, {
|
|
serializer: ActiveModel::Serializer::ErrorsSerializer,
|
|
each_serializer: ActiveModel::Serializer::ErrorSerializer,
|
|
adapter: :json_api
|
|
})
|
|
expected_response_document =
|
|
{ :errors =>
|
|
[
|
|
{ :source => { :pointer => '/data/attributes/title' }, :detail => 'must be amazing' }
|
|
]
|
|
}
|
|
assert_equal serializable_resource.as_json(options), expected_response_document
|
|
end
|
|
end
|
|
end
|
|
end
|