aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorLarry Lv <larrylv1990@gmail.com>2012-09-15 03:25:21 +0800
committerLarry Lv <larrylv1990@gmail.com>2012-09-17 09:14:21 +0800
commit1efe30ebcef2e6d3967742f9bcf4f6675a946d14 (patch)
tree3f667a6d1535e0dfee5009b9b368a0a5c3ea0e1c /activesupport
parent82efe8943bf6ff12b597987506bf177d66609049 (diff)
downloadrails-1efe30ebcef2e6d3967742f9bcf4f6675a946d14.tar.gz
rails-1efe30ebcef2e6d3967742f9bcf4f6675a946d14.tar.bz2
rails-1efe30ebcef2e6d3967742f9bcf4f6675a946d14.zip
set up config_accessor with a default value by block
* ActiveSupport::Configurable should allow config_accessor to take default value by block, just like cattr_accessor. class User include ActiveSupport::Configurable config_accessor :hair_colors do [:brown, :black, :blonde, :red] end end User.hair_colors # => [:brown, :black, :blonde, :red] * remove trailing whitespaces in configurable.rb and its test file. * Update ActiveSupport CHANGELOG.
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG.md13
-rw-r--r--activesupport/lib/active_support/configurable.rb24
-rw-r--r--activesupport/test/configurable_test.rb16
3 files changed, 45 insertions, 8 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index c10a0b390b..59e112e3f9 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,5 +1,18 @@
## Rails 4.0.0 (unreleased) ##
+* An optional block can be passed to `config_accessor` to set its default value
+
+ class User
+ include ActiveSupport::Configurable
+ config_accessor :hair_colors do
+ [:brown, :black, :blonde, :red]
+ end
+ end
+
+ User.hair_colors # => [:brown, :black, :blonde, :red]
+
+ *Larry Lv*
+
* ActiveSupport::Benchmarkable#silence has been deprecated due to its lack of
thread safety. It will be removed without replacement in Rails 4.1. *Steve
Klabnik*
diff --git a/activesupport/lib/active_support/configurable.rb b/activesupport/lib/active_support/configurable.rb
index 307ae40398..15a5b98d56 100644
--- a/activesupport/lib/active_support/configurable.rb
+++ b/activesupport/lib/active_support/configurable.rb
@@ -39,7 +39,7 @@ module ActiveSupport
# Allows you to add shortcut so that you don't have to refer to attribute
# through config. Also look at the example for config to contrast.
- #
+ #
# Defines both class and instance config accessors.
#
# class User
@@ -47,16 +47,16 @@ module ActiveSupport
# config_accessor :allowed_access
# end
#
- # User.allowed_access # => nil
+ # User.allowed_access # => nil
# User.allowed_access = false
- # User.allowed_access # => false
- #
+ # User.allowed_access # => false
+ #
# user = User.new
# user.allowed_access # => false
# user.allowed_access = true
# user.allowed_access # => true
#
- # User.allowed_access # => false
+ # User.allowed_access # => false
#
# The attribute name must be a valid method name in Ruby.
#
@@ -91,7 +91,18 @@ module ActiveSupport
#  User.allowed_access # => false
#
# 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.
+ #
+ # class User
+ # include ActiveSupport::Configurable
+ # config_accessor :hair_colors do
+ # [:brown, :black, :blonde, :red]
+ # end
+ # end
+ #
+ # User.hair_colors # => [:brown, :black, :blonde, :red]
def config_accessor(*names)
options = names.extract_options!
@@ -108,6 +119,7 @@ module ActiveSupport
class_eval reader, __FILE__, reader_line unless options[:instance_reader] == false
class_eval writer, __FILE__, writer_line unless options[:instance_writer] == false
end
+ send("#{name}=", yield) if block_given?
end
end
end
diff --git a/activesupport/test/configurable_test.rb b/activesupport/test/configurable_test.rb
index da7729d066..215a6e06b0 100644
--- a/activesupport/test/configurable_test.rb
+++ b/activesupport/test/configurable_test.rb
@@ -48,6 +48,18 @@ class ConfigurableActiveSupport < ActiveSupport::TestCase
assert !instance.respond_to?(:baz=)
end
+ test "configuration accessors can take a default value" do
+ parent = Class.new do
+ include ActiveSupport::Configurable
+ config_accessor :hair_colors, :tshirt_colors do
+ [:black, :blue, :white]
+ end
+ end
+
+ assert_equal [:black, :blue, :white], parent.hair_colors
+ assert_equal [:black, :blue, :white], parent.tshirt_colors
+ end
+
test "configuration hash is available on instance" do
instance = Parent.new
assert_equal :bar, instance.config.foo
@@ -78,7 +90,7 @@ class ConfigurableActiveSupport < ActiveSupport::TestCase
test "should raise name error if attribute name is invalid" do
assert_raises NameError do
- Class.new do
+ Class.new do
include ActiveSupport::Configurable
config_accessor "invalid attribute name"
end
@@ -94,4 +106,4 @@ class ConfigurableActiveSupport < ActiveSupport::TestCase
methods = object.public_methods.map(&:to_s)
assert !methods.include?(method.to_s), "Expected #{methods.inspect} to not include #{method.to_s.inspect}"
end
-end \ No newline at end of file
+end