#700 new
James Mead

ActionMailer::Base should respond to methods handled by method_missing

Reported by James Mead | July 24th, 2008 @ 06:43 PM | in 2.x

ActionMailer::Base uses method_missing to handle dynamic create & deliver methods, but does not alter respond_to? to include the handled methods. So for example:

TopicMailer.deliver_activation_email # => although this works
TopicMailer.respond_to?(:deliver_activation_email) # => this returns false

It's my understanding that if you implement method_missing on a class to handle extra methods, you should add a corresponding implementation of respond_to? which recognizes the methods that method_missing will handle. Dan Manges has a nice explanation on his blog. If this were fixed, the above example becomes:

TopicMailer.deliver_activation_email # => this still works
TopicMailer.respond_to?(:deliver_activation_email) # => this returns true

I have a practical reason for wanting this fixed. I've added functionality to Mocha which allows you to get warnings or errors when stubbing non-existent methods. The problem is that when running in a Rails project, false negatives are produced when you are legitimately stubbing ActionMailer create/deliver methods. I think this functionality could be really useful in catching incorrectly stubbed methods.

The attached patch fixes this problem. It includes tests which fail against the current implementation and tests that check that original behaviour is preserved.

This ticket corresponds to a similar patch applied to ActiveRecord::Base.

I wonder whether the method_missing interception of the "new" method is really necessary - there is only one (seemingly insignificant) test failure if this is removed. Also it would be good if method_missing and thus respond_to? checked whether the requested template existed - in a similar manner to ActiveRecord::Base checking whether a specific dynamic finder exists depending on attributes.

Comments and changes to this ticket

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Source available from github

The Git repository resides at http://github.com/rails

Check out the current development trunk (Edge Rails) with:

git clone git://github.com/rails/rails.git

The latest development for the 1.2.x and 2.0.x releases are on the 1-2-stable and 2-0-stable branches.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".

Shared Ticket Bins