Allow specifying attributes with a block

Adapted from https://github.com/rails-api/active_model_serializers/pull/1262
This commit is contained in:
Benjamin Fleischer
2015-11-30 16:45:17 -06:00
parent 87d18e9c32
commit 6020450fe4
2 changed files with 26 additions and 4 deletions

View File

@@ -71,6 +71,21 @@ module ActiveModel
assert_equal('custom', hash[:blog][:id])
end
PostWithVirtualAttribute = Class.new(::Model)
class PostWithVirtualAttributeSerializer < ActiveModel::Serializer
attribute :name do
"#{object.first_name} #{object.last_name}"
end
end
def test_virtual_attribute_block
post = PostWithVirtualAttribute.new(first_name: 'Lucas', last_name: 'Hosseini')
hash = serializable(post).serializable_hash
expected = { name: 'Lucas Hosseini' }
assert_equal(expected, hash)
end
end
end
end