Class: MessageRecipientQuery

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

Defined Under Namespace

Classes: ModelIdNotFound

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, type, id, model) ⇒ MessageRecipientQuery

Returns a new instance of MessageRecipientQuery.



10
11
12
13
14
15
# File 'app/models/message_recipient_query.rb', line 10

def initialize(query, type, id, model)
  @query = query
  @type = type
  @id = id
  @model = model
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



7
8
9
# File 'app/models/message_recipient_query.rb', line 7

def id
  @id
end

#modelObject

Returns the value of attribute model.



8
9
10
# File 'app/models/message_recipient_query.rb', line 8

def model
  @model
end

#queryObject

Returns the value of attribute query.



5
6
7
# File 'app/models/message_recipient_query.rb', line 5

def query
  @query
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'app/models/message_recipient_query.rb', line 6

def type
  @type
end

Class Method Details

.friendly_name(query, model = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/message_recipient_query.rb', line 50

def self.friendly_name(query, model = nil)
  begin
    recipient_query = parse(query, model)
    model = recipient_query.model
  rescue MessageRecipientQuery::ModelIdNotFound
    return "[invalid recipient]"
  end

  case recipient_query.type
  when "bus-list"
    "Bus List: #{model.name} (signed up for bus)"
  when "bus-list--eligible"
    "Bus List: #{model.name} (eligible, not signed up for bus)"
  when "bus-list--applied"
    "Bus List: #{model.name} (applied/not accepted)"
  when "school"
    "Confirmed or Accepted: #{model.name}"
  when "blazer"
    "Blazer Query: #{model.name}"
  else
    raise "Unknown recipient query type: #{recipient_query.type.inspect} (in message recipient query: #{r.inspect}"
  end
end

.parse(query, model = nil) ⇒ Object

Raises:



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", "bus-list--applied", "bus-list--eligible"
    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