mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
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:
commit
e3bfd07ac4
@ -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
|
||||
|
||||
@ -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})
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user