-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support conditions in link statements #2279
Conversation
aa1be1f
to
aef6acb
Compare
lib/active_model/serializer.rb
Outdated
_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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo :)
- use cae
+ use case
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 }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interesting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be ->(*) { args.first }
# end | ||
# end | ||
# | ||
class Link < Field |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice re-use
@@ -480,6 +480,10 @@ def relationships_for(serializer, requested_associations, include_slice) | |||
# }.reject! {|_,v| v.nil? } | |||
def links_for(serializer) | |||
serializer._links.each_with_object({}) do |(name, value), hash| | |||
if value.is_a?(ActiveModel::Serializer::Link) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it always a ActiveModel::Serializer::Link now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. Changed it.
if value.is_a?(ActiveModel::Serializer::Link) | ||
next if value.excluded?(serializer) | ||
value = value.block | ||
end | ||
result = Link.new(serializer, value).as_json |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems ripe for a refactor
Rebased. Looks like the tests are stable now in upstream 👍 |
Looks good. |
Hello,
First time contributor here, however have been using AMS happily for quite some time.
Purpose
Currently hiding certain links is a bit cumbersome, since the serializer's instance options are not available when the link block is evaluated by the (JSON-API) adapter.
With this change the
link()
statement in serializers supports the same condition options asattribute()
and is possible to hide certain links from being rendered.Eg I can now write a serializer like this:
Changes
ActiveModel::Serializer::Link
as a subclass ofField
to store options and handle conditionlinks_for
detectsActiveModel::Serializer::Link
, excludes them if necessary and converts them to the adapter'sLink
instances.