简体   繁体   中英

In Rails how do I turn off an email reminder job

I have a client complaining about email reminders being sent out. So I've looked through the code and I think there are a couple of locations I can probably try and address.

Reminder Emails Controller which as:

class Admin::ReminderEmailsController < Admin::ApplicationController

def edit
 @system_email = SystemEmail.first
end

def update
 @system_email = SystemEmail.first
 flash[:notice] = 'System Email updated successfully' if @system_email.update_attributes(system_email_params)
 respond_with @system_email, location: admin_system_emails_path
end

private

def system_email_params
  params.require(:system_email)
       .permit(:reminder_content)
end
end

Users Controller that includes:

  def send_training_email(user)
   future_date = DateTime.current + 10.days
   TrainingReminderJob.set(wait_until: future_date).perform_later(user)
 end

Training Reminder Jobs

class TrainingReminderJob < ApplicationJob
 queue_as :default

 def perform(user)
  UserAgentMailer.training_reminder(user).deliver_later
 end
end

My inclination is to just mess with training reminder jobs as it seems the least invasive. Would just adding in under perform be enough?:

UserAgentMailer.perform_deliveries = false

Typically, you would add a boolean column to the User model such as skip_reminder_email and make it admin and/or user changeable through your UI. Then test for that in your send_training_email method.

$ rails g migration AddSkipReminderEmailToUser skip_reminder_email:boolean
$ rake db:migrate

then in your method:

def send_training_email(user)
  unless user.skip_reminder_email
    future_date = DateTime.current + 10.days
    TrainingReminderJob.set(wait_until: future_date).perform_later(user)
  end
end

Again, you need a way for the admin or user to update the skip_reminder_email boolean to true or false. To test in development though, you could just update the flag from the rails console:

irb> User.find(user_id).update(skip_reminder_email: true)

And then run the controller action in UsersController that calls send_training_email .

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.

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