Skip to content

Commit

Permalink
Merge pull request #1195 from beauby/fix-id-override
Browse files Browse the repository at this point in the history
Fix id override
  • Loading branch information
NullVoxPopuli committed Sep 23, 2015
2 parents a54d237 + e552e43 commit f2d0a91
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def self.attribute(attr, options = {})
ActiveModelSerializers.silence_warnings do
define_method key do
object.read_attribute_for_serialization(attr)
end unless (key != :id && method_defined?(key)) || _fragmented.respond_to?(attr)
end unless method_defined?(key) || _fragmented.respond_to?(attr)
end
end

Expand Down
14 changes: 14 additions & 0 deletions test/serializers/attribute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ def test_type_attribute
adapter = ActiveModel::Serializer::Adapter::Json.new(attributes_serializer.new(@blog))
assert_equal({ blog: { type: 'stuff' } }, adapter.serializable_hash)
end

def test_id_attribute_override_before
serializer = Class.new(ActiveModel::Serializer) do
def id
'custom'
end

attribute :id
end

hash = ActiveModel::SerializableResource.new(@blog, adapter: :json, serializer: serializer).serializable_hash

assert_equal('custom', hash[:blog][:id])
end
end
end
end

0 comments on commit f2d0a91

Please sign in to comment.