public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
ActionMailer should respond_to? to methods handled by method_missing [#700 
state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
floehopper (author)
Fri Aug 29 13:08:16 -0700 2008
josh (committer)
Fri Aug 29 13:09:47 -0700 2008
commit  3cf773b187e803e16b8237e5923fa4c1139cde8a
tree    d1d62af672f28c39f7bafa980f0dd35d9ddfcc47
parent  99492bad885aa0ee44c770e2c61ad36c3058c697
...
374
375
376
377
378
379
380
381
 
 
 
 
 
 
 
 
 
 
382
383
384
...
424
425
426
 
 
 
 
 
 
427
428
429
...
374
375
376
 
 
 
 
 
377
378
379
380
381
382
383
384
385
386
387
388
389
...
429
430
431
432
433
434
435
436
437
438
439
440
0
@@ -374,11 +374,16 @@ module ActionMailer #:nodoc:
0
       alias_method :controller_name, :mailer_name
0
       alias_method :controller_path, :mailer_name
0
 
0
-      def method_missing(method_symbol, *parameters)#:nodoc:
0
-        case method_symbol.id2name
0
-          when /^create_([_a-z]\w*)/  then new($1, *parameters).mail
0
-          when /^deliver_([_a-z]\w*)/ then new($1, *parameters).deliver!
0
-          when "new" then nil
0
+      def respond_to?(method_symbol, include_private = false) #:nodoc:
0
+        matches_dynamic_method?(method_symbol) || super
0
+      end
0
+
0
+      def method_missing(method_symbol, *parameters) #:nodoc:
0
+        match = matches_dynamic_method?(method_symbol)
0
+        case match[1]
0
+          when 'create'  then new(match[2], *parameters).mail
0
+          when 'deliver' then new(match[2], *parameters).deliver!
0
+          when 'new'     then nil
0
           else super
0
         end
0
       end
0
@@ -424,6 +429,12 @@ module ActionMailer #:nodoc:
0
       def template_root=(root)
0
         self.view_paths = ActionView::Base.process_view_paths(root)
0
       end
0
+
0
+      private
0
+        def matches_dynamic_method?(method_name) #:nodoc:
0
+          method_name = method_name.to_s
0
+          /(create|deliver)_([_a-z]\w*)/.match(method_name) || /^(new)$/.match(method_name)
0
+        end
0
     end
0
 
0
     # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer
...
968
969
970
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
0
@@ -968,3 +968,55 @@ class MethodNamingTest < Test::Unit::TestCase
0
     end
0
   end
0
 end
0
+
0
+class RespondToTest < Test::Unit::TestCase
0
+  class RespondToMailer < ActionMailer::Base; end
0
+
0
+  def setup
0
+    set_delivery_method :test
0
+  end
0
+
0
+  def teardown
0
+    restore_delivery_method
0
+  end
0
+
0
+  def test_should_respond_to_new
0
+    assert RespondToMailer.respond_to?(:new)
0
+  end
0
+
0
+  def test_should_respond_to_create_with_template_suffix
0
+    assert RespondToMailer.respond_to?(:create_any_old_template)
0
+  end
0
+
0
+  def test_should_respond_to_deliver_with_template_suffix
0
+    assert RespondToMailer.respond_to?(:deliver_any_old_template)
0
+  end
0
+
0
+  def test_should_not_respond_to_new_with_template_suffix
0
+    assert !RespondToMailer.respond_to?(:new_any_old_template)
0
+  end
0
+
0
+  def test_should_not_respond_to_create_with_template_suffix_unless_it_is_separated_by_an_underscore
0
+    assert !RespondToMailer.respond_to?(:createany_old_template)
0
+  end
0
+
0
+  def test_should_not_respond_to_deliver_with_template_suffix_unless_it_is_separated_by_an_underscore
0
+    assert !RespondToMailer.respond_to?(:deliverany_old_template)
0
+  end
0
+
0
+  def test_should_not_respond_to_create_with_template_suffix_if_it_begins_with_a_uppercase_letter
0
+    assert !RespondToMailer.respond_to?(:create_Any_old_template)
0
+  end
0
+
0
+  def test_should_not_respond_to_deliver_with_template_suffix_if_it_begins_with_a_uppercase_letter
0
+    assert !RespondToMailer.respond_to?(:deliver_Any_old_template)
0
+  end
0
+
0
+  def test_should_not_respond_to_create_with_template_suffix_if_it_begins_with_a_digit
0
+    assert !RespondToMailer.respond_to?(:create_1_template)
0
+  end
0
+
0
+  def test_should_not_respond_to_deliver_with_template_suffix_if_it_begins_with_a_digit
0
+    assert !RespondToMailer.respond_to?(:deliver_1_template)
0
+  end
0
+end

Comments