Created
November 30, 2012 12:23
-
-
Save nicholasjhenry/4175484 to your computer and use it in GitHub Desktop.
Hexagonal Rails with Responder Objects
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 GetItemService < Struct.new( :id, :responder ) | |
| def get | |
| item = Item.find( id ) | |
| item ? responder.success( item ) : responder.failure( item ) | |
| end | |
| end | |
| class GetItemResponder < SimpleDelegator | |
| def success( item ) | |
| @item = item | |
| render( "show" ) | |
| end | |
| def failure | |
| # handle failure -- render a 404, or whatever | |
| end | |
| end | |
| class ItemsController < ApplicationController | |
| def show | |
| GetItemService.new( params[:id], GetItemResponder.new( self ) ).get | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment