Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActionMailer::Base should respond to methods handled by method_missing

#700

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.

Reported by James Mead · July 25th, 2008 @ 01:07 AM

State: resolved
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Clemens Kofler
    Clemens Kofler
    • Tag changed from mailer, patch to actionmailer, bug, patch, tested, tests

    +1. Plus we should probably investigate if the same happens as well for other cases where method_missing is used.

    July 25th, 2008 @ 02:17 AM

  2. Luke Redpath
    Luke Redpath

    +1 - if it quacks like a duck, it should know that it can quack like a duck.

    August 20th, 2008 @ 09:31 AM

  3. James Adam
    James Adam

    +1 - This seems like a no-brainer win.

    August 20th, 2008 @ 09:54 AM

  4. Chris Roos
    Chris Roos
    1. The current implementation generates a lot of unnecessary noise when you disable the stubbing of non existent methods in Mocha.

    August 20th, 2008 @ 10:01 AM

  5. Paul Battley
  6. Repository
    Repository
    • State changed from new to resolved

    (from [3cf773b187e803e16b8237e5923fa4c1139cde8a]) ActionMailer should respond_to? to methods handled by method_missing [#700 ActionMailer::Base should respond to methods handled by method_missing state:resolved]

    Signed-off-by: Joshua Peek josh@joshpeek.com http://github.com/rails/rails/co...

    August 29th, 2008 @ 09:10 PM