Module: ActiveBookings::Booker::InstanceMethods

Defined in:
lib/active_bookings/booker.rb

Instance Method Summary collapse

Instance Method Details

#book!(bookable, opts = {}) ⇒ Object

Book a bookable model

Example:

@user.book!(@room)

Raises:

  • ActiveBookings::OptionsInvalid if opts are not valid for given bookable

  • ActiveBookings::AvailabilityError if the bookable is not available for given options

  • ActiveRecord::RecordInvalid if trying to create an invalid booking



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/active_bookings/booker.rb', line 44

def book!(bookable, opts={})
  # check availability
  bookable.check_availability!(opts) if bookable.class.bookable?

  # create the new booking
  booking_params = opts.merge({booker: self, bookable: bookable})
  booking_class = bookable.class.reflect_on_association(:bookings).klass
  booking = booking_class.create!(booking_params)

  # reload the bookable to make changes available
  bookable.reload
  self.reload
  booking
end

#booker?Boolean



59
60
61
# File 'lib/active_bookings/booker.rb', line 59

def booker?
  self.class.booker?
end