diff --git a/activesupport/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb b/activesupport/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb index 452a456a52..ea4034303e 100644 --- a/activesupport/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +++ b/activesupport/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb @@ -53,8 +53,7 @@ class Module EOS end - default_val = (block_given? && default.nil?) ? yield : default - Thread.current["attr_" + name + "_#{sym}"] = default_val unless default_val.nil? + Thread.current["attr_" + name + "_#{sym}"] = default unless default.nil? end end alias :thread_cattr_reader :thread_mattr_reader @@ -97,8 +96,7 @@ class Module EOS end - default_val = (block_given? && default.nil?) ? yield : default - Thread.current["attr_" + name + "_#{sym}"] = default_val unless default_val.nil? + public_send("#{sym}=", default) unless default.nil? end end alias :thread_cattr_writer :thread_mattr_writer @@ -142,9 +140,9 @@ class Module # # Current.new.user = "DHH" # => NoMethodError # Current.new.user # => NoMethodError - def thread_mattr_accessor(*syms, instance_reader: true, instance_writer: true, instance_accessor: true, default: nil, &blk) - thread_mattr_reader(*syms, instance_reader: instance_reader, instance_accessor: instance_accessor, default: default, &blk) - thread_mattr_writer(*syms, instance_writer: instance_writer, instance_accessor: instance_accessor, default: default) + def thread_mattr_accessor(*syms, instance_reader: true, instance_writer: true, instance_accessor: true, default: nil) + thread_mattr_reader(*syms, instance_reader: instance_reader, instance_accessor: instance_accessor, default: default) + thread_mattr_writer(*syms, instance_writer: instance_writer, instance_accessor: instance_accessor) end alias :thread_cattr_accessor :thread_mattr_accessor end diff --git a/activesupport/test/core_ext/module/attribute_accessor_per_thread_test.rb b/activesupport/test/core_ext/module/attribute_accessor_per_thread_test.rb index c73ca0be0c..a2b3239884 100644 --- a/activesupport/test/core_ext/module/attribute_accessor_per_thread_test.rb +++ b/activesupport/test/core_ext/module/attribute_accessor_per_thread_test.rb @@ -14,7 +14,7 @@ class ModuleAttributeAccessorPerThreadTest < ActiveSupport::TestCase class SubMyClass < MyClass end - def setup + setup do @class = MyClass @subclass = SubMyClass @object = @class.new @@ -23,18 +23,11 @@ class ModuleAttributeAccessorPerThreadTest < ActiveSupport::TestCase def test_can_initialize_with_default_value Thread.new do @class.thread_mattr_accessor :baz, default: "default_value" - assert_equal "default_value", @class.baz - end.join - end - - def test_can_initialize_with_a_block_as_default_value - Thread.new do - @class.thread_mattr_accessor :baz do - "default_value" - end assert_equal "default_value", @class.baz end.join + + assert_nil @class.baz end def test_should_use_mattr_default @@ -82,23 +75,23 @@ class ModuleAttributeAccessorPerThreadTest < ActiveSupport::TestCase threads = [] threads << Thread.new do @class.foo = "things" - sleep 1 + Thread.pass assert_equal "things", @class.foo end threads << Thread.new do @class.foo = "other things" - sleep 1 + Thread.pass assert_equal "other things", @class.foo end threads << Thread.new do @class.foo = "really other things" - sleep 1 + Thread.pass assert_equal "really other things", @class.foo end - threads.each { |t| t.join } + threads.each(&:join) end def test_should_raise_name_error_if_attribute_name_is_invalid