class UpcomingEvents::Base

Attributes

date[R]
discipline[R]
disciplines[R]
weeks[R]

Public Class Methods

new(date, weeks, discipline) click to toggle source

Date = start date. Defaults to today

# File app/models/upcoming_events/base.rb, line 12
def initialize(date, weeks, discipline)
  @date = date || Time.zone.today
  @discipline = discipline
  @disciplines = disciplines_for(discipline)
  @weeks = weeks || 2
end

Public Instance Methods

[](discipline_name) click to toggle source

Get Discipline by name

# File app/models/upcoming_events/base.rb, line 58
def [](discipline_name)
  disciplines.detect { |discipline| discipline.name == discipline_name }
end
cutoff_date() click to toggle source

Set date to nearest Monday

# File app/models/upcoming_events/base.rb, line 34
def cutoff_date
  case date.wday
  when 0
    date + (weeks.to_i * 7)
  when 1
    date + (weeks.to_i * 7) - 1
  when 2
    date + (weeks.to_i * 7) - 2
  when 3
    date + (weeks.to_i * 7) - 3
  when 4
    date + (weeks.to_i * 7) - 4
  when 5
    date + (weeks.to_i * 7) - 5
  when 6
    date + (weeks.to_i * 7) + 1
  end
end
dates() click to toggle source
# File app/models/upcoming_events/base.rb, line 29
def dates
  @dates ||= (date.to_date..cutoff_date.to_date)
end
disciplines_for(discipline) click to toggle source

By default, we’ll search “all” disciplines (Road and Mountain Bike include other disciplines), but if discipline is present, we will only search that one

# File app/models/upcoming_events/base.rb, line 21
def disciplines_for(discipline)
  if discipline
    [Discipline[discipline]]
  else
    [Discipline[:road], Discipline[:mountain_bike], Discipline[:bmx], Discipline[:track], Discipline[:cyclocross]].compact
  end
end
empty?() click to toggle source
# File app/models/upcoming_events/base.rb, line 53
def empty?
  disciplines.all? { |discipline| discipline.upcoming_events.empty? && discipline.upcoming_weekly_series.empty? }
end