mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
changes to be able to specify multiple attributes with keys
This commit is contained in:
parent
1c28577280
commit
2d2094b588
@ -69,10 +69,15 @@ module ActiveModel
|
|||||||
class << self
|
class << self
|
||||||
# Define attributes to be used in the serialization.
|
# Define attributes to be used in the serialization.
|
||||||
def attributes(*attrs)
|
def attributes(*attrs)
|
||||||
|
|
||||||
self._attributes = _attributes.dup
|
self._attributes = _attributes.dup
|
||||||
|
|
||||||
attrs.each do |attr|
|
attrs.each do |attr|
|
||||||
attribute attr
|
if Hash === attr
|
||||||
|
attr.each {|attr_real, key| attribute attr_real, :key => key }
|
||||||
|
else
|
||||||
|
attribute attr
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -29,6 +29,28 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
}, hash)
|
}, hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_attributes_method_specifying_keys
|
||||||
|
user = User.new
|
||||||
|
user_serializer = UserAttributesWithKeySerializer.new(user, :scope => {})
|
||||||
|
|
||||||
|
hash = user_serializer.as_json
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
:user_attributes_with_key => { :f_name => "Jose", :l_name => "Valim", :ok => true }
|
||||||
|
}, hash)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_attributes_method_specifying_some_keys
|
||||||
|
user = User.new
|
||||||
|
user_serializer = UserAttributesWithSomeKeySerializer.new(user, :scope => {})
|
||||||
|
|
||||||
|
hash = user_serializer.as_json
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
:user_attributes_with_some_key => { :first_name => "Jose", :l_name => "Valim", :ok => true }
|
||||||
|
}, hash)
|
||||||
|
end
|
||||||
|
|
||||||
def test_attribute_method_with_name_as_serializer_prefix
|
def test_attribute_method_with_name_as_serializer_prefix
|
||||||
object = SomeObject.new("something")
|
object = SomeObject.new("something")
|
||||||
object_serializer = SomeSerializer.new(object, {})
|
object_serializer = SomeSerializer.new(object, {})
|
||||||
|
|||||||
@ -54,6 +54,22 @@ class UserSerializer < ActiveModel::Serializer
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class UserAttributesWithKeySerializer < ActiveModel::Serializer
|
||||||
|
attributes :first_name => :f_name, :last_name => :l_name
|
||||||
|
|
||||||
|
def serializable_hash
|
||||||
|
attributes.merge(:ok => true).merge(options[:scope])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class UserAttributesWithSomeKeySerializer < ActiveModel::Serializer
|
||||||
|
attributes :first_name, :last_name => :l_name
|
||||||
|
|
||||||
|
def serializable_hash
|
||||||
|
attributes.merge(:ok => true).merge(options[:scope])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class DefaultUserSerializer < ActiveModel::Serializer
|
class DefaultUserSerializer < ActiveModel::Serializer
|
||||||
attributes :first_name, :last_name
|
attributes :first_name, :last_name
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user