Created
November 28, 2012 14:48
-
-
Save biot023/4161747 to your computer and use it in GitHub Desktop.
This file contains 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 Broadcaster < Struct.new( :listeners ) | |
def broadcast( message, *args ) | |
listeners.each { |listener| listener.send( message, *args ) } | |
end | |
end | |
module Services | |
class ItemCreate < Broadcaster | |
def initialize( params, *listeners ) | |
super( listeners ) | |
@params = params | |
end | |
def create | |
item = Item.create( params ) | |
item.save ? | |
broadcast( :item_created, item ) : | |
broadcast( :item_create_failed, item ) | |
end | |
private | |
attr_accessor :params | |
end | |
end | |
module Responders | |
class ItemCreate < SimpleDelegator | |
def item_created( item ) | |
flash[:notice] = "The item has been created" | |
redirect_to( item_path( item ) ) | |
end | |
def item_create_failed( item ) | |
flash[:error] = "That all went horribly wrong" | |
render( "new", :locals => { :item => item } ) | |
end | |
end | |
end | |
class ItemController < ApplicationController | |
def new | |
render( "new" ) | |
end | |
def create | |
Services::ItemCreate.new( params, Responders::ItemCreate.new( self ) ).create | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment