Open JanDintel opened 9 years ago
Could you have a look at this MR @elskwid?
Maybe you could have a look @solnic?
I will take a look yes! My initial reaction is "OMG NO" but maybe I'm missing something, I need to take a closer look. Will get back to you as soon as possible.
@solnic Maybe it's just the name of the option (:allow_nil
) that puts you off. But I think this feature/fix is a good addition to Virtus, while staying backwards compatible in the API.
:+1: This also seems related to https://github.com/solnic/virtus/issues/235.
I'm inclined to accept this, or something similar. Since we don't do validations, we can't just reject the whole object if one attribute contains something that we really don't want in our object. So at this level, I suppose it makes sense to ensure our object is in a useable state, whatever that might mean.
I've encountered this need a couple of times; it would be great to be able to specify that an attribute falls back to the default
when set to nil
.
I wonder if this would be an easier feature to pull through if the naming was more explicit? I could see how :allow_nil
could be misinterpreted as to mean something different.
Something like :fallback_to_default_on_nil
would be hard to misunderstand, though it's quite a mouthful. Would maybe :default_on_nil
be close enough..?
Bump... I'd really like this functionality
This MR adds the option
:allow_nil
to aVirtus::Attribute
. The option allows the user to specify wether to use the default value of an attribute that got assigned as nil. The option will befalse
by default for backwards compatibility.See gist for before and after examples
Related issues for this MR are: #291 and #172.