aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarcel Molina <marcel@vernix.org>2006-04-29 20:13:27 +0000
committerMarcel Molina <marcel@vernix.org>2006-04-29 20:13:27 +0000
commit995167ec2eced73f44d4f961349dbbee6b297210 (patch)
treecd03978ee19fc2dfc57e534761ecfc4468d31adb
parent61864909628f5ac2f20d3337e0274dab016ac7c5 (diff)
downloadrails-995167ec2eced73f44d4f961349dbbee6b297210.tar.gz
rails-995167ec2eced73f44d4f961349dbbee6b297210.tar.bz2
rails-995167ec2eced73f44d4f961349dbbee6b297210.zip
Strip out punctuation on predicates or bang methods being aliased with alias_method_chain since target?_without_feature is not a valid method name. Add tests for Module#alias_method_chain. [Marcel Molina Jr.]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4311 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/module/aliasing.rb7
-rw-r--r--activesupport/test/core_ext/module_test.rb60
3 files changed, 66 insertions, 3 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index f781fe321d..7d20611df2 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Strip out punctuation on predicates or bang methods being aliased with alias_method_chain since target?_without_feature is not a valid method name. Add tests for Module#alias_method_chain. [Marcel Molina Jr.]
+
* Replace Ruby's deprecated append_features in favor of included. [Marcel Molina Jr.]
* Allow default options in with_options to be overridden. Closes #4480. [murphy@cYcnus.de]
diff --git a/activesupport/lib/active_support/core_ext/module/aliasing.rb b/activesupport/lib/active_support/core_ext/module/aliasing.rb
index a80ff0f26d..1f904377df 100644
--- a/activesupport/lib/active_support/core_ext/module/aliasing.rb
+++ b/activesupport/lib/active_support/core_ext/module/aliasing.rb
@@ -10,7 +10,10 @@ class Module
#
# And both aliases are set up for you.
def alias_method_chain(target, feature)
- alias_method "#{target}_without_#{feature}", target
- alias_method target, "#{target}_with_#{feature}"
+ # Strip out punctuation on predicates or bang methods since
+ # e.g. target?_without_feature is not a valid method name.
+ aliased_target = target.to_s.sub(/[?!]/, '')
+ alias_method "#{aliased_target}_without_#{feature}", target
+ alias_method target, "#{aliased_target}_with_#{feature}"
end
end
diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb
index eb92fbc12c..06dd44cd2d 100644
--- a/activesupport/test/core_ext/module_test.rb
+++ b/activesupport/test/core_ext/module_test.rb
@@ -98,4 +98,62 @@ class ModuleTest < Test::Unit::TestCase
assert_equal 'yz/zy', Yz::Zy.as_load_path
assert_equal 'yz', Yz.as_load_path
end
-end \ No newline at end of file
+end
+
+module BarMethodAliaser
+ def self.included(foo_class)
+ foo_class.alias_method_chain :bar, :baz
+ end
+
+ def bar_with_baz
+ bar_without_baz << '_with_baz'
+ end
+
+ def quux_with_baz
+ quux_without_baz << '_with_baz'
+ end
+end
+
+class MethodAliasingTest < Test::Unit::TestCase
+
+ def setup
+ Object.const_set(:FooClassWithBarMethod, Class.new)
+ FooClassWithBarMethod.send(:define_method, 'bar', Proc.new { 'bar' })
+ @instance = FooClassWithBarMethod.new
+ end
+
+ def teardown
+ Object.send(:remove_const, :FooClassWithBarMethod)
+ end
+
+ def test_alias_method_chain
+ assert @instance.respond_to? :bar
+ feature_aliases = [:bar_with_baz, :bar_without_baz]
+
+ feature_aliases.each do |method|
+ assert !@instance.respond_to?(method)
+ end
+
+ assert_equal 'bar', @instance.bar
+
+ FooClassWithBarMethod.send(:include, BarMethodAliaser)
+
+ feature_aliases.each do |method|
+ assert @instance.respond_to?(method)
+ end
+
+ assert_equal 'bar_with_baz', @instance.bar
+ assert_equal 'bar', @instance.bar_without_baz
+ end
+
+ def test_alias_method_chain_with_punctuation_method
+ FooClassWithBarMethod.send(:define_method, 'quux!', Proc.new { 'quux' })
+ assert !@instance.respond_to?(:quux_with_baz)
+ FooClassWithBarMethod.send(:include, BarMethodAliaser)
+ FooClassWithBarMethod.alias_method_chain :quux!, :baz
+ assert @instance.respond_to?(:quux_with_baz)
+
+ assert_equal 'quux_with_baz', @instance.quux!
+ assert_equal 'quux', @instance.quux_without_baz
+ end
+end