Search code examples
ruby-on-railsrubyclass-methodinstance-methods

How can you call class methods on mailers when they're not defined as such?


When sending mail in Rails, usually one would do something like this:

UserMailer.password_reset(user).deliver

But if we look inside UserMailer we can see this:

def password_reset(user) # not self.password_reset
  # ...
end

Notice that the method name is not prefixed with self. Looking at it, it seems like you need to instantiate the object first as below. How does Rails do this?

UserMailer.new.password_reset(user).deliver

Solution

  • That's a great question. In the source (https://github.com/rails/rails/blob/master/actionmailer/lib/action_mailer/base.rb), Rails uses method_missing to create a new instance of the ActionMailer. Here's the relevant excerpt from the source:

    def method_missing(method_name, *args) # :nodoc:
      if respond_to?(method_name)
        new(method_name, *args).message
      else
        super
      end
    end