mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Only invoke the default block for mattr_accessor once so that it does not cause issues if it is not idempotent
This commit is contained in:
parent
4b91db5b12
commit
a51dad1c52
2 changed files with 7 additions and 1 deletions
|
@ -206,7 +206,7 @@ class Module
|
|||
# Person.class_variable_get("@@hair_colors") # => [:brown, :black, :blonde, :red]
|
||||
def mattr_accessor(*syms, &blk)
|
||||
mattr_reader(*syms, &blk)
|
||||
mattr_writer(*syms, &blk)
|
||||
mattr_writer(*syms)
|
||||
end
|
||||
alias :cattr_accessor :mattr_accessor
|
||||
end
|
||||
|
|
|
@ -76,4 +76,10 @@ class ModuleAttributeAccessorTest < ActiveSupport::TestCase
|
|||
assert_equal 'default_reader_value', @module.defr
|
||||
assert_equal 'default_writer_value', @module.class_variable_get('@@defw')
|
||||
end
|
||||
|
||||
def test_should_not_invoke_default_value_block_multiple_times
|
||||
count = 0
|
||||
@module.cattr_accessor(:defcount){ count += 1 }
|
||||
assert_equal 1, count
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue