-
Notifications
You must be signed in to change notification settings - Fork 21.8k
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
Add option for class_attribute default #29270
Conversation
class_attribute :_layout, :_layout_conditions, instance_accessor: false | ||
self._layout = nil | ||
self._layout_conditions = {} | ||
class_attribute :_layout |
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.
Lost instance_accessor: false
@@ -123,6 +124,10 @@ def class_attribute(*attrs) | |||
remove_possible_method "#{name}=" | |||
attr_writer name | |||
end | |||
|
|||
if default_value |
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.
unless default_value.nil?
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.
.. or just change the nil
on line 79/80
@@ -53,9 +53,8 @@ class << self; attr_accessor :helpers_path; end | |||
include AbstractController::Helpers | |||
|
|||
included do | |||
class_attribute :helpers_path, :include_all_helpers | |||
self.helpers_path ||= [] |
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.
Probably shouldn't replace this with a default:, right? Given that it checks the parent class first.
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 hesitated at that, but ultimately convinced myself the ||=
is a lie: class_attribute
will always define a method on the current class, with the value set to nil... so AFAICS, this is no different from if it were a straight assignment.
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.
Ah, right. Yup, good 👍
@@ -62,8 +62,7 @@ module Callbacks | |||
|
|||
included do | |||
extend ActiveSupport::DescendantsTracker | |||
class_attribute :__callbacks, instance_writer: false | |||
self.__callbacks ||= {} |
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.
Same issue here: "Probably shouldn't replace this with a default:, right? Given that it checks the parent class first."
instance_predicate = options.fetch(:instance_predicate, true) | ||
default_value = options.fetch(:default, nil) |
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.
This (really, the fact it's in the options
hash at all) does mean the methods below will capture the default value in their closure -- so even if the value is changed on the root class, the original default won't be GCed.
I don't think we particularly need to care about a couple of extra empty hashes and arrays floating around, but I figured I'd at least mention it.
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.
Would be nice to convert all these to kwargs. Should probably do a big sweep for Rails 6.
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.
We can do it right now. We only support Ruby versions that work with kwargs
@@ -70,9 +70,10 @@ class Class | |||
# To opt out of both instance methods, pass <tt>instance_accessor: false</tt>. | |||
def class_attribute(*attrs) | |||
options = attrs.extract_options! | |||
instance_reader = options.fetch(:instance_accessor, true) && options.fetch(:instance_reader, true) | |||
instance_writer = options.fetch(:instance_accessor, true) && options.fetch(:instance_writer, true) | |||
instance_reader = options.fetch(:instance_accessor, true) && options.fetch(:instance_reader, true) |
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.
Sorry for a little off-topic here: I have spotted this formatting changes in few recent PRs. Is this new standard? Wasn't this considered "cosmetic" change before?
Not a general new standard. Just a dash of BDFL privilege 😂
…On Mon, May 29, 2017 at 5:47 PM, Josef Šimánek ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In activesupport/lib/active_support/core_ext/class/attribute.rb
<#29270 (comment)>:
> @@ -70,9 +70,10 @@ class Class
# To opt out of both instance methods, pass <tt>instance_accessor: false</tt>.
def class_attribute(*attrs)
options = attrs.extract_options!
- instance_reader = options.fetch(:instance_accessor, true) && options.fetch(:instance_reader, true)
- instance_writer = options.fetch(:instance_accessor, true) && options.fetch(:instance_writer, true)
+ instance_reader = options.fetch(:instance_accessor, true) && options.fetch(:instance_reader, true)
*Sorry for a little off-topic here: I have spotted this formatting changes
in few recent PRs. Is this new standard? Wasn't this considered "cosmetic"
change before?*
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#29270 (review)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAAKtUkXZF1vQlGV1xuQ1Of4NX4ShM9-ks5r-uiKgaJpZM4NpZ0L>
.
|
David, have you tried defaulting with a block? class_attribute :periodic_timers, instance_reader: false, default: [] vs class_attribute(:periodic_timers, instance_reader: false) { [] } The reason I'm bringing it up is because |
That strikes me as less revealing, but the upside is that it won't get evaluated until you access it, which is a nice perk. Default does have symmetry with belongs_to/default, though. |
Add a |
Other aspect is that class_attribute already has other named options. mattr_accessor doesn't and follows the rest of the Ruby methods by same name.
… On May 29, 2017, at 21:11, Genadi Samokovarov ***@***.***> wrote:
Add a default to mattr_accessor as well then? Dunno if it will make the interface too big though.
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Let's make it so!
… On May 30, 2017, at 03:43, Rafael França ***@***.***> wrote:
@rafaelfranca commented on this pull request.
In activesupport/lib/active_support/core_ext/class/attribute.rb:
> instance_predicate = options.fetch(:instance_predicate, true)
+ default_value = options.fetch(:default, nil)
We can do it right now. We only support Ruby versions that work with kwargs
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub, or mute the thread.
|
|
@dhh GitHub didn't connect your email reply: this was in reference to kwargs across the board right? |
Kasper, yes.
…On Tue, May 30, 2017 at 10:52 AM, Kasper Timm Hansen < ***@***.***> wrote:
Let's make it so!
@dhh <https://github.com/dhh> GitHub didn't connect your email reply:
this was in reference to kwargs across the board right?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#29270 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAAKtT_2B2SEt8D8gPlrBs-vmHpVBUL-ks5r-9jKgaJpZM4NpZ0L>
.
|
True. I'd rather make mattr_accessor use default:, then, than going with
{}. For lazy resolution, we could always have default take a lambda.
…On Tue, May 30, 2017 at 10:20 AM, Genadi Samokovarov < ***@***.***> wrote:
mattr_accessor family actually accept options as well
<https://github.com/rails/rails/blob/6847877a30fd8d578f72db0cf40674f71a9b6286/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb#L67>,
they let you choose whether or not to have the instance methods. The
interfaces between class_attributes and cattr_accessor aren't actually
that far off.
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#29270 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAAKtV3g5Nes_o1Dp59zdfMyw3kYvJysks5r-9FggaJpZM4NpZ0L>
.
|
Looks like
Then there doesn't seem to be a need for I'd be 👍 on deprecating the block to |
Ah, yeah, then I definitely don't think it's worth it. A kwarg seems
clearly better to me.
…On Tue, May 30, 2017 at 1:21 PM, Kasper Timm Hansen < ***@***.***> wrote:
Looks like mattr_* just yields to the block immediately, so there's no
lazy loading at all: https://github.com/rails/rails/blob/
3fbe657/activesupport/lib/
active_support/core_ext/module/attribute_accessors.rb#L74
Then there doesn't seem to be a need for default: to take lambdas —
besides all our class_attribute didn't need deferred evaluation, and I
haven't seen the need for it in my app's usage of mattr_* either.
I'd be 👍 on deprecating the block to mattr_* and cattr_* and replacing
it with a default: arg.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#29270 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAAKtUEcs1Osnx8aP9vavCQhZAVbZm_dks5r-_u2gaJpZM4NpZ0L>
.
|
Cool, I can take stab at that. |
PR rails#29270 changed the number of arguments that gets passed to Procs defined in ActionMail::Base.default. This fixes it and introduce a deprecation so the hack can be removed (the argument is useless since it has the same value as `self`).
PR rails#29270 changed the number of arguments that gets passed to Procs defined in ActionMail::Base.default. With this changeset, Procs can now have 1 or 0 arguments Also adds test coverage for AM::Base.default Proc arity.
As default value for class_attribute introduced only in Rails 5 rails/rails#29270
* Allow a default value to be declared for class_attribute * Convert to using class_attribute default rather than explicit setter * Removed instance_accessor option by mistake * False is a valid default value * Documentation
It's extremely common, as evidenced by the commit in this PR of Rails conversions, to set a default value for a class_attribute. So let's provide that directly.