Skip to content

Add option for class_attribute default - #29270

Merged
dhh merged 5 commits into
masterfrom
class-attribute-default
May 29, 2017
Merged

dhh merged 5 commits into
masterfrom
class-attribute-default

Conversation

@dhh

@dhh dhh commented May 29, 2017

Copy link
Copy Markdown
Member

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.

Comment thread actionview/lib/action_view/layouts.rb Outdated
class_attribute :_layout, :_layout_conditions, instance_accessor: false
self._layout = nil
self._layout_conditions = {}
class_attribute :_layout

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lost instance_accessor: false

attr_writer name
end

if default_value

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless default_value.nil?

Copy link
Copy Markdown
Member

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


included do
class_attribute :helpers_path, :include_all_helpers
self.helpers_path ||= []

Copy link
Copy Markdown
Member Author

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.

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right. Yup, good 👍

included do
extend ActiveSupport::DescendantsTracker
class_attribute :__callbacks, instance_writer: false
self.__callbacks ||= {}

Copy link
Copy Markdown
Member Author

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_reader = options.fetch(:instance_accessor, true) && options.fetch(:instance_reader, true)
instance_writer = options.fetch(:instance_accessor, true) && options.fetch(:instance_writer, true)
instance_predicate = options.fetch(:instance_predicate, true)
default_value = options.fetch(:default, nil)

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member Author

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.

Copy link
Copy Markdown
Member

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

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)

Copy link
Copy Markdown
Contributor

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?

@dhh

dhh commented May 29, 2017 via email

Copy link
Copy Markdown
Member Author

@dhh
dhh merged commit 1c275d8 into master May 29, 2017
@dhh
dhh deleted the class-attribute-default branch May 29, 2017 16:01
@gsamokovarov

Copy link
Copy Markdown
Contributor

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 mattr_accessor works like 👆 and does not have the default option. Should we pair the functionality so it's symmetrical across all the attribute extensions?

@dhh

dhh commented May 29, 2017

Copy link
Copy Markdown
Member Author

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.

@gsamokovarov

Copy link
Copy Markdown
Contributor

Add a default to mattr_accessor as well then? Dunno if it will make the interface too big though.

@dhh

dhh commented May 29, 2017 via email

Copy link
Copy Markdown
Member Author

@dhh

dhh commented May 30, 2017 via email

Copy link
Copy Markdown
Member Author

@gsamokovarov

gsamokovarov commented May 30, 2017 •

Copy link
Copy Markdown
Contributor

mattr_accessor family actually accept options as well, they let you choose whether or not to have the instance methods. The interfaces between class_attributes and cattr_accessor aren't that far off.

@kaspth

kaspth commented May 30, 2017

Copy link
Copy Markdown
Contributor

Let's make it so!

@dhh GitHub didn't connect your email reply: this was in reference to kwargs across the board right?

@dhh

dhh commented May 30, 2017 via email

Copy link
Copy Markdown
Member Author

@dhh

dhh commented May 30, 2017 via email

Copy link
Copy Markdown
Member Author

@kaspth

kaspth commented May 30, 2017

Copy link
Copy Markdown
Contributor

Looks like mattr_* just yields to the block immediately, so there's no lazy loading at all:

class_variable_set("@@#{sym}", yield) if block_given?

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.

@dhh

dhh commented May 30, 2017 via email

Copy link
Copy Markdown
Member Author

@gsamokovarov

Copy link
Copy Markdown
Contributor

Cool, I can take stab at that.

jbourassa pushed a commit to jbourassa/rails that referenced this pull request Aug 24, 2017
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`).
jbourassa pushed a commit to jbourassa/rails that referenced this pull request Aug 29, 2017
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.
antonzaytsev added a commit to sdtechdev/activestorage-rails4 that referenced this pull request Dec 19, 2017
As default value for class_attribute introduced only in Rails 5
rails/rails#29270
matthewrudy pushed a commit to matthewrudy/rude-rails that referenced this pull request Feb 12, 2018
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants