Conversation
Scopes are special in that they don't have a predicate. This removes the check for the predicate, and thus makes scopes as filtering options work with JSONAPI.rb.
mamhoff
left a comment
There was a problem hiding this comment.
Thank you for taking this over! I like your solution, but I have a question regarding requiring the scope's name to be an allowed_field.
Also, thank you for maintaining these gems, they really help us!
| allow = predicates.any? || ( | ||
| predicates.none? && | ||
| scopes.include?(requested_field) && | ||
| allowed_fields.include?(requested_field) |
There was a problem hiding this comment.
Hm, given that we handle scopes separately already, I don't really see why we should add the scopes to the allowed_fields array as well. Most scopes will have names that don't directly match the name of a field, so I think this would be a little confusing.
However, I might be missing something as well!
There was a problem hiding this comment.
Most scopes will have names that don't directly match the name of a field, so I think this would be a little confusing.
Yup, I was thinking about it too and the only reason I'd leave it explicit, is because there might be scopes that can conflict with the field name. My concern is that we might accidentally enable a scope as filterable without the user being aware of it.
@fluxsaas would be nice to get your opinion on this too. 🙇
|
Any updates on this idea? Was just trying to do this today. |
|
Also just ran into this |
|
Dealing with this now, nice solution! |
|
I'll have to make it backwards compatible, but definitely could have it released by next week. Ty ty for bringing it all up! |
|
Checking in on this status as well. Any updates? |
|
Any updates? This would be very helpful to get merged. |
|
Just in case it may help anyone, my workaround for this is something like below: jsonapi_filter(User.all, [:created_before], options) do |filtered|
whitelisted_scopes_params = params[:filter].permit!.slice(*User.ransackable_scopes)
records = filtered.result.ransack(whitelisted_scopes_params).result
render jsonapi: records
end |
What is the current behavior?
ActiveRecord scopes can't be used in filters. This supersedes the #54
What is the new behavior?
You can use scopes for filtering as well, to enable scopes, use the option
flags:
Assuming your model
Userhas the following scope defined:This allows you to run queries like:
Checklist
Please make sure the following requirements are complete:
features)