Skip to content
This repository
Browse code

Made the :prefix option on Module#delegate accept a custom prefix.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information...
commit ab2b1570fdfcb6fa148f30ccd6edcff6428b4d7a 1 parent 731c63f
Daniel Schierbeck authored September 21, 2008 NZKoz committed October 19, 2008
2  activesupport/lib/active_support/core_ext/module/delegation.rb
@@ -53,7 +53,7 @@ def delegate(*methods)
53 53
       raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
54 54
     end
55 55
 
56  
-    prefix = options[:prefix] ? "#{to}_" : ""
  56
+    prefix = options[:prefix] && (options[:prefix] == true ? "#{to}_" : "#{options[:prefix]}_")
57 57
 
58 58
     methods.each do |method|
59 59
       module_eval(<<-EOS, "(__DELEGATION__)", 1)
9  activesupport/test/core_ext/module_test.rb
@@ -38,6 +38,7 @@ class De
38 38
 
39 39
 Invoice   = Struct.new(:client) do
40 40
   delegate :street, :city, :name, :to => :client, :prefix => true
  41
+  delegate :street, :city, :name, :to => :client, :prefix => :customer
41 42
 end
42 43
 
43 44
 class Name
@@ -97,6 +98,14 @@ def test_delegation_prefix
97 98
     assert_equal invoice.client_city, "Chicago"
98 99
   end
99 100
 
  101
+  def test_delegation_custom_prefix
  102
+    david = Someone.new("David", Somewhere.new("Paulina", "Chicago"))
  103
+    invoice = Invoice.new(david)
  104
+    assert_equal invoice.customer_name, "David"
  105
+    assert_equal invoice.customer_street, "Paulina"
  106
+    assert_equal invoice.customer_city, "Chicago"
  107
+  end
  108
+
100 109
   def test_parent
101 110
     assert_equal Yz::Zy, Yz::Zy::Cd.parent
102 111
     assert_equal Yz, Yz::Zy.parent

0 notes on commit ab2b157

Please sign in to comment.
Something went wrong with that request. Please try again.