3

给定班级:

class UserMailer < ActionMailer::Base
  default from: "do-not-reply@mark.com"
  def contact_email(contact)
    @contact = contact
    mail(to: 'm@mark.com', from: @contact.email, subject: "Website Contact")
  end
end

和以下测试代码:

c = Contact.new
UserMailer.contact_email(c)

这段代码是如何工作的?我以为我的contact_email 是一个实例方法,但它被作为类方法调用,并且它有效。

感谢您的帮助 - 在我学习 Ruby 和 Rails 时:)

-标记

4

2 回答 2

5

乍一看,这看起来是错误的,这是完全正确的。

它之所以有效,是因为类上有一个看起来像这样method_missing 的(参见源代码)

  def method_missing(method_name, *args) # :nodoc:
    if action_methods.include?(method_name.to_s)
      MessageDelivery.new(self, method_name, *args)
    else
      super
    end
  end

action_methods基本上是与可以发送的电子邮件相对应的邮件方法的名称,并且MessageDelivery是一个最终会执行的小代理类

YourMailer.new.send(:contact_mailer, ...)

在我的脑海中,我不完全确定为什么要这样做,但是实例方法代理的基本类方法从 actionmailer 的早期就以一种或另一种形式存在

于 2015-01-13T19:14:42.403 回答
0

检查来源

def method_missing(method_name, *args) # :nodoc:
  if action_methods.include?(method_name.to_s)
     MessageDelivery.new(self, method_name, *args)
  else
     super
  end
end

示例实现:

class MyMailer
  def self.method_missing(method, *args)
    puts "Here, I can call any instance method" 
  end

  def sending_mail_for_you 
    puts "I am actually sending mail for you"
  end  

end
#notice, fake_method is not defined in the MyMailer class.
MyMailer.fake_method
This will give output: 
=> "Here, I can call any instance method"
"I am actually sending mail for you"

ActionMailer::Base 做了类似上面代码的事情。即使我们在执行 method_missing 部分时仍然没有调用任何此类方法"fake_method",它也会在内部调用您的'sending_mail_for_you'方法。

于 2015-01-13T19:22:02.387 回答