mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Add tests for conditional attributes/associations.
This commit is contained in:
parent
7fbf7e536d
commit
7af198653d
@ -238,6 +238,29 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -4,7 +4,7 @@ module ActiveModel
|
|||||||
class Serializer
|
class Serializer
|
||||||
class AttributeTest < ActiveSupport::TestCase
|
class AttributeTest < ActiveSupport::TestCase
|
||||||
def setup
|
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)
|
@blog_serializer = AlternateBlogSerializer.new(@blog)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -95,6 +95,29 @@ module ActiveModel
|
|||||||
|
|
||||||
assert_equal(expected, hash)
|
assert_equal(expected, hash)
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user