Skip to content

Commit

Permalink
Merge pull request rails#25681 from willnet/fix-thread_mattr_accessor
Browse files Browse the repository at this point in the history
Fix `thread_mattr_accessor` share variable superclass with subclass
  • Loading branch information
senny committed Aug 8, 2016
2 parents 628474f + 3529e58 commit e9852c9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
28 changes: 28 additions & 0 deletions activesupport/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
* Fix `thread_mattr_accessor` subclass no longer overwrites parent.

Assigning a value to a subclass using `thread_mattr_accessor` no
longer changes the value of the parent class. This brings the
behavior inline with the documentation.

Given:

class Account
thread_mattr_accessor :user
end

class Customer < Account
end

Account.user = "DHH"
Customer.user = "Rafael"

Before:

Account.user # => "Rafael"

After:

Account.user # => "DHH"

*Shinichi Maeshima*

* Since weeks are no longer converted to days, add `:weeks` to the list of
parts that `ActiveSupport::TimeWithZone` will recognize as possibly being
of variable duration to take account of DST transitions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ def thread_mattr_reader(*syms)
raise NameError.new("invalid attribute name: #{sym}") unless /^[_A-Za-z]\w*$/.match?(sym)
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
def self.#{sym}
Thread.current[:"attr_#{name}_#{sym}"]
Thread.current["attr_"+ name + "_#{sym}"]
end
EOS

unless options[:instance_reader] == false || options[:instance_accessor] == false
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
def #{sym}
Thread.current[:"attr_#{name}_#{sym}"]
Thread.current["attr_"+ self.class.name + "_#{sym}"]
end
EOS
end
Expand Down Expand Up @@ -80,14 +80,14 @@ def thread_mattr_writer(*syms)
raise NameError.new("invalid attribute name: #{sym}") unless /^[_A-Za-z]\w*$/.match?(sym)
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
def self.#{sym}=(obj)
Thread.current[:"attr_#{name}_#{sym}"] = obj
Thread.current["attr_"+ name + "_#{sym}"] = obj
end
EOS

unless options[:instance_writer] == false || options[:instance_accessor] == false
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
def #{sym}=(obj)
Thread.current[:"attr_#{name}_#{sym}"] = obj
Thread.current["attr_"+ self.class.name + "_#{sym}"] = obj
end
EOS
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ def setup
thread_mattr_accessor :bar, instance_writer: false
thread_mattr_reader :shaq, instance_reader: false
thread_mattr_accessor :camp, instance_accessor: false

def self.name; "MyClass" end
end

@subclass = Class.new(@class) do
def self.name; "SubMyClass" end
end

@object = @class.new
Expand Down Expand Up @@ -112,4 +118,14 @@ def test_should_return_same_value_by_class_or_instance_accessor

assert_equal @class.foo, @object.foo
end

def test_should_not_affect_superclass_if_subclass_set_value
@class.foo = "super"
assert_equal @class.foo, "super"
assert_nil @subclass.foo

@subclass.foo = "sub"
assert_equal @class.foo, "super"
assert_equal @subclass.foo, "sub"
end
end

0 comments on commit e9852c9

Please sign in to comment.