Class: Message

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/message.rb

Constant Summary collapse

POSSIBLE_TEMPLATES =
["default"].freeze
POSSIBLE_TYPES =
["bulk", "automated"].freeze
POSSIBLE_SIMPLE_RECIPIENTS =
{
  "all"                              => "Everyone",
  "incomplete"                       => "Incomplete Applications",
  "complete"                         => "Complete Applications",
  "accepted"                         => "Accepted Applications",
  "denied"                           => "Denied Applications",
  "waitlisted"                       => "Waitlisted Applications",
  "late-waitlisted"                  => "Late, Waitlisted Applications",
  "rsvp-confirmed"                   => "RSVP Confirmed Attendees",
  "rsvp-denied"                      => "RSVP Denied Attendees",
  "checked-in"                       => "Checked-In Attendees",
  "non-checked-in"                   => "Non-Checked-In, Accepted & RSVP'd Applications",
  "non-checked-in-excluding"         => "Non-Checked-In Applications, Excluding Accepted & RSVP'd"
}.freeze
POSSIBLE_TRIGGERS =
{
  "questionnaire.pending"        => "Questionnaire Status: Pending Review (new application)",
  "questionnaire.accepted"       => "Questionnaire Status: Accepted",
  "questionnaire.waitlist"       => "Questionnaire Status: Waitlisted",
  "questionnaire.denied"         => "Questionnaire Status: Denied",
  "questionnaire.late_waitlist"  => "Questionnaire Status: Waitlisted, Late",
  "questionnaire.rsvp_confirmed" => "Questionnaire Status: RSVP Confirmed",
  "questionnaire.rsvp_denied"    => "Questionnaire Status: RSVP Denied"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.automatedObject



147
148
149
# File 'app/models/message.rb', line 147

def self.automated
  where(type: 'automated')
end

.bulkObject



143
144
145
# File 'app/models/message.rb', line 143

def self.bulk
  where(type: 'bulk')
end

.inheritance_columnObject



151
152
153
# File 'app/models/message.rb', line 151

def self.inheritance_column
  "class_type"
end

.queue_for_trigger(trigger, user_id) ⇒ Object



138
139
140
141
# File 'app/models/message.rb', line 138

def self.queue_for_trigger(trigger, user_id)
  messages_to_queue = Message.where(trigger: trigger)
  messages_to_queue.map { |message| Mailer.delay.bulk_message_email(message.id, user_id) }
end

Instance Method Details

#automated?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'app/models/message.rb', line 64

def automated?
  type == "automated"
end

#bulk?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'app/models/message.rb', line 60

def bulk?
  type == "bulk"
end

#can_edit?Boolean

Returns:

  • (Boolean)


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

def can_edit?
  automated? || status == "drafted"
end

#can_queue?Boolean

Returns:

  • (Boolean)


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

def can_queue?
  status == "drafted" && recipients_list.present?
end

#delivered?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'app/models/message.rb', line 68

def delivered?
  delivered_at.present?
end

#possible_recipientsObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/models/message.rb', line 100

def possible_recipients
  # Produce an array like:
  # ["School: My University", "school::123"]
  option = ->(query, model) { [MessageRecipientQuery.friendly_name(query, model), query] }
  bus_list_recipients = BusList.select(:id, :name).map do |bus_list|
    [
      option.call("bus-list::#{bus_list.id}", bus_list)
    ]
  end
  bus_list_recipients.flatten!(1) # Required since we have multiple options for each bus list

  school_recipients = School.select(:id, :name).map do |school|
    option.call("school::#{school.id}", school)
  end
  # No flatten needed here since each map returns a single option

  blazer_recipients = Blazer::Query.select(:id, :name).map do |query|
    option.call("blazer::#{query.id}", query)
  end
  # No flatten needed here since each map returns a single option

  # Combine all recipients. push(*recipients) is the most efficient,
  # as it doesn't create a new array each time (concat() does)
  recipients = POSSIBLE_SIMPLE_RECIPIENTS.invert.to_a
  recipients.push(*bus_list_recipients)
  recipients.push(*school_recipients)
  recipients.push(*blazer_recipients)

  # Add current recipients if not included
  self.recipients.each do |recipient|
    if recipients.none? { |recipient_pair| recipient_pair[1] == recipient }
      recipients.push([recipient, recipient])
    end
  end

  recipients
end

#queued?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'app/models/message.rb', line 76

def queued?
  queued_at.present?
end

#recipients=(values) ⇒ Object



43
44
45
# File 'app/models/message.rb', line 43

def recipients=(values)
  values.present? ? super(values.reject(&:blank?)) : super(values)
end

#recipients_listObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/message.rb', line 47

def recipients_list
  labels = recipients.map do |r|
    if POSSIBLE_SIMPLE_RECIPIENTS.include?(r)
      POSSIBLE_SIMPLE_RECIPIENTS[r]
    elsif r.match? /(.*)::(\d*)/
      MessageRecipientQuery.friendly_name(r)
    else
      "(unknown)"
    end
  end
  labels
end

#started?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'app/models/message.rb', line 72

def started?
  started_at.present?
end

#statusObject



80
81
82
83
84
85
86
# File 'app/models/message.rb', line 80

def status
  return "automated" if automated?
  return "delivered" if delivered?
  return "started" if started?
  return "queued" if queued?
  "drafted"
end

#using_default_template?Boolean

Returns:

  • (Boolean)


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

def using_default_template?
  template == "default"
end