Comments (9)
Hey there, @griley!
As you mentioned, JSON::Utils
only applies equality filters – like the one in the example – automatically. Unfortunately JU
doesn't provide any fancy macro to customize filters since it doesn't assume any responsibility over your controller action's operation.
For example, in our production JSON API, when we need to apply a very specific filter, like filtering records by a given scope, we apply that filter at the interactor/service object level. The reason for such decision is that we understand that it's a good pattern do make non-trivial filters explicit in the context where the operation of the action happens.
Hope it helps you and if you have any other ideas, just let me know.
from jsonapi-utils.
Thanks @tiagopog . I agree with the decision to make the filters explicit.
However, the problem I'm running into (and the origin of the issue above) is that the default equality filter is still scoped to any custom filtering I may do in a controller.
For example:
class UserResource < JSONAPI::Resource
attributes :first_name, :last_name, :full_name
has_many :posts
# I'd like to expose a filter on a derived attribute
filter :full_name
def full_name
"#{@model.first_name} #{@model.last_name}"
end
end
class UsersController < BaseController
# GET /users
def index
jsonapi_render json: filtered_users
end
private
def filtered_users
first_name, last_name = params[:filter][:full_name].split
User.where(first_name: first_name, last_name: last_name)
end
end
When filtered_users
is run above, the scope implied by the filter in UserResource
is also applied to it. Resulting in:
User.where(first_name: first_name, last_name: last_name).where(full_name: params[:filter][:full_name])
Obviously, this is a problem because the attribute full_name
doesn't actually exist.
Any suggestion on a way around this or a different approach?
from jsonapi-utils.
I guess I could unscope(where: :full_name)
in filtered_users
but that smells a bit to me.
from jsonapi-utils.
Ok, now I gotcha! In this case you might simply skip the default equality filter: jsonapi_render json: filtered_users, options: { filter: false }
from jsonapi-utils.
You might apply the same to pagination (options: { paginate: false }
). I'm sorry for the lack of documentation for this feature. I may write something covering this case in the README.md
later.
from jsonapi-utils.
Thanks for the quick response @tiagopog and pointers!
The jsonapi_render json: filtered_users, options: { filter: false }
addressed my issue.
from jsonapi-utils.
I finally realized why Tiago suggested setting a pagination option after I enabled the pagination feature in the initializer:
config.default_paginator = :paged
The record count information applies the same default filter to it. But even after adding the paginate: false
option, it continued to occur. I sniffed around the code a little bit and instead tried to add my own custom record count:
jsonapi_render json: filtered_users, options: { filter: false , count: filtered_users.length }
However, this line of code is thwarting that feature because it's overwriting the options I passed in.
from jsonapi-utils.
@griley, thanks for reporting it. I might take a look on this possible bug later as well as I'll try to remember if there was any particular motivation behind this line of code.
from jsonapi-utils.
On a side note...
If the options
weren't reinitialized, the apply_filters(records, options)
found here would cause the record count to be calculated correctly because of the filter: false
This would obviate the need for a count
option in this issue.
Thanks, and good luck on Sunday in the football match vs Germany ;)
from jsonapi-utils.
Related Issues (20)
- Trying to override pagination...
- Decimals formatted as strings in json response
- Update jsonapi-resources 0.9.12 HOT 10
- Bump the rails version to 6.1 for tagged version 7.13.0 HOT 1
- Release a new version? HOT 1
- Support for operation processors? HOT 3
- Rendering scopes with `includes` is slow for no reason HOT 2
- Passing resource relationship in POST request HOT 1
- resource_params => NoMethodError: undefined method `keys' for nil:NilClass HOT 2
- Render multiple invalid AR objects
- Bring support to fast_jsonapi HOT 9
- Use the cache with jsonapi_render. HOT 1
- Getting Invalid resource for custom resource response on patch request
- Non-REST actions?
- JSONAPI::Utils::Exceptions::BadRequest undefined
- Relax jsonapi-resources dependency HOT 3
- Support for sorting and pagination from JR
- jsonapi/utils.rb:9:in `include': wrong number of arguments (given 0, expected 1+) (ArgumentError)
- Formatting of error's "detail" attribute
- Accept and Content-Type headers are not verified
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonapi-utils.