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

Returns:

  • (Boolean)


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

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

#email_templateObject



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

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

#notifiable?Boolean

Returns:

  • (Boolean)


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

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

#notify!(force: false) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/effective/poll_notification.rb', line 132

def notify!(force: false)
  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|
    Effective::PollsMailer.public_send(email_template, self, user).deliver_now
  end

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

#notify_now?Boolean

Returns:

  • (Boolean)


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

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

Returns:

  • (Boolean)


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

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

#poll_start?Boolean

Returns:

  • (Boolean)


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

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

#reminder?Boolean

Returns:

  • (Boolean)


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

def reminder?
  category == 'Reminder'
end

#to_sObject



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

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

#upcoming_reminder?Boolean

Returns:

  • (Boolean)


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

def upcoming_reminder?
  category == 'Upcoming reminder'
end