Skip to content
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

Merged
merged 3 commits into from
Oct 31, 2018
Merged

Conversation

mkon
Copy link

@mkon mkon commented Sep 1, 2018

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 as attribute() and is possible to hide certain links from being rendered.

Eg I can now write a serializer like this:

link(:private, if: -> { internal? }) do
  object.private_link
end

Changes

  • Added ActiveModel::Serializer::Link as a subclass of Field to store options and handle condition
  • JSON-API apater's links_for detects ActiveModel::Serializer::Link, excludes them if necessary and converts them to the adapter's Link instances.

@mkon mkon force-pushed the link-conditions branch 2 times, most recently from aa1be1f to aef6acb Compare September 1, 2018 11:31
_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
Copy link

@ghost ghost Sep 5, 2018

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 })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting

Copy link
Member

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
Copy link
Member

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)
Copy link
Member

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?

Copy link
Author

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
Copy link
Member

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

@mkon
Copy link
Author

mkon commented Oct 25, 2018

Rebased. Looks like the tests are stable now in upstream 👍

@bf4
Copy link
Member

bf4 commented Oct 31, 2018

Looks good.

@bf4 bf4 merged commit cb67434 into rails-api:0-10-stable Oct 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants