Skip to content

Instantly share code, notes, and snippets.

@krisleech
Created August 10, 2012 11:11
Show Gist options
  • Save krisleech/3313465 to your computer and use it in GitHub Desktop.
Save krisleech/3313465 to your computer and use it in GitHub Desktop.
separating persistance and domain, DCI-ish.
# 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