mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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.
This commit is contained in:
parent
bc488431ae
commit
99525cb6ff
2 changed files with 16 additions and 4 deletions
|
@ -94,17 +94,19 @@ module ActiveSupport
|
||||||
# User.new.allowed_access = true # => NoMethodError
|
# User.new.allowed_access = true # => NoMethodError
|
||||||
# User.new.allowed_access # => 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 <tt>default</tt> or a block to set up the attribute with a default value.
|
||||||
#
|
#
|
||||||
# class User
|
# class User
|
||||||
# include ActiveSupport::Configurable
|
# include ActiveSupport::Configurable
|
||||||
|
# config_accessor :allowed_access, default: false
|
||||||
# config_accessor :hair_colors do
|
# config_accessor :hair_colors do
|
||||||
# [:brown, :black, :blonde, :red]
|
# [:brown, :black, :blonde, :red]
|
||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
|
# User.allowed_access # => false
|
||||||
# User.hair_colors # => [:brown, :black, :blonde, :red]
|
# 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|
|
names.each do |name|
|
||||||
raise NameError.new("invalid config attribute name") unless /\A[_A-Za-z]\w*\z/.match?(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 reader, __FILE__, reader_line if instance_reader
|
||||||
class_eval writer, __FILE__, writer_line if instance_writer
|
class_eval writer, __FILE__, writer_line if instance_writer
|
||||||
end
|
end
|
||||||
send("#{name}=", yield) if block_given?
|
|
||||||
|
send("#{name}=", block_given? ? yield : default)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
private :config_accessor
|
private :config_accessor
|
||||||
|
|
|
@ -50,7 +50,7 @@ class ConfigurableActiveSupport < ActiveSupport::TestCase
|
||||||
assert_not_respond_to instance, :baz=
|
assert_not_respond_to instance, :baz=
|
||||||
end
|
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
|
parent = Class.new do
|
||||||
include ActiveSupport::Configurable
|
include ActiveSupport::Configurable
|
||||||
config_accessor :hair_colors, :tshirt_colors do
|
config_accessor :hair_colors, :tshirt_colors do
|
||||||
|
@ -62,6 +62,15 @@ class ConfigurableActiveSupport < ActiveSupport::TestCase
|
||||||
assert_equal [:black, :blue, :white], parent.tshirt_colors
|
assert_equal [:black, :blue, :white], parent.tshirt_colors
|
||||||
end
|
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
|
test "configuration hash is available on instance" do
|
||||||
instance = Parent.new
|
instance = Parent.new
|
||||||
assert_equal :bar, instance.config.foo
|
assert_equal :bar, instance.config.foo
|
||||||
|
|
Loading…
Reference in a new issue