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

@@ -22,6 +22,7 @@ module ActiveModel
autoload :Adapter
autoload :Null
autoload :Attribute
autoload :Link
autoload :Association
autoload :Reflection
autoload :BelongsToReflection
@@ -275,9 +276,14 @@ module ActiveModel
# link(:self) { "http://example.com/resource/#{object.id}" }
# @example
# link :resource, "http://example.com/resource"
# @example
# link(:callback, if: :internal?), { "http://example.com/callback" }
#
def self.link(name, value = nil, &block)
_links[name] = block || value
def self.link(name, *args, &block)
options = args.extract_options!
# For compatibility with the use cae of passing link directly as string argument
# without block, we are creating a wrapping block
_links[name] = Link.new(name, options, block || ->(_serializer) { args.first })
end
# Set the JSON API meta attribute of a serializer.