Skip to content
This repository
Browse code

ActionMailer should respond_to? to methods handled by method_missing [#…

…700 state:resolved]

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

0 notes on commit 3cf773b

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