Method: MessageRecipientQuery.parse

Defined in:
app/models/message_recipient_query.rb

.parse(query, model = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/message_recipient_query.rb', line 17

def self.parse(query, model = nil)
  # Format:
  # model::ID
  # model--modifier::ID
  match = query.match(/(.*)::(\d*)/)
  type = match[1]
  id = match[2]

  # Find the backing database model, ensuring the given ID exists for that model.
  model_name = nil
  case type
  when "bus-list"
    model ||= BusList.find_by_id(id)
    model_name = "Bus List"
  when "school"
    model ||= School.find_by_id(id)
    model_name = "School"
  when "blazer"
    model ||= Blazer::Query.find_by_id(id)
    model_name = "Blazer Query"
  else
    raise "Unknown recipient query type: #{type.inspect} (in message recipient query: #{query.inspect}"
  end
  raise MessageRecipientQuery::ModelIdNotFound, "Could not find #{model_name} with ID #{id.inspect} (in message recipient query: #{query.inspect})" if model.blank?

  MessageRecipientQuery.new(
    query,
    type,
    id,
    model
  )
end