Skip to content

Instantly share code, notes, and snippets.

@j-em
Created October 13, 2014 02:47
Show Gist options
  • Save j-em/a0e5c856a66f6015218a to your computer and use it in GitHub Desktop.
Save j-em/a0e5c856a66f6015218a to your computer and use it in GitHub Desktop.
class Teetime < ActiveRecord::Base
validates :user, format: { with: /\w/, message: "can only contains letters or numbers"}, uniqueness: true
validates :date, presence: true
validates :time, presence: true
validates :club_id, inclusion: {in: Club.all.map { |club| club.id }, message: "%{value} is not a valid club"}
belongs_to :club
def self.available_on(date, club)
# date comes in the format mm/dd/yyy
# club comes in the format "clubname"
club_id = Club.where(name: club).first.id
spdate = date.split "/"
day, month, year = spdate[1], spdate[0], spdate[2]
possible_time = (9..16).map do |h|
[0, 20, 40].map do |m|
Time.new(year, month, day, h, m)
end
end
taken_time = self.where(date: date, club_id: club_id).map do |teetimes|
sptime = teetimes.time.split(":")
hour, minutes = sptime[0], sptime[1]
Time.new(year, month, day, hour, minutes)
end
possible_time.flatten.delete_if do |t|
taken_time.flatten.include? t
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment