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', '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

#email_templateObject



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

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

#notifiable?Boolean

Returns:

  • (Boolean)


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

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

#notify!Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/models/effective/poll_notification.rb', line 128

def notify!
  return false unless notify_now?

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

  update_column(:started_at, Time.zone.now)

  users.find_each do |user|
    Effective::PollsMailer.public_send(email_template, self, user).deliver_now
  end

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

#notify_now?Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/effective/poll_notification.rb', line 111

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)
  else
    raise('unexpected category')
  end
end

#poll_end?Boolean

Returns:

  • (Boolean)


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

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

#poll_start?Boolean

Returns:

  • (Boolean)


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

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

#reminder?Boolean

Returns:

  • (Boolean)


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

def reminder?
  category == 'Reminder'
end

#to_sObject



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

def to_s
  'poll notification'
end

#upcoming_reminder?Boolean

Returns:

  • (Boolean)


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

def upcoming_reminder?
  category == 'Upcoming reminder'
end