我有一个rake任务发送电子邮件通知我如何对它们进行分组

时间:2015-04-22 16:02:48

标签: ruby-on-rails ruby ruby-on-rails-4

我有一个rake任务,根据我的条目表发送电子邮件通知:

如果approve_disapprove有一个“3”(3表示“待处理”),它将向批准该条目仍然未决的条目的人发送一封电子邮件。

问题在于,如果有多个条目,它将通过并查找每个条目,其中3为approve_disapprove,并为每个条目发送一封电子邮件。

因此,如果我有5个条目,然后当我的任务经过第二天并且看到它们仍被标记为3时,它将向批准者发送5封电子邮件。

我如何对此进行分组,以便根据我的名为section的条目表中的其他列将所有这些组合在一起。如果它对所有条目进行分块或分组,其中3表示待处理,并按部分名称对它们进行分组,那么它只会向该部分的经理发送一封包含所有5个请求的电子邮件?

这是我的条目模型,它具有check_pending任务

def self.check_pending # What this does is goes through each entry and looks at approve_disapprove if its a 3 which is pending it will sent an alert to the employees manager. 
  check_pending = Entry.where(approve_disapprove: 3)                      
  check_pending.each do |entry|
      EntryMailer.check_pending(entry).deliver
  end
end

这是我的报名待处理邮件

class EntryMailer < ActionMailer::Base

  def check_pending(entry)
    @entry = entry

    mail to: @entry.emp_mail_addr, subject: '(TEST) You have pending time off requests that require approval or disapproval'
  end
end

这是我的check_pending邮件程序视图

Hello

#{@entry.mgr_name}


 The following time off request are pending please approve or disapprove once you have made your decision.


 %li
  %span.u= @entry.emp_first_name
  %span.u= @entry.emp_last_name
 %li Dept
 %li= @entry.emp_dept
 %li Leave Start
 %li= @entry.leave_start.strftime('%m/%d/%y')
 %li Leave End
 %li= @entry.leave_end.strftime('%m/%d/%y')
 %li Type Of Request
 %li= @entry.indirect_id

这是佣金任务

 desc "Looks at pending request if the are still marked pending sends a email to the manager for the request"
 task :check_pending => :environment do
   Rails.logger.info "Check Pending: Finding all pending."
   Entry.check_pending
   Rails.logger.info "Found Pending: Check complete."
   Rails.logger.flush
 end

额外信息

条目表列

  table "entries"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "emp_id"
    t.string   "emp_no"
    t.string   "emp_first_name"
    t.string   "emp_last_name"
    t.string   "emp_mail_addr"
    t.string   "indirect_id"
    t.string   "mgr_no"
    t.string   "mgr_first_name"
    t.string   "mgr_last_name"
    t.string   "mgr_mail_addr"
    t.datetime "leave_start"
    t.string   "employee_type"
    t.integer  "seq_no",           
    t.decimal  "range_days",        
    t.string   "alt_mgr_no"
    t.string   "alt_mgr_name"
    t.string   "alt_mgr_addr"
    t.string   "emp_dept"
    t.datetime "leave_end"
    t.string   "approve_disapprove"
    t.string   "section"

1 个答案:

答案 0 :(得分:1)

这里的问题是您需要输入对象来获取有关条目的信息,因此您必须重新编写视图的逻辑。

首先,在Entry模型中定义几个范围:

class Entry < ActiveRecord::Base #I'm assuming AR here
  scope :pending, -> { where(approve_disapprove: 3) }
  scope :for_section, ->(section) { where(section: section) }
end

然后将您的rake任务更改为分组并传递关系,而不是单个条目:

def self.check_pending 
  sections = Entry.pending.pluck(:section).uniq                     
  sections.each do |section|
      entries = Entry.pending.for_section(section)
      EntryMailer.check_pending(entries).deliver
  end
end

然后您需要修改邮件:

class EntryMailer < ActionMailer::Base

  def check_pending(entries)
    @entries = entries
    emails = @entries.map(&:emp_mail_addr).uniq.join(',') #may not need this if your email will always be the same, could just grab the first @entries.first.enp_addr
    mail to: emails, subject: '(TEST) You have pending time off requests that require approval or disapproval'
  end
end

最后你的观点将需要迭代这些:

Hello

#{@entries.first.mgr_name}


 The following time off request are pending please approve or disapprove once you have made your decision.

- @entries.each do |entry|

  %li
    %span.u= entry.emp_first_name #note change from @entry to entry
    ...