Skip to content

Enable use of an object key in serilaized forms #1354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
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
40 changes: 25 additions & 15 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ def self.digest_caller_file(caller_line)

with_options instance_writer: false, instance_reader: false do |serializer|
class_attribute :_type, instance_reader: true
class_attribute :_attributes # @api private : names of attribute methods, @see Serializer#attribute
self._attributes ||= []
class_attribute :_attributes_keys # @api private : maps attribute value to explict key name, @see Serializer#attribute
self._attributes_keys ||= {}
class_attribute :_attributes_map # @api private : maps attribute key names to names to names of implementing methods, @see Serializer#attribute
self._attributes_map ||= {}
class_attribute :_links # @api private : links definitions, @see Serializer#link
self._links ||= {}

Expand All @@ -69,12 +67,11 @@ def self.digest_caller_file(caller_line)
serializer.class_attribute :_cache_digest # @api private : Generated
end

# Serializers inherit _attributes and _attributes_keys.
# Serializers inherit _attributes_map
# Generates a unique digest for each serializer at load.
def self.inherited(base)
caller_line = caller.first
base._attributes = _attributes.dup
base._attributes_keys = _attributes_keys.dup
base._attributes_map = _attributes_map.dup
base._links = _links.dup
base._cache_digest = digest_caller_file(caller_line)
super
Expand Down Expand Up @@ -112,16 +109,29 @@ def self.attributes(*attrs)
# enr
def self.attribute(attr, options = {})
key = options.fetch(:key, attr)
_attributes_keys[attr] = { key: key } if key != attr
_attributes << key unless _attributes.include?(key)
_attributes_map[key] = attr

ActiveModelSerializers.silence_warnings do
define_method key do
define_method attr do
object.read_attribute_for_serialization(attr)
end unless method_defined?(key) || _fragmented.respond_to?(attr)
end unless method_defined?(attr) || _fragmented.respond_to?(attr)
end
end

# @api private
# An accessor for the old _attributes internal API
def self._attributes
_attributes_map.keys
end

# @api private
# An accessor for the old _attributes_keys internal API
def self._attributes_keys
Copy link
Contributor

Choose a reason for hiding this comment

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

can you provide an example in the method comment about what this method does (before and after).

This is to help future developers debug more painlessly

_attributes_map
.select { |key, impl| key != impl }
.each_with_object({}) { |(key, impl), acc| acc[impl] = { key: key } }
end

# @api private
# Used by FragmentCache on the CachedSerializer
# to call attribute methods on the fragmented cached serializer.
Expand Down Expand Up @@ -247,12 +257,12 @@ def json_key
# Return the +attributes+ of +object+ as presented
# by the serializer.
def attributes(requested_attrs = nil)
self.class._attributes.each_with_object({}) do |name, hash|
next unless requested_attrs.nil? || requested_attrs.include?(name)
self.class._attributes_map.each_with_object({}) do |(key, attr), hash|
next unless requested_attrs.nil? || requested_attrs.include?(key)
if self.class._fragmented
hash[name] = self.class._fragmented.public_send(name)
hash[key] = self.class._fragmented.public_send(attr)
else
hash[name] = send(name)
hash[key] = send(attr)
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions test/serializers/attribute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ def test_id_attribute_override
assert_equal({ blog: { id: 'AMS Hints' } }, adapter.serializable_hash)
end

def test_object_attribute_override
serializer = Class.new(ActiveModel::Serializer) do
attribute :name, key: :object
end

adapter = ActiveModel::Serializer::Adapter::Json.new(serializer.new(@blog))
assert_equal({ blog: { object: 'AMS Hints' } }, adapter.serializable_hash)
end

Copy link
Member

Choose a reason for hiding this comment

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

nice

def test_type_attribute
attribute_serializer = Class.new(ActiveModel::Serializer) do
attribute :id, key: :type
Expand Down