aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2006-09-12 07:10:43 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2006-09-12 07:10:43 +0000
commit74f60c032e92b2473b5feeeb85782836c2b83fdd (patch)
tree97d9d1975e8d6a3a286a3e2fa20480f224493255
parent3c0e7b1b51cb775e55a4b10a9872f74cecacd6a7 (diff)
downloadrails-74f60c032e92b2473b5feeeb85782836c2b83fdd.tar.gz
rails-74f60c032e92b2473b5feeeb85782836c2b83fdd.tar.bz2
rails-74f60c032e92b2473b5feeeb85782836c2b83fdd.zip
alias_method_chain works with accessor= methods also. Closes #6153.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5091 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/module/aliasing.rb2
-rw-r--r--activesupport/test/core_ext/module_test.rb31
3 files changed, 25 insertions, 10 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 33961ce1c7..b2a5cd1cd8 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* alias_method_chain works with accessor= methods also. #6153 [Caio Chassot]
+
* Fix loadable_constants_for_path to handle load paths that do not end with a slash. [Nicholas Seckar]
* Fix logic error in determining what was loaded by a given file. Closes #6039. [Nicholas Seckar]
diff --git a/activesupport/lib/active_support/core_ext/module/aliasing.rb b/activesupport/lib/active_support/core_ext/module/aliasing.rb
index a4c7e539e3..19a9297aa4 100644
--- a/activesupport/lib/active_support/core_ext/module/aliasing.rb
+++ b/activesupport/lib/active_support/core_ext/module/aliasing.rb
@@ -23,7 +23,7 @@ class Module
def alias_method_chain(target, feature)
# Strip out punctuation on predicates or bang methods since
# e.g. target?_without_feature is not a valid method name.
- aliased_target, punctuation = target.to_s.sub(/([?!])$/, ''), $1
+ aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
alias_method "#{aliased_target}_without_#{feature}#{punctuation}", target
alias_method target, "#{aliased_target}_with_#{feature}#{punctuation}"
end
diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb
index 2b51656509..9d86c84e21 100644
--- a/activesupport/test/core_ext/module_test.rb
+++ b/activesupport/test/core_ext/module_test.rb
@@ -108,12 +108,16 @@ module BarMethodAliaser
end
def quux_with_baz!
- quux_without_baz! << '_with_baz!'
+ quux_without_baz! << '_with_baz'
end
def quux_with_baz?
false
end
+
+ def quux_with_baz=(v)
+ send(:quux_without_baz=, v) << '_with_baz'
+ end
end
class MethodAliasingTest < Test::Unit::TestCase
@@ -154,26 +158,35 @@ class MethodAliasingTest < Test::Unit::TestCase
FooClassWithBarMethod.alias_method_chain :quux!, :baz
assert @instance.respond_to?(:quux_with_baz!)
- assert_equal 'quux_with_baz!', @instance.quux!
+ assert_equal 'quux_with_baz', @instance.quux!
assert_equal 'quux', @instance.quux_without_baz!
end
def test_alias_method_chain_with_same_names_between_predicates_and_bang_methods
- FooClassWithBarMethod.send(:define_method, 'quux!', Proc.new { 'quux' })
+ FooClassWithBarMethod.send(:define_method, 'quux!', Proc.new { 'quux!' })
FooClassWithBarMethod.send(:define_method, 'quux?', Proc.new { true })
+ FooClassWithBarMethod.send(:define_method, 'quux=', Proc.new { 'quux=' })
assert !@instance.respond_to?(:quux_with_baz!)
assert !@instance.respond_to?(:quux_with_baz?)
+ assert !@instance.respond_to?(:quux_with_baz=)
FooClassWithBarMethod.send(:include, BarMethodAliaser)
- FooClassWithBarMethod.alias_method_chain :quux!, :baz
- FooClassWithBarMethod.alias_method_chain :quux?, :baz
-
assert @instance.respond_to?(:quux_with_baz!)
assert @instance.respond_to?(:quux_with_baz?)
- assert_equal 'quux_with_baz!', @instance.quux!
- assert_equal 'quux', @instance.quux_without_baz!
+ assert @instance.respond_to?(:quux_with_baz=)
+
+
+ FooClassWithBarMethod.alias_method_chain :quux!, :baz
+ assert_equal 'quux!_with_baz', @instance.quux!
+ assert_equal 'quux!', @instance.quux_without_baz!
+
+ FooClassWithBarMethod.alias_method_chain :quux?, :baz
assert_equal false, @instance.quux?
assert_equal true, @instance.quux_without_baz?
+
+ FooClassWithBarMethod.alias_method_chain :quux=, :baz
+ assert_equal 'quux=_with_baz', @instance.send(:quux=, 1234)
+ assert_equal 'quux=', @instance.send(:quux_without_baz=, 1234)
end
def test_alias_method_chain_with_feature_punctuation
@@ -183,7 +196,7 @@ class MethodAliasingTest < Test::Unit::TestCase
FooClassWithBarMethod.alias_method_chain :quux, :baz!
assert_nothing_raised do
- assert_equal 'quux_with_baz!', @instance.quux_with_baz!
+ assert_equal 'quux_with_baz', @instance.quux_with_baz!
end
assert_raise(NameError) do