-
Notifications
You must be signed in to change notification settings - Fork 230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow a default value when the attribute is assigned as nil, with the :allow_nil option #292
base: master
Are you sure you want to change the base?
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. |
|
||
# default from a singleton value (string in this case), | ||
# that doesn't allow a nil value | ||
attribute :author, String, :default => 'Mies', :allow_nil => false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need :allow_nil => false
when you send a default?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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_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.