[英]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.