Skip to content

Commit

Permalink
Merge pull request #1129 from bf4/remove_serializable_resource_serialize
Browse files Browse the repository at this point in the history
Remove SerializableResource.serialize in favor of `.new`
  • Loading branch information
NullVoxPopuli committed Sep 15, 2015
2 parents 2111ae8 + 7eddbe4 commit 610775a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
21 changes: 10 additions & 11 deletions lib/action_controller/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,20 @@ def serialization_scope
def get_serializer(resource, options = {})
if !use_adapter?
warn 'ActionController::Serialization#use_adapter? has been removed. '\
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource#serialize"
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource.new"
options[:adapter] = false
end
ActiveModel::SerializableResource.serialize(resource, options) do |serializable_resource|
if serializable_resource.serializer?
serializable_resource.serialization_scope ||= serialization_scope
serializable_resource.serialization_scope_name = _serialization_scope
begin
serializable_resource.adapter
rescue ActiveModel::Serializer::ArraySerializer::NoSerializerError
resource
end
else
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
serializable_resource.adapter
rescue ActiveModel::Serializer::ArraySerializer::NoSerializerError
resource
end
else
resource
end
end

Expand Down
16 changes: 2 additions & 14 deletions lib/active_model/serializable_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module ActiveModel
class SerializableResource
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter])

# Primary interface to composing a resource with a serializer and adapter.
# @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
def initialize(resource, options = {})
@resource = resource
@adapter_opts, @serializer_opts =
Expand All @@ -11,20 +13,6 @@ def initialize(resource, options = {})

delegate :serializable_hash, :as_json, :to_json, to: :adapter

# Primary interface to building a serializer (with adapter)
# If no block is given,
# returns the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
# Otherwise, yields the serializable_resource and
# returns the contents of the block
def self.serialize(resource, options = {})
serializable_resource = SerializableResource.new(resource, options)
if block_given?
yield serializable_resource
else
serializable_resource
end
end

def serialization_scope=(scope)
serializer_opts[:scope] = scope
end
Expand Down

0 comments on commit 610775a

Please sign in to comment.