-
Notifications
You must be signed in to change notification settings - Fork 64
Add configurations/hooks for fields, include, and links. #49
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
Conversation
lib/jsonapi/rails/configuration.rb
Outdated
| }.freeze | ||
| } | ||
|
|
||
| DEFAULT_JSONAPI_FIELDS = ->() { params[:fields] } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid this won't work out of the box.
params[:fields] would return AC::Parameters, which is not compatible with Hash in Rails 5. So we can use something like params[:fields]&.to_unsafe_hash instead. But this won't work either because fields for every resource specified still need to be parsed (for instance, a request to /api/posts?fields[posts]=title,summary&fields[comments]=content,rating will result in a :fields hash { "posts" => "title,summary", "comments" => "content,rating" }, which will cause an error if passed to the renderer).
So I believe it will be safe to disable parsing/processing of JSONAPI fields by default (set it to nil or an empty hash) and provide a parser, just like you did with jsonapi_include.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the PR according to your feedback. At the moment I simply provide an example in the initializer but I agree it might make sense in the future to:
- provide
Hash[fields_param.map { |k, v| [k.to_sym, v.split(',').map!(&:to_sym)] }]as a method somewhere, - expose some mechanism to parse the
fieldsparam by default without necessarily using it (i.e. make it into a hash and turn it back to anAC::Parametersinstance that the user can directly validate).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, I like it. I can test this PR (the fields part) in an hour on my website.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested jsonapi_fields configuration, works without any issue.
5eb9ae0 to
6e66a0a
Compare
6e66a0a to
6b323ae
Compare
910d321 to
5ee0c4b
Compare
lib/jsonapi/rails/configuration.rb
Outdated
| }.freeze | ||
| } | ||
|
|
||
| DEFAULT_JSONAPI_FIELDS = ->() { params[:fields] } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested jsonapi_fields configuration, works without any issue.
Closes #50.