mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
SerializableResource handles no serializer like controller
This commit is contained in:
parent
5af7d96294
commit
84197e4dad
@ -3,6 +3,7 @@
|
||||
Breaking changes:
|
||||
|
||||
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
|
||||
empty collection from explicit serializer option, when possible. (@bf4)
|
||||
- [#1574](https://github.com/rails-api/active_model_serializers/pull/1574) Provide key translation. (@remear)
|
||||
|
||||
@ -33,20 +33,12 @@ module ActionController
|
||||
options[:adapter] = false
|
||||
end
|
||||
serializable_resource = ActiveModel::SerializableResource.new(resource, options)
|
||||
if serializable_resource.serializer?
|
||||
serializable_resource.serialization_scope ||= serialization_scope
|
||||
serializable_resource.serialization_scope_name = _serialization_scope
|
||||
begin
|
||||
# Necessary to ensure we have an adapter for the serializable resource
|
||||
# after it has been figured.
|
||||
# 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
|
||||
# For compatibility with the JSON renderer: `json.to_json(options) if json.is_a?(String)`.
|
||||
# Otherwise, since `serializable_resource` is not a string, the renderer would call
|
||||
# `to_json` on a String and given odd results, such as `"".to_json #=> '""'`
|
||||
serializable_resource.adapter.is_a?(String) ? serializable_resource.adapter : serializable_resource
|
||||
end
|
||||
|
||||
# Deprecated
|
||||
|
||||
@ -30,11 +30,19 @@ module ActiveModel
|
||||
serializer_opts[:scope_name] = scope_name
|
||||
end
|
||||
|
||||
# NOTE: if no adapter is available, returns the resource itself. (i.e. adapter is a no-op)
|
||||
def adapter
|
||||
@adapter ||= ActiveModelSerializers::Adapter.create(serializer_instance, adapter_opts)
|
||||
@adapter ||= find_adapter
|
||||
end
|
||||
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
|
||||
@serializer_instance ||= serializer.new(resource, serializer_opts)
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user