mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Map attributes to Attribute values when defined in serializer
This commit is contained in:
parent
b3b9a46eeb
commit
87d18e9c32
@ -46,8 +46,8 @@ module ActiveModel
|
|||||||
|
|
||||||
with_options instance_writer: false, instance_reader: false do |serializer|
|
with_options instance_writer: false, instance_reader: false do |serializer|
|
||||||
class_attribute :_type, instance_reader: true
|
class_attribute :_type, instance_reader: true
|
||||||
class_attribute :_attributes # @api private : names of attribute methods, @see Serializer#attribute
|
class_attribute :serialized_attributes, instance_writer: false # @api public: maps attribute name to 'Attribute' function
|
||||||
self._attributes ||= []
|
self.serialized_attributes ||= {}
|
||||||
class_attribute :_attributes_keys # @api private : maps attribute value to explict key name, @see Serializer#attribute
|
class_attribute :_attributes_keys # @api private : maps attribute value to explict key name, @see Serializer#attribute
|
||||||
self._attributes_keys ||= {}
|
self._attributes_keys ||= {}
|
||||||
class_attribute :_links # @api private : links definitions, @see Serializer#link
|
class_attribute :_links # @api private : links definitions, @see Serializer#link
|
||||||
@ -69,11 +69,11 @@ module ActiveModel
|
|||||||
serializer.class_attribute :_cache_digest # @api private : Generated
|
serializer.class_attribute :_cache_digest # @api private : Generated
|
||||||
end
|
end
|
||||||
|
|
||||||
# Serializers inherit _attributes and _attributes_keys.
|
# Serializers inherit serialized_attributes and _attributes_keys.
|
||||||
# Generates a unique digest for each serializer at load.
|
# Generates a unique digest for each serializer at load.
|
||||||
def self.inherited(base)
|
def self.inherited(base)
|
||||||
caller_line = caller.first
|
caller_line = caller.first
|
||||||
base._attributes = _attributes.dup
|
base.serialized_attributes = serialized_attributes.dup
|
||||||
base._attributes_keys = _attributes_keys.dup
|
base._attributes_keys = _attributes_keys.dup
|
||||||
base._links = _links.dup
|
base._links = _links.dup
|
||||||
base._cache_digest = digest_caller_file(caller_line)
|
base._cache_digest = digest_caller_file(caller_line)
|
||||||
@ -91,6 +91,10 @@ module ActiveModel
|
|||||||
_links[name] = block || value
|
_links[name] = block || value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self._attributes
|
||||||
|
serialized_attributes.keys
|
||||||
|
end
|
||||||
|
|
||||||
# @example
|
# @example
|
||||||
# class AdminAuthorSerializer < ActiveModel::Serializer
|
# class AdminAuthorSerializer < ActiveModel::Serializer
|
||||||
# attributes :id, :name, :recent_edits
|
# attributes :id, :name, :recent_edits
|
||||||
@ -102,6 +106,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO: remove the dynamic method definition
|
||||||
# @example
|
# @example
|
||||||
# class AdminAuthorSerializer < ActiveModel::Serializer
|
# class AdminAuthorSerializer < ActiveModel::Serializer
|
||||||
# attributes :id, :recent_edits
|
# attributes :id, :recent_edits
|
||||||
@ -109,15 +114,17 @@ module ActiveModel
|
|||||||
#
|
#
|
||||||
# def recent_edits
|
# def recent_edits
|
||||||
# object.edits.last(5)
|
# object.edits.last(5)
|
||||||
# enr
|
# end
|
||||||
def self.attribute(attr, options = {})
|
def self.attribute(attr, options = {})
|
||||||
key = options.fetch(:key, attr)
|
key = options.fetch(:key, attr)
|
||||||
_attributes_keys[attr] = { key: key } if key != attr
|
_attributes_keys[attr] = { key: key } if key != attr
|
||||||
_attributes << key unless _attributes.include?(key)
|
_attributes << key unless _attributes.include?(key)
|
||||||
|
|
||||||
|
serialized_attributes[key] = ->(object) { object.read_attribute_for_serialization(attr) }
|
||||||
|
|
||||||
ActiveModelSerializers.silence_warnings do
|
ActiveModelSerializers.silence_warnings do
|
||||||
define_method key do
|
define_method key do
|
||||||
object.read_attribute_for_serialization(attr)
|
serialized_attributes[key].call(object)
|
||||||
end unless method_defined?(key) || _fragmented.respond_to?(attr)
|
end unless method_defined?(key) || _fragmented.respond_to?(attr)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -249,12 +256,12 @@ module ActiveModel
|
|||||||
def attributes(requested_attrs = nil)
|
def attributes(requested_attrs = nil)
|
||||||
self.class._attributes.each_with_object({}) do |name, hash|
|
self.class._attributes.each_with_object({}) do |name, hash|
|
||||||
next unless requested_attrs.nil? || requested_attrs.include?(name)
|
next unless requested_attrs.nil? || requested_attrs.include?(name)
|
||||||
if self.class._fragmented
|
hash[name] = read_attribute_for_serialization(name)
|
||||||
hash[name] = self.class._fragmented.public_send(name)
|
|
||||||
else
|
|
||||||
hash[name] = send(name)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def read_attribute_for_serialization(key)
|
||||||
|
self.class._fragmented ? self.class._fragmented.public_send(key) : send(key)
|
||||||
end
|
end
|
||||||
|
|
||||||
# @api private
|
# @api private
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user