diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2006-06-03 22:43:13 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2006-06-03 22:43:13 +0000 |
commit | e72ff355c35879ffaf262f4bd91ce7612f028a8e (patch) | |
tree | 6ad1f9f497c79297c3ac107cef56b5f11ef728fd /activesupport | |
parent | b85c5356c905add4ba2f3d68e9a67ab5e3b247d2 (diff) | |
download | rails-e72ff355c35879ffaf262f4bd91ce7612f028a8e.tar.gz rails-e72ff355c35879ffaf262f4bd91ce7612f028a8e.tar.bz2 rails-e72ff355c35879ffaf262f4bd91ce7612f028a8e.zip |
Fixed that Module#alias_method_chain should work with both foo? foo! and foo at the same time (closes #4954) [anna@wota.jp]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4429 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/module/aliasing.rb | 14 | ||||
-rw-r--r-- | activesupport/test/core_ext/module_test.rb | 34 |
3 files changed, 40 insertions, 10 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index a9fd1a902e..79201b2300 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed that Module#alias_method_chain should work with both foo? foo! and foo at the same time #4954 [anna@wota.jp] + * to_xml fixes, features, and speedup: introduce :dasherize option that converts updated_at to updated-at if true (the existing default); binary columns get encoding="base64" attribute; nil values get nil="true" attribute to distinguish empty values; add type information for float columns; allow arbitrarily deep :include; include SQL type information as the type attribute. #4989 [Blair Zajac <blair@orcaware.com>] * Add OrderedHash#values. [Sam Stephenson] diff --git a/activesupport/lib/active_support/core_ext/module/aliasing.rb b/activesupport/lib/active_support/core_ext/module/aliasing.rb index 1f904377df..5c5875504f 100644 --- a/activesupport/lib/active_support/core_ext/module/aliasing.rb +++ b/activesupport/lib/active_support/core_ext/module/aliasing.rb @@ -9,11 +9,17 @@ class Module # alias_method_chain :foo, :feature # # And both aliases are set up for you. + # + # A punctuation is moved to the end on predicates or bang methods. + # + # alias_method_chain :foo?, :feature + # + # generates "foo_without_feature?" method for old one, + # and expects "foo_with_feature?" method for new one. 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. + punctuation = target.to_s.scan(/[?!]/).first aliased_target = target.to_s.sub(/[?!]/, '') - alias_method "#{aliased_target}_without_#{feature}", target - alias_method target, "#{aliased_target}_with_#{feature}" + alias_method "#{aliased_target}_without_#{feature}#{punctuation}", target + alias_method target, "#{aliased_target}_with_#{feature}#{punctuation}" end end diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index bd51aa11c3..66e4afc918 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -109,8 +109,12 @@ module BarMethodAliaser bar_without_baz << '_with_baz' end - def quux_with_baz - quux_without_baz << '_with_baz' + def quux_with_baz! + quux_without_baz! << '_with_baz!' + end + + def quux_with_baz? + false end end @@ -148,12 +152,30 @@ class MethodAliasingTest < Test::Unit::TestCase def test_alias_method_chain_with_punctuation_method FooClassWithBarMethod.send(:define_method, 'quux!', Proc.new { 'quux' }) - assert !@instance.respond_to?(:quux_with_baz) + 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 @instance.respond_to?(:quux_with_baz!) + + assert_equal 'quux_with_baz!', @instance.quux! + assert_equal 'quux', @instance.quux_without_baz! + end - assert_equal 'quux_with_baz', @instance.quux! - assert_equal 'quux', @instance.quux_without_baz + 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 { true }) + 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_equal false, @instance.quux? + assert_equal true, @instance.quux_without_baz? end end |