Created
February 13, 2013 22:35
-
-
Save krisleech/4949022 to your computer and use it in GitHub Desktop.
Using focused_controller to provide service object with hooks for callbacks
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
class TripsController < ApplicationController | |
class Action < ApplicationController::Action | |
end | |
class New < Action | |
expose(:trip) { Trip.new(params[:trip]) } | |
end | |
class Create < New | |
def call | |
CreateTrip.new(self).execute(current_user, params[:trip]) | |
end | |
def successful(trip) | |
redirect_to trip | |
end | |
def failed(trip) | |
render :action => :new | |
end | |
end | |
end |
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
class CreateTrip | |
def initialize(listener) | |
@listener = listener | |
end | |
def execute(user, attributes) | |
trip = Trip.new(attributes) | |
trip.user = user | |
if trip.save | |
listener.successful(trip) | |
else | |
listener.failed(trip) | |
end | |
end | |
private | |
attr_reader :listener | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment