简体   繁体   English

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

[英]I have a rake task that send's out email notifications how can I group them

I have a rake task that sends out email notifications based on my entries table: 我有一个rake任务,它根据我的条目表发送电子邮件通知:

If approve_disapprove has a "3" (3 means 'pending') it will send out an email to the person who approves the entry that a entry is still pending. 如果approve_disapprove的值为“ 3”(3表示“待定”),它将向批准该条目的人发送电子邮件,表明该条目仍在等待处理。

The problem with this is if there are multiple entries it will go through and find each entry with a 3 for approve_disapprove and send out an email for each one. 问题是,如果有多个条目,它将通过并为approve_disapprove查找带有3的每个条目,并为每个条目发送一封电子邮件。

So if I have 5 entries and then when my task goes through the next day and sees they are still marked as 3 it will send 5 emails to the approver. 因此,如果我有5个条目,然后当我的任务在第二天完成并看到它们仍标记为3时,它将向批准者发送5封电子邮件。

How can I group this so it chunks all of them together based off my other column in my entry table called section. 如何将其分组,以便根据我在条目表中称为section的另一列将它们全部分组。 If it chunked or grouped all of the entries with a 3 for pending and grouped them by section name it would only send one email with all 5 request in it to the manager with that section? 如果它对所有条目进行了分块或分组(带有3个待处理条目),并按部门名称分组,那么它只会向该部门的经理发送一封电子邮件,其中包含所有5个请求?

Here is my entry model that has the check_pending task 这是我的具有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

This is my entry mailer for check pending 这是我的待审核支票邮件

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

and this is my check_pending mailer view 这是我的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

And this is the rake task 这是耙任务

 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

extra info 额外信息

Entry table columns 条目表列

  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"

The problem here is that you need the entry object to get the information about the entry, so you'd have to re-write the logic of the view. 这里的问题是您需要入口对象来获取有关入口的信息,因此您必须重新编写视图的逻辑。

Firstly, define a couple of scopes in your Entry model: 首先,在您的Entry模型中定义几个范围:

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

then change your rake task to group on section and pass in a relation, rather than a single entry: 然后将您的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

You'll then need to modify your mailer: 然后,您需要修改邮件程序:

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

And finally your view will need to iterate through these: 最后,您的视图将需要遍历以下内容:

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
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM