-
Notifications
You must be signed in to change notification settings - Fork 2
Service Objects
cavalle edited this page Jan 7, 2013
·
28 revisions
- The controller (service-layer?)
- The AR model
- A role
- An external interface
- A use case
- http://rails-vs-oo.heroku.com/
- http://confreaks.com/videos/1393-rubymanor3-rails-vs-object-oriented-design
class OrdersController < ActionController::Base
def close
#retrieval
payment_handler = Payment.for(params[:method], params[:card])
process = OrderProcess.new(order, payment_handler)
process.close
#rendering
end
endclass Payment
def self.for(method, card_details=nil)
if method == "card"
CardPayment.new(card_details)
else
new
end
end
def handle_payment
#default payment handling code
end
endclass CardPayment
#...
def handle_payment
ExternalPaymentService.new.charge(@card_details)
end
endclass OrderProcess
#...
def close
@payment_handler.handle_payment
Delivery.create! :status => :pending, :order => @order
@order.close!
end
end```ruby` class Order < ActiveRecord::Base #...
def close! self.closed = true self.save! end end