jbuilder icon indicating copy to clipboard operation
jbuilder copied to clipboard

Ignore! block

Open abhiQmar opened this issue 9 years ago • 3 comments

Just like ignore_nil! , can we have ignore! method which takes in a block.

This can be useful if we don't want (empty strings, empty array, etc) in our JSON output. I think the block will be really useful.

What are your thoughts on this. If it seems like a good idea, I can start working on this.

@rwz what do you think about this?

abhiQmar avatar Sep 04 '16 18:09 abhiQmar

I noticed that null within objects which are within an array don't get removed.

aubergene avatar Sep 14 '16 18:09 aubergene

@aubergene return if @ignore_nil && value.nil? or _blank?(value)link According to this line, only nil values will be ignored which are the topmost level.

json.ignore_nil! json.set 'this_hash' {'a' => nil}

will output json with this_hash key with a inside it. I think this is the intended behaviour here.

However I am suggesting we give the user the power to ignore value according to their condition which can be given in a block.

abhiQmar avatar Sep 15 '16 06:09 abhiQmar

You're right this is different issue. I opened #363 with a test to demonstrate.

nil values are ignored within nested objects, I think it's a different issue.

json.ignore_nil! true

json.one do
  json.name "Bob"
  json.dne nil
  json.two do
    json.name "Bob 2"
    json.dne nil
  end
end

aubergene avatar Oct 12 '16 11:10 aubergene