Skip to content
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

ignore! function added #361

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

ignore! function added #361

wants to merge 1 commit into from

Conversation

abhiQmar
Copy link

@abhiQmar abhiQmar commented Oct 8, 2016

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

@rails-bot
Copy link

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.

lib/jbuilder.rb Outdated
# or empty array, you can pass a block.
#
# Example:
# json.ignore { |value| value.nil? }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these say ignore!?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it should. Thanks for pointing that out.

@nateberkopec
Copy link

@abhiQmar Doesn't jbuilder already check for blank values?

@abhiQmar
Copy link
Author

@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].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants