From 99525cb6ffee2dadf4b11ce1a8094a353ef1a566 Mon Sep 17 00:00:00 2001 From: Diego Toral Date: Tue, 20 Jul 2021 10:36:58 -0300 Subject: [PATCH] ActiveSupport::Configurable default value option 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. --- activesupport/lib/active_support/configurable.rb | 9 ++++++--- activesupport/test/configurable_test.rb | 11 ++++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/activesupport/lib/active_support/configurable.rb b/activesupport/lib/active_support/configurable.rb index 92bd411df5..a6ae8b3494 100644 --- a/activesupport/lib/active_support/configurable.rb +++ b/activesupport/lib/active_support/configurable.rb @@ -94,17 +94,19 @@ module ActiveSupport # User.new.allowed_access = true # => NoMethodError # User.new.allowed_access # => NoMethodError # - # Also you can pass a block to set up the attribute with a default value. + # Also you can pass default or a block to set up the attribute with a default value. # # class User # include ActiveSupport::Configurable + # config_accessor :allowed_access, default: false # config_accessor :hair_colors do # [:brown, :black, :blonde, :red] # end # end # + # User.allowed_access # => false # User.hair_colors # => [:brown, :black, :blonde, :red] - def config_accessor(*names, instance_reader: true, instance_writer: true, instance_accessor: true) # :doc: + def config_accessor(*names, instance_reader: true, instance_writer: true, instance_accessor: true, default: nil) # :doc: names.each do |name| raise NameError.new("invalid config attribute name") unless /\A[_A-Za-z]\w*\z/.match?(name) @@ -118,7 +120,8 @@ module ActiveSupport class_eval reader, __FILE__, reader_line if instance_reader class_eval writer, __FILE__, writer_line if instance_writer end - send("#{name}=", yield) if block_given? + + send("#{name}=", block_given? ? yield : default) end end private :config_accessor diff --git a/activesupport/test/configurable_test.rb b/activesupport/test/configurable_test.rb index a466a1d11d..b41e05c7b2 100644 --- a/activesupport/test/configurable_test.rb +++ b/activesupport/test/configurable_test.rb @@ -50,7 +50,7 @@ class ConfigurableActiveSupport < ActiveSupport::TestCase assert_not_respond_to instance, :baz= end - test "configuration accessors can take a default value" do + test "configuration accessors can take a default value as a block" do parent = Class.new do include ActiveSupport::Configurable config_accessor :hair_colors, :tshirt_colors do @@ -62,6 +62,15 @@ class ConfigurableActiveSupport < ActiveSupport::TestCase assert_equal [:black, :blue, :white], parent.tshirt_colors end + test "configuration accessors can take a default value as an option" do + parent = Class.new do + include ActiveSupport::Configurable + config_accessor :foo, default: :bar + end + + assert_equal :bar, parent.foo + end + test "configuration hash is available on instance" do instance = Parent.new assert_equal :bar, instance.config.foo