solnic / virtus

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects
MIT License
3.77k stars 228 forks source link

Quickly remove empty attributes from returned hash by adding remove_empty_attributes! method #273

Closed cmrichards closed 10 years ago

cmrichards commented 10 years ago

It would be good if the hashes returned from form.save had a method that would remove all of the empty hashes. This would save some time spent manually removing all the empty attributes from hashes. e.g.

form.save  do |data, nested| 

  # do processing with the nested attributes here, run delete_if or whatever ... 

  # Remove any un-needed empty attributes
  nested.remove_empty_attributes! 
  # or...
  nested[:contracts].remove_empty_attributes!

   # Create using accepts_nested_attributes_for
  Job.create(nested)   
end

Could this easily be done by subclassing Hash?

solnic commented 10 years ago

I'm sorry but this kind of functionality will not be added to virtus. It's not meant to be used as an input data sanitazer or validator.

mbj commented 10 years ago

@solnic Full ack.