ruby on rails - I have a rake task that send's out email notifications how can I group them -
i have rake task sends out email notifications based on entries table:
if approve_disapprove has "3" (3 means 'pending') send out email person approves entry entry still pending.
the problem if there multiple entries go through , find each entry 3 approve_disapprove , send out email each one.
so if have 5 entries , when task goes through next day , sees still marked 3 send 5 emails approver.
how can group chunks of them based off other column in entry table called section. if chunked or grouped of entries 3 pending , grouped them section name send 1 email 5 request in manager section?
here entry model has check_pending task
def self.check_pending # goes through each entry , looks @ approve_disapprove if 3 pending sent alert employees manager. check_pending = entry.where(approve_disapprove: 3) check_pending.each |entry| entrymailer.check_pending(entry).deliver end end
this entry mailer check pending
class entrymailer < actionmailer::base def check_pending(entry) @entry = entry mail to: @entry.emp_mail_addr, subject: '(test) have pending time off requests require approval or disapproval' end end
and check_pending mailer view
hello #{@entry.mgr_name} following time off request pending please approve or disapprove once have made 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 rake task
desc "looks @ pending request if still marked pending sends email manager request" task :check_pending => :environment rails.logger.info "check pending: finding 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 need entry object information entry, you'd have re-write logic of view.
firstly, define couple of scopes in 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 rake task group on section , pass in relation, rather single entry:
def self.check_pending sections = entry.pending.pluck(:section).uniq sections.each |section| entries = entry.pending.for_section(section) entrymailer.check_pending(entries).deliver end end
you'll need modify mailer:
class entrymailer < actionmailer::base def check_pending(entries) @entries = entries emails = @entries.map(&:emp_mail_addr).uniq.join(',') #may not need if email same, grab first @entries.first.enp_addr mail to: emails, subject: '(test) have pending time off requests require approval or disapproval' end end
and view need iterate through these:
hello #{@entries.first.mgr_name} following time off request pending please approve or disapprove once have made decision. - @entries.each |entry| %li %span.u= entry.emp_first_name #note change @entry entry ...
Comments
Post a Comment