-
Notifications
You must be signed in to change notification settings - Fork 140
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
scoping resource associations with state #9
Comments
Hey @BastianL, thanks for the kind words. I think there's a better answer here, but wanted to respond while I have a quick minute. One, you could pass this in with the request, since 0.6.x supports association filtering:
The filter is applied as normal, just like if we were writing the resource for allow_filter :user_id I think that's your best immediate-term solution. For a longer-term one, we actually have access to the controller from the resource. I use this for guards, currently: allow_filter :whatever, if: :allowed_for_user?
def allowed_for_user?
# code with current_user here
end That code is here: https://github.com/jsonapi-suite/jsonapi_compliable/blob/master/lib/jsonapi_compliable/scoping/filterable.rb#L14. So one way to give access to the current user is to evaluate all the scope procs within that context. That said, I'm not thrilled about the pattern and not sure I want to double down on it that much. I think there's a better solution, but that's what's top of my head at the moment. |
I have used the filter aspect of the jsonapi successfully in the past, but it seems lacking especially when dealing with has_and_belongs_to many tables, and don't want to expose the intermediate join table to your client. In above example
I want to to query |
HABTM is definitely a use case that can be improved. I've thought a lot about this and the short answer is, "it's on the list, but not a priority for me". I agree ideally the join table is not exposed, since this is an implementation detail. However, let's say you now add a column to the join table - a boolean like Really we want this data treated as metadata of the relationship, not a separate object. JSONAPI does support per-object metadata...but almost no library actually supports it and there's not a fully-fleshed out filter/sort/etc paradigm for it. I think it's a good long term direction but not one I'm ready to spend time embracing at the moment. There is a solution for your use case, though, I think:
If I'm understanding correctly, you could: allow_filter :user_id do |scope, value|
scope.joins(:users_to_bars).where("users_to_bars.user_id = ?", value)
end Which at least solves the immediate filter issue. All that said, I'm personally exposing my join tables at the moment. Yes, it's a little more verbose but at least everything is dead simple and easy to reason about. I'm working to release an isomorphic js client that works similar to ActiveRecord...I'm considering if adding a All that summed up - definitely on my radar, but I don't think exposing the join tables is prohibitive at the moment, just less than ideal. So I'm waiting to get more data before settling on a solution there. |
Another solution you might want to consider is middleware setting |
Thanks for the help! The filter block should actually do nicely, that wasn't on my radar. I'll check it out and close the issue soon. |
Hi Lee,
Thanks for the great set of libraries, it has greatly simplified writing a jsonapi compatible application.
Frequently I find myself scoping queries through some sort of user state, for example in this simplified situation:
I might want to scope all requests to the
Bar
index endpoint, to only return bars that are associated to thecurrent_user
. However, if I requestGET /foo?include=bar
I will still get otherBars
associated to theFoo
that are not relevant to the user.How would one accomplish this sort of relationship filtering with the
jsonapi_suite
? My initial thought would be to allow passing some sort of global scope intorender_jsonapi
which can be used by theassociation scope
in theFooResource
, similar to how ActiveRecord handles scopes:What are your thoughts on how this could be handled? I'm happy to implement a PR.
The text was updated successfully, but these errors were encountered: