Skip to content

Instantly share code, notes, and snippets.

@simkessy
Last active November 10, 2015 01:01
Show Gist options
  • Save simkessy/2372fae76588180c756b to your computer and use it in GitHub Desktop.
Save simkessy/2372fae76588180c756b to your computer and use it in GitHub Desktop.
class Availability < ActiveRecord::Base
# Associations
belongs_to :facility
has_one :venue, through: :facility
# Scopes for search filters
scope :close_to, -> (venues) {where{facility.venue_id.in venues}}
scope :activity, -> (activity) {where{facility.activities.name.in activity}}
scope :start_date, -> (datetime) {where{ start_time >= datetime }}
scope :end_date, -> (datetime) {where{ end_time <= datetime }}
scope :after_now, -> {where{start_time >= Time.current }}
scope :before_now, -> {where{start_time < Time.current }}
scope :not_booked, -> {where(booking: nil)}
scope :booked, -> {joins(:booking)}
scope :sold, -> {where {booking_id != nil}}
scope :sorted, -> {order{start_time.desc}}
scope :join, -> {joins{facility.activities}}
scope :after_notice_time, -> {where{start_time >= (facility.venue.notice_time.hours.from_now)}}
scope :after_notice_time2, lambda {|o| where{start_time >= (o.venue.notice_time.hours.from_now)}}
def self.perform_search(location, activity, start_time, end_time, period)
self
.join
.not_booked
.after_now
.after_notice_time
.close_to(Venue.close_to(location))
.activity(Activity.find_by_name activity)
.start_date(validate_start_time start_time)
.end_date(validate_end_time end_time)
.within_segment_of_day(period)
.sorted
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment