Sha256: 6dabf93e3fb76bdcb800dc1e24e39680f9736af9a29c1640a964c76c0faf9b5e

Contents?: true

Size: 1.99 KB

Versions: 14

Compression:

Stored size: 1.99 KB

Contents

class MessageRecipientQuery
  class ModelIdNotFound < StandardError
  end

  attr_accessor :query
  attr_accessor :type
  attr_accessor :id
  attr_accessor :model

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

  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

  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
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
hackathon_manager-0.11.0 app/models/message_recipient_query.rb
hackathon_manager-0.10.3 app/models/message_recipient_query.rb
hackathon_manager-0.10.2 app/models/message_recipient_query.rb
hackathon_manager-0.10.1 app/models/message_recipient_query.rb
hackathon_manager-0.10.0 app/models/message_recipient_query.rb
hackathon_manager-0.9.2 app/models/message_recipient_query.rb
hackathon_manager-0.9.1 app/models/message_recipient_query.rb
hackathon_manager-0.9.0 app/models/message_recipient_query.rb
hackathon_manager-0.8.2 app/models/message_recipient_query.rb
hackathon_manager-0.8.1 app/models/message_recipient_query.rb
hackathon_manager-0.8.0 app/models/message_recipient_query.rb
hackathon_manager-0.7.1 app/models/message_recipient_query.rb
hackathon_manager-0.7.0 app/models/message_recipient_query.rb
hackathon_manager-0.6.6 app/models/message_recipient_query.rb