Skip to content

Instantly share code, notes, and snippets.

@biot023
Created November 28, 2012 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biot023/4161747 to your computer and use it in GitHub Desktop.
Save biot023/4161747 to your computer and use it in GitHub Desktop.
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