Skip to content

Prevent possible duplicated attributes in serializer #914

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

Merged
merged 1 commit into from
May 18, 2015
Merged
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
3 changes: 2 additions & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def self.inherited(base)
def self.attributes(*attrs)
attrs = attrs.first if attrs.first.class == Array
@_attributes.concat attrs
@_attributes.uniq!
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if we need to worry about the uniq! here

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, I can see 3 use cases:

  • a user calls attributes :title, :title
  • a user calls:
attributes :title
attributes :title
  • a future feature to call attributes in a similar way the fragment cache does

The first two are not really serious, but the last one might go unseen like the fragment cache.


attrs.each do |attr|
define_method attr do
Expand All @@ -42,7 +43,7 @@ def self.attributes(*attrs)
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)
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 @@ -24,6 +24,15 @@ def test_attribute_inheritance_with_key
adapter = ActiveModel::Serializer::Adapter::Json.new(blog_serializer)
assert_equal({:id=>1, :title=>"AMS Hints"}, adapter.serializable_hash)
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
9 changes: 9 additions & 0 deletions test/serializers/attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ def test_attribute_inheritance_with_new_attribute
assert_equal({id: 1, body: "ZOMG!!", date: "2015", likes: nil},
serializer.attributes)
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