FooBarWidget/default_value_for

The <<-operator changes the default value for new instances

ramhoj opened this issue · 1 comments

I'm using it to set a default value to a serialized column. In a before_save filter in the same model i push (<<) a value to the array that the serialized column gives me.

This had the effect that the next time I initialized a new instance of that model the column's default value had changed.

I found this test case in the test file: http://gist.github.com/487565#file_default_value_for_original_test.rb
I was expecting the behavior to be http://gist.github.com/487565#file_default_value_for_modified_test.rb

To get my modified test to pass I did this change to the implementation: http://gist.github.com/487565#file_default_value_for_original_init.diff

If you'd like I can send you a pull request but first of all I wanted to make sure I didn't completely misunderstood something elemental.

Thank you for providing a very useful plugin!

Kind regards,
Nicklas Ramhöj

The behavior is explicitly documented in the README, which even provides a solution. That said lots of people don't expect this so I've now made duplication the default behavior; see README. But it's a shallow copy so watch out for modification of objects referenced by the default value.