Support conditions in link statements

This commit is contained in:
Konstantin Munteanu
2018-09-01 13:30:51 +02:00
parent 05e5ba6939
commit 0d3999c36d
6 changed files with 59 additions and 3 deletions

View File

@@ -17,7 +17,21 @@ module ActiveModelSerializers
link :yet_another do
"http://example.com/resource/#{object.id}"
end
link :conditional1, if: -> { instance_truth } do
"http://example.com/conditional1/#{object.id}"
end
link :conditional2, if: :instance_falsey do
"http://example.com/conditional2/#{object.id}"
end
link(:nil) { nil }
def instance_truth
true
end
def instance_falsey
false
end
end
def setup
@@ -85,7 +99,8 @@ module ActiveModelSerializers
:"link-authors" => 'http://example.com/link_authors',
resource: 'http://example.com/resource',
posts: 'http://example.com/link_authors/1337/posts',
:"yet-another" => 'http://example.com/resource/1337'
:"yet-another" => 'http://example.com/resource/1337',
conditional1: 'http://example.com/conditional1/1337'
}
assert_equal(expected, hash[:data][:links])
end