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.deliver
  update! last_reminded_date: Time.now
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 aproppriate 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
29
30
31
32
33
34
35
36
37
38
# File 'app/models/concerns/with_reminders.rb', line 26

def try_remind_with_lock!
  # Some notes:
  #
  # * nowait is a postgre specific option and may not work with other databases
  # * nowait will raise an exception if the lock can not be acquired
  # * we are using a double check lock pattern to reduce lock acquisition
  with_lock('for update nowait') do
    reload
    remind! if should_remind?
  end if should_remind?
rescue
  nil
end