1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Deprecate attr_accessor_with_default.

This commit is contained in:
José Valim 2011-05-16 11:19:11 -04:00
parent 40fa51123d
commit 6733721520
2 changed files with 13 additions and 4 deletions

View file

@ -19,6 +19,7 @@ class Module
# attr_accessor_with_default(:element_name) { name.underscore }
#
def attr_accessor_with_default(sym, default = Proc.new)
ActiveSupport::Deprecation.warn "attr_accessor_with_default is deprecated. Use Ruby instead!"
define_method(sym, block_given? ? default : Proc.new { default })
module_eval(<<-EVAL, __FILE__, __LINE__ + 1)
def #{sym}=(value) # def age=(value)

View file

@ -1,7 +1,7 @@
require 'abstract_unit'
require 'active_support/core_ext/module/attr_accessor_with_default'
class AttrAccessorWithDefaultTest < Test::Unit::TestCase
class AttrAccessorWithDefaultTest < ActiveSupport::TestCase
def setup
@target = Class.new do
def helper
@ -12,20 +12,28 @@ class AttrAccessorWithDefaultTest < Test::Unit::TestCase
end
def test_default_arg
@target.attr_accessor_with_default :foo, :bar
assert_deprecated do
@target.attr_accessor_with_default :foo, :bar
end
assert_equal(:bar, @instance.foo)
@instance.foo = nil
assert_nil(@instance.foo)
end
def test_default_proc
@target.attr_accessor_with_default(:foo) {helper.upcase}
assert_deprecated do
@target.attr_accessor_with_default(:foo) {helper.upcase}
end
assert_equal('HELPER', @instance.foo)
@instance.foo = nil
assert_nil(@instance.foo)
end
def test_invalid_args
assert_raise(ArgumentError) {@target.attr_accessor_with_default :foo}
assert_raise(ArgumentError) do
assert_deprecated do
@target.attr_accessor_with_default :foo
end
end
end
end