Add tests for conditional attributes/associations.

This commit is contained in:
Lucas Hosseini 2016-01-12 13:42:44 +01:00
parent 7fbf7e536d
commit 7af198653d
2 changed files with 47 additions and 1 deletions

View File

@ -238,6 +238,29 @@ module ActiveModel
end
end
end
def test_conditional_associations
serializer = Class.new(ActiveModel::Serializer) do
belongs_to :if_assoc_included, if: :true
belongs_to :if_assoc_excluded, if: :false
belongs_to :unless_assoc_included, unless: :false
belongs_to :unless_assoc_excluded, unless: :true
def true
true
end
def false
false
end
end
model = ::Model.new
hash = serializable(model, serializer: serializer).serializable_hash
expected = { if_assoc_included: nil, unless_assoc_included: nil }
assert_equal(expected, hash)
end
end
end
end

View File

@ -4,7 +4,7 @@ module ActiveModel
class Serializer
class AttributeTest < ActiveSupport::TestCase
def setup
@blog = Blog.new({ id: 1, name: 'AMS Hints', type: 'stuff' })
@blog = Blog.new(id: 1, name: 'AMS Hints', type: 'stuff')
@blog_serializer = AlternateBlogSerializer.new(@blog)
end
@ -95,6 +95,29 @@ module ActiveModel
assert_equal(expected, hash)
end
def test_conditional_attributes
serializer = Class.new(ActiveModel::Serializer) do
attribute :if_attribute_included, if: :true
attribute :if_attribute_excluded, if: :false
attribute :unless_attribute_included, unless: :false
attribute :unless_attribute_excluded, unless: :true
def true
true
end
def false
false
end
end
model = ::Model.new
hash = serializable(model, serializer: serializer).serializable_hash
expected = { if_attribute_included: nil, unless_attribute_included: nil }
assert_equal(expected, hash)
end
end
end
end