Merge pull request #161 from kevins90/fix_attr_name_as_serializer_underscored_prefix

Fix serialization of attribute whose name matches the serializer prefix underscored
This commit is contained in:
Steve Klabnik 2013-01-05 14:09:15 -08:00
commit e3bfd07ac4
3 changed files with 19 additions and 2 deletions

View File

@ -226,7 +226,6 @@ module ActiveModel
name = klass.name.demodulize.underscore.sub(/_serializer$/, '')
klass.class_eval do
alias_method name.to_sym, :object
root name.to_sym unless self._root == false
end
end

View File

@ -29,6 +29,17 @@ class SerializerTest < ActiveModel::TestCase
}, hash)
end
def test_attribute_method_with_name_as_serializer_prefix
object = SomeObject.new("something")
object_serializer = SomeSerializer.new(object, {})
hash = object_serializer.as_json
assert_equal({
:some => { :some => "something" }
}, hash)
end
def test_serializer_receives_scope
user = User.new
user_serializer = UserSerializer.new(user, :scope => {:scope => true})

View File

@ -63,7 +63,7 @@ class MyUserSerializer < ActiveModel::Serializer
def serializable_hash
hash = attributes
hash = hash.merge(:super_user => true) if my_user.super_user?
hash = hash.merge(:super_user => true) if object.super_user?
hash
end
end
@ -143,6 +143,13 @@ class CustomBlogSerializer < ActiveModel::Serializer
has_one :public_user, :key => :user, :serializer => UserSerializer
end
class SomeSerializer < ActiveModel::Serializer
attributes :some
end
class SomeObject < Struct.new(:some)
end
# Set up some classes for polymorphic testing
class Attachment < Model
def attachable