mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #1616 from bf4/more_controller_logic_to_serializable_resource
SerializableResource handles no serializer like controller
This commit is contained in:
commit
5af8536713
@ -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
|
||||||
|
|||||||
@ -20,6 +20,7 @@ module ActiveModel
|
|||||||
class Serializer
|
class Serializer
|
||||||
extend ActiveSupport::Autoload
|
extend ActiveSupport::Autoload
|
||||||
autoload :Adapter
|
autoload :Adapter
|
||||||
|
autoload :Null
|
||||||
include Configuration
|
include Configuration
|
||||||
include Associations
|
include Associations
|
||||||
include Attributes
|
include Attributes
|
||||||
|
|||||||
17
lib/active_model/serializer/null.rb
Normal file
17
lib/active_model/serializer/null.rb
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
class Null < Serializer
|
||||||
|
def attributes(*)
|
||||||
|
{}
|
||||||
|
end
|
||||||
|
|
||||||
|
def associations(*)
|
||||||
|
{}
|
||||||
|
end
|
||||||
|
|
||||||
|
def serializable_hash(*)
|
||||||
|
{}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -81,7 +81,10 @@ module ActiveModelSerializers
|
|||||||
end
|
end
|
||||||
|
|
||||||
def notify_render_payload
|
def notify_render_payload
|
||||||
{ serializer: serializer, adapter: adapter }
|
{
|
||||||
|
serializer: serializer || ActiveModel::Serializer::Null,
|
||||||
|
adapter: adapter || ActiveModelSerializers::Adapter::Null
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user