Class: Effective::PollNotification

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/effective/poll_notification.rb

Constant Summary collapse

CATEGORIES =
['Upcoming reminder', 'When poll starts', 'Reminder', 'Before poll ends', 'When poll ends']
EMAIL_TEMPLATE_VARIABLES =
['available_date', 'title', 'url', 'user.name', 'user.email']
UPCOMING_REMINDERS =
{
  '1 hour before' => 1.hours.to_i,
  '3 hours before' => 3.hours.to_i,
  '6 hours before' => 6.hours.to_i,
  '12 hours before' => 12.hours.to_i,
  '1 day before' => 1.days.to_i,
  '2 days before' => 2.days.to_i,
  '3 days before' => 3.days.to_i,
  '4 days before' => 4.days.to_i,
  '5 days before' => 5.days.to_i,
  '6 days before' => 6.days.to_i,
  '1 week before' => 1.weeks.to_i,
  '2 weeks before' => 2.weeks.to_i,
  '3 weeks before' => 3.weeks.to_i,
  '1 month before' => 1.month.to_i
}
REMINDERS =
{
  '1 hour after' => 1.hours.to_i,
  '3 hours after' => 3.hours.to_i,
  '6 hours after' => 6.hours.to_i,
  '12 hours after' => 12.hours.to_i,
  '1 day after' => 1.days.to_i,
  '2 days after' => 2.days.to_i,
  '3 days after' => 3.days.to_i,
  '4 days after' => 4.days.to_i,
  '5 days after' => 5.days.to_i,
  '6 days after' => 6.days.to_i,
  '1 week after' => 1.weeks.to_i,
  '2 weeks after' => 2.weeks.to_i,
  '3 weeks after' => 3.weeks.to_i,
  '1 month after' => 1.month.to_i
}

Instance Method Summary collapse

Instance Method Details

#before_poll_ends?Boolean



104
105
106
# File 'app/models/effective/poll_notification.rb', line 104

def before_poll_ends?
  category == 'Before poll ends'
end

#email_templateObject



84
85
86
# File 'app/models/effective/poll_notification.rb', line 84

def email_template
  'poll_' + category.to_s.parameterize.underscore
end

#email_template_variablesObject



88
89
90
# File 'app/models/effective/poll_notification.rb', line 88

def email_template_variables
  EMAIL_TEMPLATE_VARIABLES
end

#notifiable?Boolean



112
113
114
# File 'app/models/effective/poll_notification.rb', line 112

def notifiable?
  started_at.blank? && completed_at.blank?
end

#notify!(force: false, except: []) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/models/effective/poll_notification.rb', line 135

def notify!(force: false, except: [])
  raise('expected an Array of user IDs') if except.present? && !except.kind_of?(Array)

  return false unless (notify_now? || force)

  # We send to all users, except for the 'Reminder' that exclude completed users
  users = poll.users(except_completed: (category == 'Reminder' || category == 'Before poll ends'))

  update_column(:started_at, Time.zone.now)

  users.find_each do |user|
    print '.'

    next if except.include?(user.id)

    begin
      Effective::PollsMailer.public_send(email_template, self, user).deliver_now
    rescue => e
      EffectiveLogger.error(e.message, associated: self) if defined?(EffectiveLogger)
      ExceptionNotifier.notify_exception(e, data: { user_id: user.id, poll_notification_id: id }) if defined?(ExceptionNotifier)
      raise(e) if Rails.env.test? || Rails.env.development?
    end

  end

  update_column(:completed_at, Time.zone.now)
end

#notify_now?Boolean



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/effective/poll_notification.rb', line 116

def notify_now?
  return false unless notifiable?

  case category
  when 'When poll starts'
    poll.available?
  when 'When poll ends'
    poll.ended?
  when 'Upcoming reminder'
    !poll.started? && poll.start_at < (Time.zone.now + reminder)
  when 'Reminder'
    !poll.ended? && poll.start_at < (Time.zone.now - reminder)
  when 'Before poll ends'
    !poll.ended? && poll.end_at.present? && poll.end_at < (Time.zone.now + reminder)
  else
    raise('unexpected category')
  end
end

#poll_end?Boolean



108
109
110
# File 'app/models/effective/poll_notification.rb', line 108

def poll_end?
  category == 'When poll ends'
end

#poll_start?Boolean



96
97
98
# File 'app/models/effective/poll_notification.rb', line 96

def poll_start?
  category == 'When poll starts'
end

#reminder?Boolean



100
101
102
# File 'app/models/effective/poll_notification.rb', line 100

def reminder?
  category == 'Reminder'
end

#to_sObject



80
81
82
# File 'app/models/effective/poll_notification.rb', line 80

def to_s
  [category.presence, subject.presence].compact.join(' - ') || model_name.human
end

#upcoming_reminder?Boolean



92
93
94
# File 'app/models/effective/poll_notification.rb', line 92

def upcoming_reminder?
  category == 'Upcoming reminder'
end