I am using Rails with Devise confirmable
. When users sign up Devise automatically sends them a confirmation email. If they haven't confirmed after three days, I would like to send them another confirmation email with a new token. If they still do not confirm after this second email, we should send them no more. Mapped out on a timeline:
I'd rather not add a new column to my Users model that keeps track of how many times they've been sent a confirmation email since I think I can do it in a Worker that runs every day using the information we already have. However I'm struggling a bit with getting the logic right.
Here's what I have so far:
# workers/resend_confirmation_worker.rb
class ResendConfirmationWorker
include Sidekiq::Worker
sidekiq_options queue: :resend_confirmation, retry: false
def perform
users = User.where('confirmation_sent_at IS NOT NULL and
confirmed_at IS NULL')
users.each do |user|
return if user.created_at < Time.zone.now - 4.days || user.created_at > Time.zone.now - 3.days
user.resend_confirmation!
end
end
# config/clock.rb
module Clockwork
every(1.day, 'Resend confirmation') { ResendConfirmationWorker.perform_async }
end
Appreciate any help or suggestions for improvement.
d = 2.days.ago
users = User.where.not(confirmation_sent_at: nil)
.where(confirmed_at: nil)
.where(created_at: d.beginning_of_day..d.end_of_day)
users.find_each do |user|
user.resend_confirmation!
end