jbuilder
jbuilder copied to clipboard
ignore! function added
Use case:
I had to write checks on each line to ignore keys with empty array or empty string values.
some_list = []
Jbuilder.encode do |json|
json.ignore_nil?
json.string ''.presence
json.array some_list if !some_list.empty?
end
the above code now becomes:
some_list = []
Jbuilder.encode do |json|
json.ignore! { |value| value.blank? }
json.string ''
json.array some_list
end
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @rwz (or someone else) soon.
If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.
Please see the contribution instructions for more information.
@abhiQmar Doesn't jbuilder already check for blank values?
@nateberkopec No it doesn't, it makes sense if someone wants to blank arrays, nils and strings(for consistency) in their json output. But if you do not want any blank arrays or empty strings (my use case), you would have to put a check on each and every key-value pair. There is a method(ignore_nil!) to ignore the keys with nil values. I am just trying to extend this functionality so that we can check for anything on our keys cleanly, by sending a block [which if returns true will ignore the key-value pair].