Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
module ActiveModel
class Serializer
# @see #serializable_hash for more details on these valid keys.
SERIALIZABLE_HASH_VALID_KEYS = [:only, :except, :methods, :include, :root].freeze
SERIALIZABLE_HASH_VALID_KEYS = [:only, :except, :methods, :include].freeze
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

root still belongs here, though technically it is an option for as_json. This isn't about partitioning and need not be changed. In fact, it's an option in the ActiveModel::Serializers::JSON interface

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, will revert.

extend ActiveSupport::Autoload
autoload :Adapter
autoload :Null
Expand Down Expand Up @@ -114,15 +114,14 @@ def self.serialization_adapter_instance
@serialization_adapter_instance ||= ActiveModelSerializers::Adapter::Attributes
end

attr_accessor :object, :root, :scope
attr_accessor :object, :scope

# `scope_name` is set as :current_user by default in the controller.
# If the instance does not have a method named `scope_name`, it
# defines the method so that it calls the +scope+.
def initialize(object, options = {})
self.object = object
self.instance_options = options
self.root = instance_options[:root]
self.scope = instance_options[:scope]

scope_name = instance_options[:scope_name]
Expand Down Expand Up @@ -178,8 +177,7 @@ def serializable_hash(adapter_options = nil, options = {}, adapter_instance = se
# TODO: When moving attributes adapter logic here, @see #serializable_hash
# So that the below is true:
# @param options [nil, Hash] The same valid options passed to `as_json`
# (:root, :only, :except, :methods, and :include).
# The default for `root` is nil.
# (:only, :except, :methods, and :include).
# The default value for include_root is false. You can change it to true if the given
# JSON string includes a single root node.
def as_json(adapter_opts = nil)
Expand All @@ -188,7 +186,7 @@ def as_json(adapter_opts = nil)

# Used by adapter as resource root.
def json_key
root || _type || object.class.model_name.to_s.underscore
_type || object.class.model_name.to_s.underscore
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now done here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as https://github.com/rails-api/active_model_serializers/pull/1782/files#r66382756 I'm fine removing root as a public interface, or in the initializer, but it should still be an option to as_json

end

def read_attribute_for_serialization(attr)
Expand Down
4 changes: 1 addition & 3 deletions lib/active_model/serializer/collection_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ class CollectionSerializer
include Enumerable
delegate :each, to: :@serializers

attr_reader :object, :root
attr_reader :object
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to think if this needs a deprecation.


def initialize(resources, options = {})
@object = resources
@options = options
@root = options[:root]
@serializers = serializers_from_resources
end

Expand All @@ -37,7 +36,6 @@ def serializable_hash(adapter_options, options, adapter_instance)
# Disabling cop since it's good to highlight the complexity of this method by
# including all the logic right here.
def json_key
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love for this method to be simpler :(

return root if root
# 1. get from options[:serializer] for empty resource collection
key = object.empty? &&
(explicit_serializer_class = options[:serializer]) &&
Expand Down
4 changes: 0 additions & 4 deletions lib/active_model_serializers/adapter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ def fragment_cache(cached_hash, non_cached_hash)
def serialization_options(options)
options ||= {} # rubocop:disable Lint/UselessAssignment
end

def root
serializer.json_key.to_sym if serializer.json_key
end
end
end
end
8 changes: 8 additions & 0 deletions lib/active_model_serializers/adapter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ def serializable_hash(options = nil)
self.class.transform_key_casing!(serialized_hash, instance_options)
end

def root
if instance_options.key?(:root)
instance_options[:root]
elsif serializer.json_key
serializer.json_key.to_sym
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why .to_sym is called here, just transposed this.

end
end

def meta
instance_options.fetch(:meta, nil)
end
Expand Down
4 changes: 3 additions & 1 deletion lib/active_model_serializers/serializable_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

module ActiveModelSerializers
class SerializableResource
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links, :serialization_context, :key_transform])
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta,
:meta_key, :links, :root,
:serialization_context, :key_transform])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added :root to the list, plus line breaks for readability.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As opposed to https://github.com/rails-api/active_model_serializers/pull/1782/files#r66382756 👍 here since it's the adapter's job to build the JSON response. the serializer just provides 'attributes' and 'associations' and hints as to what it's root should be, when needed

include ActiveModelSerializers::Logging

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