class Schedule::Schedule

Single year’s event schedule. Hierarchical model or Arrays: Schedule –> Month –> Week –> Day –> SingleDayEvent

Constants

COLUMNS_MAP

Attributes

months[R]

0-based array of Months

year[R]

0-based array of Months

Public Class Methods

add_one_day_events_to_parents(events, multi_day_events) click to toggle source
# File app/models/schedule/schedule.rb, line 165
def Schedule.add_one_day_events_to_parents(events, multi_day_events)
  events.each do |event|
    parent = multi_day_events[event.name]
    if parent
      parent.events << event
    end
  end
end
delete_all_future_events(events) click to toggle source

Events with results _will not_ be destroyed

# File app/models/schedule/schedule.rb, line 50
def Schedule.delete_all_future_events(events)
  date = events.map(&:date).min
  logger.debug "Delete all events after #{date}"
  # Avoid lock version errors by destroying child events first
  SingleDayEvent.destroy_all ["date >= ? and events.id not in (select event_id from races)", date]
  Event.destroy_all ["date >= ? and events.id not in (select event_id from races)", date]
end
find_multi_day_events(events) click to toggle source

Try and create parent MultiDayEvents from imported SingleDayEvents

# File app/models/schedule/schedule.rb, line 142
def Schedule.find_multi_day_events(events)
  logger.debug "Find multi-day events"

  # Hash of Arrays keyed by event name
  events_by_name = Hash.new
  events.each do |event|
    logger.debug "Find multi-day events #{event.name}"
    event_array = events_by_name[event.name] || Array.new
    event_array << event
    events_by_name[event.name] = event_array if event_array.size == 1
  end
  
  multi_day_events = []
  events_by_name.each do |name, event_array|
    logger.debug "Create multi-day event #{name}"
    if event_array.size > 1
      multi_day_events << MultiDayEvent.create_from_children(event_array)
    end
  end
  
  multi_day_events
end
has_event?(row) click to toggle source
# File app/models/schedule/schedule.rb, line 70
def Schedule.has_event?(row)
  row[:name].present? && row[:date].present? && (row[:notes].blank? || !row[:notes]["Not on calendar"])
end
import(file_path) click to toggle source

Import Schedule from Excel filename.

Warning: Deletes all events after the schedule's first event date. See trac.butlerpress.com/racing_on_rails/wiki/SampleImportFiles for format details and examples.

file_path = schedule file to import

Returns

  • date of first event

# File app/models/schedule/schedule.rb, line 36
def Schedule.import(file_path)
  start_date = nil
  Event.transaction do
    table = Tabular::Table.read(file_path, :columns => COLUMNS_MAP)
    events = parse_events(table)
    delete_all_future_events events
    multi_day_events = find_multi_day_events(events)
    save events, multi_day_events
  end
  
  start_date
end
logger() click to toggle source
# File app/models/schedule/schedule.rb, line 188
def Schedule.logger
  Rails.logger
end
new(year, events) click to toggle source
# File app/models/schedule/schedule.rb, line 192
def initialize(year, events)
  @year = year.to_i
  @months = []
  (1..12).each do |month|
    @months << Month.new(year, month)
  end
  events.each do |event|
    month = @months[event.date.month - 1]
    if month.nil?
      raise IndexError, "Could not find month for #{event.date.month} in year #{year}"
    end
    month.add event
  end
end
parse(row) click to toggle source

Read Table Row and create SingleDayEvent

# File app/models/schedule/schedule.rb, line 75
def Schedule.parse(row)
  logger.debug(row.inspect) if logger.debug?
  event = nil

  if row[:discipline] == "Clinic"
    row[:instructional] = true
  end

  if row[:discipline]
    discipline = Discipline.find_via_alias(row[:discipline])
    if discipline != nil
      row[:discipline] = discipline.name
    else
      row[:discipline] = RacingAssociation.current.default_discipline
    end
  end
  
  if row[:sanctioned_by].nil?
    if row[:notes] == 'national'
      row[:sanctioned_by] = 'USA Cycling'
    elsif row[:notes] == 'international'
      row[:sanctioned_by] = 'UCI'
    end
  end
  
  event_hash = row.to_hash
  promoter = Person.find_by_info(row[:promoter_name], row[:promoter_email], row[:promoter_phone])
  
  if promoter
    if promoter.name.blank?
      promoter.update_attributes!(:name => row[:promoter_name])
    end
      
    if promoter.home_phone.blank?
      promoter.update_attributes!(:home_phone => row[:promoter_phone])
    else
      event_hash[:phone] = row[:promoter_phone]
    end

    if promoter.email.blank?
      promoter.update_attributes!(:email => row[:promoter_email])
    else
      event_hash[:email] = row[:promoter_email]
    end
  elsif row[:promoter_name].present? || row[:promoter_email].present? || row[:promoter_phone].present?
    promoter = Person.create!(
                :name => row[:promoter_name], 
                :email => row[:promoter_email], 
                :home_phone => row[:promoter_phone]
              )
  end

  event_hash.delete :promoter_email
  event_hash.delete :promoter_phone
  event_hash[:promoter] = promoter

  event_hash.delete :series

  
  event = SingleDayEvent.new(event_hash)
  event.notification = false
  
   logger.debug("Add #{event.name} to schedule") if logger.debug?
  event
end
parse_events(file) click to toggle source

Read file, split city and state, read and create promoter

# File app/models/schedule/schedule.rb, line 59
def Schedule.parse_events(file)
  events = []
  file.rows.each do |row|
    if has_event?(row)
      events << Schedule.parse(row)
    end
  end
  events.compact!
  events
end
save(events, multi_day_events) click to toggle source
# File app/models/schedule/schedule.rb, line 174
def Schedule.save(events, multi_day_events)
  events.each do |event|
    logger.debug "Save #{event.name}"
    unless Event.where(:name => event.name, :date => event.date).exists?
      event.save!
    end
  end
  multi_day_events.each do |event|
    logger.debug "Save #{event.name}"
    event.save!
    event.update_date
  end
end