Module: WithReminders

Extended by:
ActiveSupport::Concern
Included in:
User
Defined in:
app/models/concerns/with_reminders.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#build_reminderObject



4
5
6
7
8
9
# File 'app/models/concerns/with_reminders.rb', line 4

def build_reminder
  mailer = UserMailer.new
  last_submission_date.nil? ?
    mailer.no_submissions_reminder(self) :
    mailer.we_miss_you_reminder(self, cycles_since(last_submission_date))
end

#remind!Object



11
12
13
14
# File 'app/models/concerns/with_reminders.rb', line 11

def remind!
  build_reminder.post!
  update! last_reminded_date: Time.current
end

#should_remind?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'app/models/concerns/with_reminders.rb', line 16

def should_remind?
  reminder_due? && (has_no_submissions? || has_no_recent_submission?)
end

#try_remind_with_lock!Object

Try to send a reminder, by acquiring a database lock for update the appropriate record. This object can’t be updated as long as the reminder is being sent.

This method is aimed to be sent across multiple servers or processed concurrently and still not send duplicate mails



26
27
28
# File 'app/models/concerns/with_reminders.rb', line 26

def try_remind_with_lock!
  with_pg_lock proc { remind! }, proc { should_remind? }
end