Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6153 (closed defect: fixed)

Opened 2 years ago

Last modified 2 years ago

[PATCH] alias_method_chain does not support setter methods (foo=)

Reported by: caio Assigned to: David
Priority: normal Milestone: 1.x
Component: ActiveSupport Version: edge
Severity: minor Keywords: tiny
Cc:

Description

Calling alias_method_chain :foo, :bar would result in foo_with_bar, foo_with_bar? and foo_with_bar! but not foo_with_bar=.

This patch adds support for the latter, along with tests.

Attachments

active_support_alias_method_chain_for_setters.diff (2.4 kB) - added by caio on 09/12/06 06:43:21.
patch

Change History

09/10/06 21:57:43 changed by caio

er... will post the patch when there's space left on the device.

09/10/06 22:04:02 changed by caio

In case I forget, better have it here:

Index: test/core_ext/module_test.rb
===================================================================
--- test/core_ext/module_test.rb	(revision 5086)
+++ test/core_ext/module_test.rb	(working copy)
@@ -114,6 +114,10 @@
   def quux_with_baz?
     false
   end
+  
+  def quux_with_baz=(v)
+    @quux = v + 1
+  end
 end
 
 class MethodAliasingTest < Test::Unit::TestCase
@@ -161,19 +165,27 @@
   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 })
+    FooClassWithBarMethod.send(:define_method, 'quux=', Proc.new { |v| @quux = v })
+    @instance.quux = 2
     assert !@instance.respond_to?(:quux_with_baz!)
     assert !@instance.respond_to?(:quux_with_baz?)
+    assert !@instance.respond_to?(:quux_with_baz=)
+    assert_equal 2, @instance.instance_variable_get("@quux")
 
     FooClassWithBarMethod.send(:include, BarMethodAliaser)
     FooClassWithBarMethod.alias_method_chain :quux!, :baz
     FooClassWithBarMethod.alias_method_chain :quux?, :baz
+    FooClassWithBarMethod.alias_method_chain :quux=, :baz
+    @instance.quux = 3
 
     assert @instance.respond_to?(:quux_with_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?
+    assert_equal 4, @instance.instance_variable_get("@quux")
   end
 
   def test_alias_method_chain_with_feature_punctuation
Index: lib/active_support/core_ext/module/aliasing.rb
===================================================================
--- lib/active_support/core_ext/module/aliasing.rb	(revision 5086)
+++ lib/active_support/core_ext/module/aliasing.rb	(working copy)
@@ -23,7 +23,7 @@
   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

09/10/06 22:05:08 changed by caio

  • version set to edge.

09/12/06 02:18:02 changed by martin.emde@gmail.com

I would like to see this added to core. This is annoying to have to work around and I've ended up doing it a lot in my models to overload assignment methods for attributes.

Patch works excellently for me and I'd love to see this added.

09/12/06 02:19:38 changed by martin.emde@gmail.com

Edit to above: Not so much for attribute assignments but for association assignment methods.

09/12/06 06:43:21 changed by caio

  • attachment active_support_alias_method_chain_for_setters.diff added.

patch

09/12/06 07:10:43 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.

(In [5091]) alias_method_chain works with accessor= methods also. Closes #6153.