Skip to content

Commit d96a1b6

Browse files
committed
Merge pull request rails#21158 from lsylvester/only-invoke-mattr_accessor-default-block-once
Prevent the default block for mattr_accessor being called multiple times
2 parents 4b91db5 + a51dad1 commit d96a1b6

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

activesupport/lib/active_support/core_ext/module/attribute_accessors.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def #{sym}=(obj)
206206
# Person.class_variable_get("@@hair_colors") # => [:brown, :black, :blonde, :red]
207207
def mattr_accessor(*syms, &blk)
208208
mattr_reader(*syms, &blk)
209-
mattr_writer(*syms, &blk)
209+
mattr_writer(*syms)
210210
end
211211
alias :cattr_accessor :mattr_accessor
212212
end

activesupport/test/core_ext/module/attribute_accessor_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,10 @@ def test_should_use_default_value_if_block_passed
7676
assert_equal 'default_reader_value', @module.defr
7777
assert_equal 'default_writer_value', @module.class_variable_get('@@defw')
7878
end
79+
80+
def test_should_not_invoke_default_value_block_multiple_times
81+
count = 0
82+
@module.cattr_accessor(:defcount){ count += 1 }
83+
assert_equal 1, count
84+
end
7985
end

0 commit comments

Comments
 (0)