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:
|
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)
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user