[英]I have a rake task that send's out email notifications how can I group them
我有一个rake任务,它根据我的条目表发送电子邮件通知:
如果approve_disapprove的值为“ 3”(3表示“待定”),它将向批准该条目的人发送电子邮件,表明该条目仍在等待处理。
问题是,如果有多个条目,它将通过并为approve_disapprove查找带有3的每个条目,并为每个条目发送一封电子邮件。
因此,如果我有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"
这里的问题是您需要入口对象来获取有关入口的信息,因此您必须重新编写视图的逻辑。
首先,在您的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
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.