Skip to content

Instantly share code, notes, and snippets.

@simonreed
Forked from chrismahon/_queries.rb
Last active February 15, 2016 20:40
Show Gist options
  • Save simonreed/348bd762265401a35aa8 to your computer and use it in GitHub Desktop.
Save simonreed/348bd762265401a35aa8 to your computer and use it in GitHub Desktop.
Appointment Scheduler
# Show me all available TimeSlots for a Staff member for a Service on a Day
@staff.time_slots.available.by_date(Date.today)
# Show me all available TimeSlots for a Service on a Day
@service.time_slots.available.by_date(Date.today)
# Show me all available TimeSlots for a Staff member for a Service between 2 Days grouped by Day
@staff.time_slots.available.between_dates(Date.today, Date.today + 5.days).group_by(&:date)
# Show me all available TimeSlots for a Service between 2 Days grouped by Day
@service.time_slots.available.between_dates(Date.today, Date.today + 5.days).group_by(&:date)
# Show me next available TimeSlot for a Staff member
@staff.time_slots.next_available
# Show me next available TimeSlot for a Service
@service.time_slots.next_available
# Show me all Appointments for a Day
Appointment.where(date: Date.today)
# Show me all Appointments for a Staff member on a Day
@staff_member.appointments.where(date: Date.today)
# Show me all Appointments for a Service on a Day
@service.appointments.where(date: Date.today)
# Show me all Appointments between 2 Dates grouped by Day
Appointment.between_dates(Date.today, Date.today + 5.days).group_by(&:date)
# Show me all Appointments for a Staff member between 2 Dates grouped by Day
@staff_member.appointments.between_dates(Date.today, Date.today + 5.days).group_by(&:date)
# Show me all Appointments for a Staff member for a Service between 2 Dates grouped by Day
@service.appointments.by_service(service).between_dates(Date.today, Date.today + 5.days).group_by(&:date)
# Modify an existing Appointment and move it to a new Time Slot
@appointment.time_slots = [new_time_slot_1, new_time_slot_2]
def User < ActiveRecord::Base
has_many :appointments
end
class Staff < ActiveRecord::Base
has_many :holidays
has_many :work_hours
has_many :staff_services
has_many :services, through: :staff_services
has_many :appointments, through: :staff_services
end
class Timeslot < ActiveRecord::Base
belongs_to :staff
has_one :appointment_time_slot
has_one :appointment, through: :appointment_timeslots
end
class Holiday < ActiveRecord::Base
belongs_to :staff
end
def WorkHours < ActiveRecord::Base
belongs_to :staff
end
def Service < ActiveRecord::Base
has_many :staff_services
has_many :staffs, through: :staff_services
has_many :time_slots, through: :staffs
has_many :appointments, through: :staff_services
end
def StaffService < ActiveRecord::Base
belongs_to :staff
belongs_to :service
has_many :appointments
end
def StaffService < ActiveRecord::Base
belongs_to :staff
belongs_to :service
has_many :appointments
end
def Appointment < ActiveRecord::Base
belongs_to :user
belongs_to :staff_service
has_one :staff, through: :staff_service
has_one :service, through: :staff_service
end
def AppointmentTimeslot < ActiveRecord::Base
belongs_to :appointment
belongs_to :time_slot
end
@simonreed
Copy link
Author

Here's where I've got to so far. Obviously this is still missing out on validations, scopes etc.

There's still a whole lot missing, e.g. how you'd actually figure out when the next available timeslot that could fit a service in would be.

Also I've kept the idea of TimeSlots in, rather than an appointment having a start/end time. By having TimeSlots you'll need to create this on a cronjob for every staff member for the next n days and create the TimeSlots based on whether the StaffMember is on holiday, what their work hours are etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment