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.

Bug(?) - ActionMailer responds_to? failing to return true/false bug

#2557

Hi,
Code base - Rails 2.3.2

When testing the action mailer object for an existing and non-existant method the object returned is a MatchData class. I've played around in actionmailer/lib/action_mailer/base.rb but haven't determined exactly why this is happening yet.

To replicate this issue:

rails test_mail

cd test_mail

ruby script/generate mailer Mail

Add the following method to app/models/mail.rb: def self.test_method puts "do nothing" end

def test_email puts "this is accessed by Mail.deliver_test_email!" end

ruby script/console

Loading development environment (Rails 2.3.2)

Mail.respond_to?(:something)

=> false

Mail.respond_to?(:deliver_anything)

=> #<MatchData:0x24509f0>

Mail.respond_to?(:create_anything)

=> #<MatchData:0x244e664>

Mail.respond_to?(:test_method)

=> true

Mail.respond_to?(:test_email)

=> false

Mail.respond_to?(:deliver_test_email)

=> #<MatchData:0x2450680>

Mail.respond_to?(:deliver_test_email!)

=> #<MatchData:0x244e1b4>

The workaround I'm using at the moment is testing the method name (without deliver/create) is present in the instance_methods:

Mail.instance_method.include?(:test_email)

But i don't think this is the way i should be testing the existance of a dynamic method on the action mailer object.

Please let me know if i'm looking at this all wrong. Any other info required i'd be happy to help.

Best regards,

Tom

Reported by PommyTom · April 24th, 2009 @ 10:00 AM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. Frederick Cheung
    Frederick Cheung
    • Tag changed from 2.3.2, actionmailer, base, rails, respond_to to 2.3.2, actionmailer, base, rails, respond_to

    Does it really matter? ruby will happily consider anything other than nil/false as true for the purposes of an if statement and so on.

    May 7th, 2009 @ 11:25 PM

  2. PommyTom
    PommyTom

    Hi Frederick,

    Yeah, that would be fine normally, as it responds with true just for having a value. But the problem with this instance as described above, it is returning a object for any values that start with the dynamic method starts (such as deliver... or create...) so with a matchdata being returned it will always return true even though the method doesn't actually exist (matchdata also returned when the method does exist such as deliver_test_email in the example above).

    Any other info you require, please let me know.

    Best regards,

    Tom

    May 14th, 2009 @ 02:25 AM

  3. Frederick Cheung
    Frederick Cheung

    Ah, I'm with you now, it's not actually checking whether foo exists when handling respond_to 'deliver_foo'

    May 15th, 2009 @ 01:43 PM

  4. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  5. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open
    • Tag changed from 2.3.2, actionmailer, base, rails, respond_to to 232, actionmailer, base, rails, respond_to
    • Importance changed from to

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 05:02 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 05:02 PM