Created
August 10, 2012 11:11
-
-
Save krisleech/3313465 to your computer and use it in GitHub Desktop.
separating persistance and domain, DCI-ish.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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