SerializableResource handles no serializer like controller

This commit is contained in:
Benjamin Fleischer 2016-03-24 21:56:35 -05:00
parent 5af7d96294
commit 84197e4dad
3 changed files with 16 additions and 15 deletions

View File

@ -3,6 +3,7 @@
Breaking changes: Breaking changes:
Features: Features:
- [#1616](https://github.com/rails-api/active_model_serializers/pull/1616) SerializableResource handles no serializer like controller. (@bf4)
- [#1618](https://github.com/rails-api/active_model_serializers/issues/1618) Get collection root key for - [#1618](https://github.com/rails-api/active_model_serializers/issues/1618) Get collection root key for
empty collection from explicit serializer option, when possible. (@bf4) empty collection from explicit serializer option, when possible. (@bf4)
- [#1574](https://github.com/rails-api/active_model_serializers/pull/1574) Provide key translation. (@remear) - [#1574](https://github.com/rails-api/active_model_serializers/pull/1574) Provide key translation. (@remear)

View File

@ -33,20 +33,12 @@ module ActionController
options[:adapter] = false options[:adapter] = false
end end
serializable_resource = ActiveModel::SerializableResource.new(resource, options) serializable_resource = ActiveModel::SerializableResource.new(resource, options)
if serializable_resource.serializer? serializable_resource.serialization_scope ||= serialization_scope
serializable_resource.serialization_scope ||= serialization_scope serializable_resource.serialization_scope_name = _serialization_scope
serializable_resource.serialization_scope_name = _serialization_scope # For compatibility with the JSON renderer: `json.to_json(options) if json.is_a?(String)`.
begin # Otherwise, since `serializable_resource` is not a string, the renderer would call
# Necessary to ensure we have an adapter for the serializable resource # `to_json` on a String and given odd results, such as `"".to_json #=> '""'`
# after it has been figured. serializable_resource.adapter.is_a?(String) ? serializable_resource.adapter : serializable_resource
# TODO: This logic should be less opaque and probably moved into the SerializableResource.
serializable_resource.tap(&:adapter)
rescue ActiveModel::Serializer::CollectionSerializer::NoSerializerError
resource
end
else
resource
end
end end
# Deprecated # Deprecated

View File

@ -30,11 +30,19 @@ module ActiveModel
serializer_opts[:scope_name] = scope_name serializer_opts[:scope_name] = scope_name
end end
# NOTE: if no adapter is available, returns the resource itself. (i.e. adapter is a no-op)
def adapter def adapter
@adapter ||= ActiveModelSerializers::Adapter.create(serializer_instance, adapter_opts) @adapter ||= find_adapter
end end
alias adapter_instance adapter alias adapter_instance adapter
def find_adapter
return resource unless serializer?
ActiveModelSerializers::Adapter.create(serializer_instance, adapter_opts)
rescue ActiveModel::Serializer::CollectionSerializer::NoSerializerError
resource
end
def serializer_instance def serializer_instance
@serializer_instance ||= serializer.new(resource, serializer_opts) @serializer_instance ||= serializer.new(resource, serializer_opts)
end end