-
Notifications
You must be signed in to change notification settings - Fork 325
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
add ability to wrap "where" clause #519
Comments
hi @noctivityinc there are a couple of ways to achieve this:
Some examples: Mutate parameters by redefining https://github.com/rooftopcms/rooftop-ruby/blob/master/lib/rooftop/queries/queries.rb#L11 In this example we're mutating parameters to be inside a Another example of redefining https://github.com/rooftopcms/rooftop-rails/blob/master/lib/rooftop/rails/relation_cache.rb#L8 Define a middleware to mutate parameters This is lower-level. Here's an example - in this case we pass arguments into https://github.com/rooftopcms/rooftop-ruby/blob/master/lib/rooftop/middleware/embed_middleware.rb This particular example is to get around a limitation in Her that underscored arguments to Hope this helps. Ed |
So continuing this thread, if we want to add our own class method, where exactly would we add it? For example, if I want to add support for a
|
Scratch that. I figured it out. For anyone else: Add your method here:
and then define it as a class method here:
So, for example, to add support for a
|
Glad you found a solution 😺 Normal usage for introducing common scopes would be inheritance or some sort of mixin. Is the monkey-patch based on your original need to override Going to close this issue now. Please let us know if you have any further questions! |
Right now, when you call the API with a where statement -
User.where(name: 'Josh')
The server sees the request like this:
https://www.example.com/api/users?name=Josh
This is all well and good but if you have a large model and want to be able to filter on ANY of the attributes, you would need to create a complex method to handle the filtering (i.e. loop through all attributes and compare to the querystring to see if one of the keys match).
It would be cleaner if we could wrap the where filters in a root level attribute, like
filter
or whatever we want. So this:User.where(name: 'Josh')
would appear like this:
https://www.example.com/api/users?filter[name]=Josh
and in the code we can check to see if the filter param was passed and all and then act ONLY on the keys passed.
The text was updated successfully, but these errors were encountered: