Prevent possible duplicated attributes

Calling ActiveModel::Serializer.attributes or ActiveModel::Serializer.attribute
methods multiple times won't create duplicated attributes anymore.
This commit is contained in:
groyoh
2015-05-18 22:14:36 +02:00
parent d981ee5106
commit 5393e5d235
3 changed files with 20 additions and 1 deletions

View File

@@ -31,6 +31,7 @@ module ActiveModel
def self.attributes(*attrs)
attrs = attrs.first if attrs.first.class == Array
@_attributes.concat attrs
@_attributes.uniq!
attrs.each do |attr|
define_method attr do
@@ -42,7 +43,7 @@ module ActiveModel
def self.attribute(attr, options = {})
key = options.fetch(:key, attr)
@_attributes_keys[attr] = {key: key} if key != attr
@_attributes.concat [key]
@_attributes << key unless @_attributes.include?(key)
define_method key do
object.read_attribute_for_serialization(attr)
end unless method_defined?(key) || _fragmented.respond_to?(attr)