From 454e20b725a94ec340b553d8bf7e48466f2fdb41 Mon Sep 17 00:00:00 2001 From: James Mead Date: Thu, 24 Jul 2008 23:26:46 +0100 Subject: [PATCH] ActionMailer should respond_to? to methods handled by method_missing. --- actionmailer/lib/action_mailer/base.rb | 19 +++++++++-- actionmailer/test/respond_to_test.rb | 55 ++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 actionmailer/test/respond_to_test.rb diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index a432964..3a64199 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -377,11 +377,16 @@ module ActionMailer #:nodoc: alias_method :controller_name, :mailer_name alias_method :controller_path, :mailer_name + def respond_to?(method_symbol, include_private = false)#:nodoc: + matches_dynamic_method?(method_symbol.id2name) || super + end + def method_missing(method_symbol, *parameters)#:nodoc: - case method_symbol.id2name - when /^create_([_a-z]\w*)/ then new($1, *parameters).mail - when /^deliver_([_a-z]\w*)/ then new($1, *parameters).deliver! - when "new" then nil + match = matches_dynamic_method?(method_symbol.id2name) + case match[1] + when 'create' then new(match[2], *parameters).mail + when 'deliver' then new(match[2], *parameters).deliver! + when 'new' then nil else super end end @@ -429,6 +434,12 @@ module ActionMailer #:nodoc: root = ActionView::PathSet::Path.new(root) if root.is_a?(String) write_inheritable_attribute(:template_root, root) end + + private + def matches_dynamic_method?(method_name)#:nodoc: + /(create|deliver)_([_a-z]\w*)/.match(method_name) || /^(new)$/.match(method_name) + end + end # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer diff --git a/actionmailer/test/respond_to_test.rb b/actionmailer/test/respond_to_test.rb new file mode 100644 index 0000000..a4d333e --- /dev/null +++ b/actionmailer/test/respond_to_test.rb @@ -0,0 +1,55 @@ +require 'abstract_unit' + +class RespondToMailer < ActionMailer::Base; end + +class RespondToTest < Test::Unit::TestCase + + def setup + set_delivery_method :test + end + + def teardown + restore_delivery_method + end + + def test_should_respond_to_new + assert RespondToMailer.respond_to?(:new) + end + + def test_should_respond_to_create_with_template_suffix + assert RespondToMailer.respond_to?(:create_any_old_template) + end + + def test_should_respond_to_deliver_with_template_suffix + assert RespondToMailer.respond_to?(:deliver_any_old_template) + end + + def test_should_not_respond_to_new_with_template_suffix + assert !RespondToMailer.respond_to?(:new_any_old_template) + end + + def test_should_not_respond_to_create_with_template_suffix_unless_it_is_separated_by_an_underscore + assert !RespondToMailer.respond_to?(:createany_old_template) + end + + def test_should_not_respond_to_deliver_with_template_suffix_unless_it_is_separated_by_an_underscore + assert !RespondToMailer.respond_to?(:deliverany_old_template) + end + + def test_should_not_respond_to_create_with_template_suffix_if_it_begins_with_a_uppercase_letter + assert !RespondToMailer.respond_to?(:create_Any_old_template) + end + + def test_should_not_respond_to_deliver_with_template_suffix_if_it_begins_with_a_uppercase_letter + assert !RespondToMailer.respond_to?(:deliver_Any_old_template) + end + + def test_should_not_respond_to_create_with_template_suffix_if_it_begins_with_a_digit + assert !RespondToMailer.respond_to?(:create_1_template) + end + + def test_should_not_respond_to_deliver_with_template_suffix_if_it_begins_with_a_digit + assert !RespondToMailer.respond_to?(:deliver_1_template) + end + +end \ No newline at end of file -- 1.5.3.1