Skip to content

Add links conditionally #126

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

Closed
zubin opened this issue May 2, 2024 · 1 comment
Closed

Add links conditionally #126

zubin opened this issue May 2, 2024 · 1 comment

Comments

@zubin
Copy link

zubin commented May 2, 2024

My use case is to only include possible actions on a resource.

I want to omit ones which aren't relevant, depending on the resource state.

Something like this:

link :boom do
  if @object.can_go_boom?
    href "the-boom-url"
  end
end

Currently this results in links: {boom: nil} but I'd prefer any links which are nil to be removed (like Hash#compact).

Does this make sense? I'd be happy to make a PR if the idea is sound.

@zubin
Copy link
Author

zubin commented May 2, 2024

Closing because I found a way to do it:

extend JSONAPI::Serializable::Resource::ConditionalFields

link :boom, if: -> { @object.can_go_boom? } do
  href "the-boom-url"
end

Couldn't see it in the docs, came across it here:
#49

@zubin zubin closed this as completed May 2, 2024
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

No branches or pull requests

1 participant