Skip to content

Instantly share code, notes, and snippets.

@kivanio
Forked from krisleech/booking.rb
Created June 22, 2014 21:49
Show Gist options
  • Select an option

  • Save kivanio/83e738d7dc3bf9d4906e to your computer and use it in GitHub Desktop.

Select an option

Save kivanio/83e738d7dc3bf9d4906e to your computer and use it in GitHub Desktop.
# app/models/booking.rb
class Booking < AR::Base
# ...
end
# app/models/booking/create.rb
class Booking::Create
def initialize(attributes)
@booking = Booking.new(attributes)
end
def call
@booking.tap do |booking|
if booking.save
# inform seller
# inform buyer
end
end
end
end
# app/models/booking/cancel.rb
class Booking::Cancel
def initialize(booking_id)
@booking = Booking.find(booking_id).extend(Cancelable)
end
def call
@booking.tap do |booking|
if booking.cancel!
# inform buyer
end
end
end
module Cancelable
extend ActiveSupport::Concern
included do
before_validation :guard_cancelation
end
def cancel!
update_attributes(:status => 'canceled')
end
private
def guard_cancelation
errors.add(:base, 'can not cancel paid booking') if paid?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment