Class: Message
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- Message
- 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
- .automated ⇒ Object
- .bulk ⇒ Object
- .inheritance_column ⇒ Object
- .queue_for_trigger(trigger, user_id) ⇒ Object
Instance Method Summary collapse
- #automated? ⇒ Boolean
- #bulk? ⇒ Boolean
- #can_edit? ⇒ Boolean
- #can_queue? ⇒ Boolean
- #delivered? ⇒ Boolean
- #possible_recipients ⇒ Object
- #queued? ⇒ Boolean
- #recipients=(values) ⇒ Object
- #recipients_list ⇒ Object
- #started? ⇒ Boolean
- #status ⇒ Object
- #using_default_template? ⇒ Boolean
Class Method Details
.automated ⇒ Object
147 148 149 |
# File 'app/models/message.rb', line 147 def self.automated where(type: 'automated') end |
.bulk ⇒ Object
143 144 145 |
# File 'app/models/message.rb', line 143 def self.bulk where(type: 'bulk') end |
.inheritance_column ⇒ Object
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) = Message.where(trigger: trigger) .map { || Mailer.delay.(.id, user_id) } end |
Instance Method Details
#automated? ⇒ Boolean
62 63 64 |
# File 'app/models/message.rb', line 62 def automated? type == "automated" end |
#bulk? ⇒ Boolean
58 59 60 |
# File 'app/models/message.rb', line 58 def bulk? type == "bulk" end |
#can_edit? ⇒ Boolean
86 87 88 |
# File 'app/models/message.rb', line 86 def can_edit? automated? || status == "drafted" end |
#can_queue? ⇒ Boolean
90 91 92 |
# File 'app/models/message.rb', line 90 def can_queue? status == "drafted" && recipients_list.present? end |
#delivered? ⇒ Boolean
66 67 68 |
# File 'app/models/message.rb', line 66 def delivered? delivered_at.present? end |
#possible_recipients ⇒ Object
98 99 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 98 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), option.call("bus-list--eligible::#{bus_list.id}", bus_list), option.call("bus-list--applied::#{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
74 75 76 |
# File 'app/models/message.rb', line 74 def queued? queued_at.present? end |
#recipients=(values) ⇒ Object
41 42 43 |
# File 'app/models/message.rb', line 41 def recipients=(values) values.present? ? super(values.reject(&:blank?)) : super(values) end |
#recipients_list ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/models/message.rb', line 45 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
70 71 72 |
# File 'app/models/message.rb', line 70 def started? started_at.present? end |
#status ⇒ Object
78 79 80 81 82 83 84 |
# File 'app/models/message.rb', line 78 def status return "automated" if automated? return "delivered" if delivered? return "started" if started? return "queued" if queued? "drafted" end |
#using_default_template? ⇒ Boolean
94 95 96 |
# File 'app/models/message.rb', line 94 def using_default_template? template == "default" end |