mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #914 from groyoh/fix_904
Prevent possible duplicated attributes in serializer
This commit is contained in:
commit
a59cc4c779
@ -31,6 +31,7 @@ module ActiveModel
|
|||||||
def self.attributes(*attrs)
|
def self.attributes(*attrs)
|
||||||
attrs = attrs.first if attrs.first.class == Array
|
attrs = attrs.first if attrs.first.class == Array
|
||||||
@_attributes.concat attrs
|
@_attributes.concat attrs
|
||||||
|
@_attributes.uniq!
|
||||||
|
|
||||||
attrs.each do |attr|
|
attrs.each do |attr|
|
||||||
define_method attr do
|
define_method attr do
|
||||||
@ -42,7 +43,7 @@ module ActiveModel
|
|||||||
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.concat [key]
|
@_attributes << key unless @_attributes.include?(key)
|
||||||
define_method key do
|
define_method key do
|
||||||
object.read_attribute_for_serialization(attr)
|
object.read_attribute_for_serialization(attr)
|
||||||
end unless method_defined?(key) || _fragmented.respond_to?(attr)
|
end unless method_defined?(key) || _fragmented.respond_to?(attr)
|
||||||
|
|||||||
@ -24,6 +24,15 @@ module ActiveModel
|
|||||||
adapter = ActiveModel::Serializer::Adapter::Json.new(blog_serializer)
|
adapter = ActiveModel::Serializer::Adapter::Json.new(blog_serializer)
|
||||||
assert_equal({:id=>1, :title=>"AMS Hints"}, adapter.serializable_hash)
|
assert_equal({:id=>1, :title=>"AMS Hints"}, adapter.serializable_hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_multiple_calls_with_the_same_attribute
|
||||||
|
serializer_class = Class.new(ActiveModel::Serializer) do
|
||||||
|
attribute :title
|
||||||
|
attribute :title
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal([:title], serializer_class._attributes)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -49,6 +49,15 @@ module ActiveModel
|
|||||||
assert_equal({id: 1, body: "ZOMG!!", date: "2015", likes: nil},
|
assert_equal({id: 1, body: "ZOMG!!", date: "2015", likes: nil},
|
||||||
serializer.attributes)
|
serializer.attributes)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_multiple_calls_with_the_same_attribute
|
||||||
|
serializer_class = Class.new(ActiveModel::Serializer) do
|
||||||
|
attributes :id, :title
|
||||||
|
attributes :id, :title, :title, :body
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal([:id, :title, :body], serializer_class._attributes)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user