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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With