mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Handle conflicts between key names and serializer methods
As an example, all serializers implement `#object` as a reference to the object being esrialized, but this was preventing adding a key to the serialized representation with the `object` name. Instead of having attributes directly map to methods on the serializer, we introduce one layer of abstraction: the `_attributes_map`. This hash maps the key names expected in the output to the names of the implementing methods. This simplifies some things (removing the need to maintain both `_attributes` and `_attribute_keys`), but does add some complexity in order to support overriding attributes by defining methods on the serializer. It seems that with the addition of the inline-block format, we may want to remove the usage of programatically defining methods on the serializer for this kind of customization.
This commit is contained in:
committed by
Benjamin Fleischer
parent
e2903643c5
commit
7bde7bf752
@@ -43,6 +43,15 @@ module ActiveModel
|
||||
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
|
||||
|
||||
def test_type_attribute
|
||||
attribute_serializer = Class.new(ActiveModel::Serializer) do
|
||||
attribute :id, key: :type
|
||||
|
||||
Reference in New Issue
Block a user