Feature/custom links defined per resource#486
Feature/custom links defined per resource#486akharris wants to merge 5 commits intoJSONAPI-Resources:masterfrom
Conversation
There was a problem hiding this comment.
The warning refers to the if option but the check is for the with option. Is this intended?
Also, this line isn't being covered in the tests. We should add a test for it, or wrap it with # :nocov: directives.
There was a problem hiding this comment.
yeah that's a typo, one that would've been caught with the spec I didn't write 😃
I'll add a spec that catches when a custom link type is made with no with lambda.
|
This example class ArticleResource < JSONAPI::Resource
attributes :title, :body, :published_at
custom_link :original_source, :custom, with: ->(instance) { instance.original_source }
enddoesn't work, in my experience, unless In order to get my custom next-record and previous-record links to work, it was necessary to call methods on the model directly: class ProjectResource < JSONAPI::Resource
u = Rails.application.routes.url_helpers
custom_link :next, :custom, with: ->(i) { u.project_url(i.model.next.presence) if i.model.next }
custom_link :previous, :custom, with: ->(i) { u.project_url(i.model.previous) if i.model.previous }
end |
There was a problem hiding this comment.
Do we need the instance level custom_links and custom_links? methods? It seems we could just directly access the class level methods.
There was a problem hiding this comment.
probably not. In ResourceSerializer#relationship_links I call source.custom_links? but that could just as easily be changed to source.class.custom_links?
There was a problem hiding this comment.
Thanks. Any methods we put at the instance level limit the available names for attributes.
|
@beechnut good catch, I updated the top comment with your corrections. 👍 I think the following should work as well. class ProjectResource < JSONAPI::Resource
u = Rails.application.routes.url_helpers
delegate :presence, to: :model
delegate :next, to: :model
delegate :previous, to: :model
custom_link :next, :custom, with: ->(i) { u.project_url(i.next) if i.next }
custom_link :previous, :custom, with: ->(i) { u.project_url(i.previous) if i.previous }
end |
5e8802a to
60623e4
Compare
|
sorry I've been taking some time off lately and will be offline the next two weeks. I'll resume work on this PR when I get back (unless someone else has added the same feature). |
…esource instance to build a custom url
60623e4 to
b6c9444
Compare
…at takes a JSONAPI::Resource instance and a link_builder
|
In the new resource level meta feature I'm passing in |
|
@lgebhardt I'm back from my vacation. I'll look into incorporating |
based on conversation in #464 and some code I managed to extract from a recent spike using this library.
cc @ceritium
Overview
I've made some modifications since my initial PR. I've simplified the custom link building interface so that now the
custom_linkmethod simply takes aJSONAPI::Resourceinstance and aLinkBuilderobject. I'm open to reverting to an older version of the PR. This current implementation does depend on knowing how to use internal parts of the gem since it largely relies on the passed-in link builder object.I've also backtracked on having code that adds a link with an if condition. I think it's much cleaner to return a null for a custom link than it is to remove the entire node, and we can always re-add the capability to completely add/remove custom links based on Resource instance-level business logic.
Examples
In this example, the url simply hangs off the the normal 'self' link.
generates the following
Slightly more complicated custom link
generates
Here are a couple of contrived examples that use lambdas.