Skip to content

Commit

Permalink
SerializableResource handles no serializer like controller
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Mar 25, 2016
1 parent 82da04d commit 78fd688
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- [#1574](https://github.com/rails-api/active_model_serializers/pull/1574) Provide key translation. (@remear)
- [#1494](https://github.com/rails-api/active_model_serializers/pull/1494) Make serializers serializalbe
(using the Attributes adapter by default). (@bf4)
Expand Down
20 changes: 6 additions & 14 deletions lib/action_controller/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,12 @@ def get_serializer(resource, options = {})
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
serializable_resource.serialization_scope ||= serialization_scope
serializable_resource.serialization_scope_name = _serialization_scope
# 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
Expand Down
10 changes: 9 additions & 1 deletion lib/active_model/serializable_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@ def serialization_scope_name=(scope_name)
serializer_opts[:scope_name] = scope_name
end

# NOTE: if no adapter is available, returns the resource itself.
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
Expand Down

0 comments on commit 78fd688

Please sign in to comment.