mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Merge pull request #2279 from mkon/link-conditions
Support conditions in link statements
This commit is contained in:
commit
cb67434b46
@ -5,6 +5,7 @@
|
|||||||
Breaking changes:
|
Breaking changes:
|
||||||
|
|
||||||
Features:
|
Features:
|
||||||
|
- [#2279](https://github.com/rails-api/active_model_serializers/pull/2279) Support condition options in serializer link statements
|
||||||
|
|
||||||
Fixes:
|
Fixes:
|
||||||
|
|
||||||
|
|||||||
@ -238,6 +238,15 @@ link :other, 'https://example.com/resource'
|
|||||||
link(:posts) { link_author_posts_url(object) }
|
link(:posts) { link_author_posts_url(object) }
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Just like attributes, links also support conditions in options
|
||||||
|
```ruby
|
||||||
|
link(:secret, if: :internal?) { object.secret_link }
|
||||||
|
|
||||||
|
def internal?
|
||||||
|
instance_options[:context] == :internal
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
#### #object
|
#### #object
|
||||||
|
|
||||||
The object being serialized.
|
The object being serialized.
|
||||||
|
|||||||
@ -22,6 +22,7 @@ module ActiveModel
|
|||||||
autoload :Adapter
|
autoload :Adapter
|
||||||
autoload :Null
|
autoload :Null
|
||||||
autoload :Attribute
|
autoload :Attribute
|
||||||
|
autoload :Link
|
||||||
autoload :Association
|
autoload :Association
|
||||||
autoload :Reflection
|
autoload :Reflection
|
||||||
autoload :BelongsToReflection
|
autoload :BelongsToReflection
|
||||||
@ -275,9 +276,14 @@ module ActiveModel
|
|||||||
# link(:self) { "http://example.com/resource/#{object.id}" }
|
# link(:self) { "http://example.com/resource/#{object.id}" }
|
||||||
# @example
|
# @example
|
||||||
# link :resource, "http://example.com/resource"
|
# link :resource, "http://example.com/resource"
|
||||||
|
# @example
|
||||||
|
# link(:callback, if: :internal?), { "http://example.com/callback" }
|
||||||
#
|
#
|
||||||
def self.link(name, value = nil, &block)
|
def self.link(name, *args, &block)
|
||||||
_links[name] = block || value
|
options = args.extract_options!
|
||||||
|
# For compatibility with the use case 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
|
end
|
||||||
|
|
||||||
# Set the JSON API meta attribute of a serializer.
|
# Set the JSON API meta attribute of a serializer.
|
||||||
|
|||||||
21
lib/active_model/serializer/link.rb
Normal file
21
lib/active_model/serializer/link.rb
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
require 'active_model/serializer/field'
|
||||||
|
|
||||||
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
# Holds all the data about a serializer link
|
||||||
|
#
|
||||||
|
# @example
|
||||||
|
# class PostSerializer < ActiveModel::Serializer
|
||||||
|
# link :callback, if: :internal? do
|
||||||
|
# object.callback_link
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# def internal?
|
||||||
|
# instance_options[:internal] == true
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
class Link < Field
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -482,7 +482,8 @@ module ActiveModelSerializers
|
|||||||
# }.reject! {|_,v| v.nil? }
|
# }.reject! {|_,v| v.nil? }
|
||||||
def links_for(serializer)
|
def links_for(serializer)
|
||||||
serializer._links.each_with_object({}) do |(name, value), hash|
|
serializer._links.each_with_object({}) do |(name, value), hash|
|
||||||
result = Link.new(serializer, value).as_json
|
next if value.excluded?(serializer)
|
||||||
|
result = Link.new(serializer, value.block).as_json
|
||||||
hash[name] = result if result
|
hash[name] = result if result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -17,7 +17,21 @@ module ActiveModelSerializers
|
|||||||
link :yet_another do
|
link :yet_another do
|
||||||
"http://example.com/resource/#{object.id}"
|
"http://example.com/resource/#{object.id}"
|
||||||
end
|
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 }
|
link(:nil) { nil }
|
||||||
|
|
||||||
|
def instance_truth
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def instance_falsey
|
||||||
|
false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@ -85,7 +99,8 @@ module ActiveModelSerializers
|
|||||||
:"link-authors" => 'http://example.com/link_authors',
|
:"link-authors" => 'http://example.com/link_authors',
|
||||||
resource: 'http://example.com/resource',
|
resource: 'http://example.com/resource',
|
||||||
posts: 'http://example.com/link_authors/1337/posts',
|
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])
|
assert_equal(expected, hash[:data][:links])
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user