I have a rake task that sends out email notifications based on my entries table:
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.
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.
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.
How can I group this so it chunks all of them together based off my other column in my entry table called 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?
Here is my entry model that has the check_pending task
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
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:
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:
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
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.