Allow a default value when the attribute is assigned as nil, with the :allow_nil option#292
Allow a default value when the attribute is assigned as nil, with the :allow_nil option#292JanDintel wants to merge 3 commits intosolnic:masterfrom
Conversation
…il? predicate on the Virtus::Attribute class
…the presence of the attribute on the Virtus instance given the :allow_nil option
…is assigned as nil: - Replace the Virtus::Attribute::Accessor#defined? method for the #present? method in the Virtus::AttributeSet#skip_default? private method - Replace the #instance_variable_defined? method for the Virtus::Attribute::Accessor#present? method in the Virtus::Attribute::LazyDefault#get method - Update the README with the documentation of this feature This fixes Github issue solnic#291 (solnic#291) and solnic#172 (solnic#172)
|
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 ( |
|
👍 This also seems related to #235. |
There was a problem hiding this comment.
I don't think the example should use a default, because it's much less likely that you'd need to worry about nil if you've got a default.
There was a problem hiding this comment.
Oh, you're suggesting to use the default if nil is passed in. Hmm. That's a harder sell.
There was a problem hiding this comment.
Why do you need :allow_nil => false when you send a default?
There was a problem hiding this comment.
@envygeeks Maybe this example explains it the best:
class Page
include Virtus.model
attribute :title, String, :default => 'New page', :allow_nil => true
attribute :author, String, :default => 'Mies', :allow_nil => false
end
page = Page.new(title: nil, author: nil)
page.title # => nil
page.author # => 'Mies'With the :allow_nil => false option, the default is used when the attributes is assigned as nil.
Edit: Just saw that I published a gist that describes the behaviour as well.
There was a problem hiding this comment.
Can you talk more about the differences between page = Page.new(title: nil, author: nil) and page = Page.new?
|
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 I wonder if this would be an easier feature to pull through if the naming was more explicit? I could see how Something like |
|
Bump... I'd really like this functionality |
This MR adds the option
:allow_nilto 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 befalseby default for backwards compatibility.See gist for before and after examples
Related issues for this MR are: #291 and #172.