Module: ActsAsBookable::Booker::InstanceMethods

Defined in:
lib/acts_as_bookable/booker.rb

Instance Method Summary collapse

Instance Method Details

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

Book a bookable model

Example:

@user.book!(@room)

Parameters:

  • bookable

    The resource that will be booked

Returns:

  • The booking created

Raises:

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

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

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



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/acts_as_bookable/booker.rb', line 42

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 = ActsAsBookable::Booking.create!(booking_params)

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

#booker?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/acts_as_bookable/booker.rb', line 56

def booker?
  self.class.booker?
end