Skip to content
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 support for defining default values as option for ActiveSupport::Configurable accessors #42030

Merged
merged 1 commit into from
Jul 29, 2021

Conversation

diegotoral
Copy link
Contributor

Summary

Sometimes it can be very strange or long have to define default values for config accessors as blocks, specially for simple values. This commit adds the ability to specify those values by just passing a default option when defining the accessor.

@diegotoral diegotoral changed the title Add support for defining default values for ActiveSupport::Configurable accessors Add support for defining default values as option for ActiveSupport::Configurable accessors Apr 26, 2021
@p8
Copy link
Member

p8 commented Jul 14, 2021

Hi @diegotoral thanks for the PR.
Can you give some code examples that would benefit from this change?

@diegotoral
Copy link
Contributor Author

Hi @p8!

The idea is to make simpler to specify default values for config accessors. The current implementation requires the usage of a block for specifying those values which results in a many lines of code.

class User
  include ActiveSupport::Configurable

  config_accessor :a, default: 1
  config_accessor :b, default: 2
  config_accessor :c, default: 3

  # vs
  
  config_accessor :a do
    1
  end

  config_accessor :b do
    2
  end

  config_accessor :c do
    3
  end
end

Copy link
Member

@jonathanhefner jonathanhefner left a comment

Choose a reason for hiding this comment

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

I think this makes sense for parity with class_attribute, which also has the instance_reader, instance_writer, and instance_accessor options.

activesupport/lib/active_support/configurable.rb Outdated Show resolved Hide resolved
@diegotoral diegotoral force-pushed the configurable-default-option branch from 835252f to b9c3182 Compare July 20, 2021 12:45
@diegotoral
Copy link
Contributor Author

@p8 @jonathanhefner updated the branch to apply the requested changes. 😄

Sometimes it can be very strange or long have to define default values
for config accessors as blocks, specially when config is a simple value
like 1, true, :symbol. This commit adds the ability to specify those
values by just passing a ` default` option when defining the accessor.

It also makes config accessor's interface similar to other Rails
methods like `class_attribute`, which also has the instance_reader,
instance_writer and instance_accessor options.
@diegotoral diegotoral force-pushed the configurable-default-option branch from b9c3182 to 99525cb Compare July 20, 2021 13:37
@jonathanhefner jonathanhefner added the ready PRs ready to merge label Jul 20, 2021
@rafaelfranca rafaelfranca merged commit 646e9d3 into rails:main Jul 29, 2021
@diegotoral diegotoral deleted the configurable-default-option branch July 29, 2021 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
activesupport ready PRs ready to merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants