Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 423
dee Avatar asked Jun 13 '13 13:06

dee


1 Answers

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
like image 55
Rebitzele Avatar answered Oct 19 '22 23:10

Rebitzele