ActionMailer w / Custom Rake Task

时间:2016-09-02 19:44:31

标签: ruby-on-rails rake actionmailer rake-task

我正在尝试在Rails 4应用程序中创建两个自定义rake任务。他们应该读取agent_card表并根据逻辑,在触发时间时触发电子邮件。这是我第一次创建rake任务而且我没有收到错误,因此我不知道我是否正确行事。我会假设在使用邮件时,一旦邮件被解雇,开信刀就会起作用(如果一切正常),但我不确定。我在下面发布了所有相关代码。提前谢谢!

.rake file

namespace :agent_cards do
   desc 'Sends an email to an agent with a license expiring 2 months from today'
   task license_expire_agent: :environment do
      Rails.logger.info "Mailer Method #{ActionMailer::Base.delivery_method}"
      AgentCard.all.each do |agent_card|
         if agent_card.real_estate_license_expires_at == Date.today + 2.months
            LicenseExpireMailer.license_expire_agent(@agent_card, @agent).deliver_later
         end
      end
   end
end

namespace :agent_cards do
   desc 'Sends an email to an the agent development manager when a license expires 1 week from today'
   task license_expire_mgr: :environment do
      Rails.logger.info "Mailer Method #{ActionMailer::Base.delivery_method}"
      AgentCard.all.each do |agent_card|
         if agent_card.real_estate_license_expires_at == Date.today + 7
            LicenseExpireMailer.license_expire_mgr(@agent_card, @agent).deliver_later
         end
      end
   end
end

license_expire_agent_mailer.rb

class LicenseExpireMailer < ActionMailer::Base
   default from: "Mike <help@mike.com>"

   def license_expire_agent(agent, agent_card)
      @agent = agent
      @agent_card = agent_card
      mail to: "mike@mike.com", subject: 'Your license is about to expire!'
   end
 end

license_expire_mgr_mailer.rb

class LicenseExpireMailer < ActionMailer::Base
   default from: "Mike <help@mike.com>"

   def license_expire_mgr(agent, agent_card)
      @agent = agent
      @agent_card = agent_card
      # Head of Agent Development
      mail to: "mike@mike.com", subject: "#{@agent.name}'s License Expiring"
   end
end

当我尝试测试rake任务时,我输入

rake agent_cards:license_expire_agent

or

rake agent_cards:license_expire_mgr

错误

NameError: uninitialized constant LicenseExpireMailer
/Users/michaelwiesenhart/Code/lib/tasks/license_expiration.rake:7:in `block (3      levels) in <top (required)>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/activerecord-   4.2.6/lib/active_record/relation/delegation.rb:46:in `each'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/activerecord-4.2.6/lib/active_record/relation/delegation.rb:46:in `each'
/Users/michaelwiesenhart/Code/lib/tasks/license_expiration.rake:5:in `block (2 levels) in <top (required)>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:248:in `block in execute'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:243:in `each'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:243:in `execute'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:187:in `block in invoke_with_call_chain'
/Users/michaelwiesenhart/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:180:in `invoke_with_call_chain'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:173:in `invoke'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:150:in `invoke_task'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `each'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `block in top_level'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:115:in `run_with_threads'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:100:in `top_level'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:78:in `block in run'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:176:in `standard_exception_handling'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:75:in `run'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/bin/rake:33:in  `<top (required)>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/rake:23:in `load'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/rake:23:in `<main>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => agent_cards:license_expire_agent

1 个答案:

答案 0 :(得分:1)

根据Rails约定,您的邮件程序文件名与邮件程序类名称不匹配。

Mailer类应该放在这条路径中:

app/mailers/license_expire_mailer.rb