Skip to content

Instantly share code, notes, and snippets.

@gma
Created July 6, 2012 17:00
Show Gist options
  • Save gma/3061327 to your computer and use it in GitHub Desktop.
Save gma/3061327 to your computer and use it in GitHub Desktop.
Moving response callbacks out of controller
class CardCreator < Struct.new(:listener)
def create(iteration, attributes)
card = iteration.cards.build(attributes)
if card.save
listener.created(card)
else
listener.create_failed(card)
end
end
end
class CardPersistenceResponder < Struct.new(:controller)
def created(card)
controller.instance_eval do
redirect_to planning_path(card.project)
end
end
def create_failed(card)
controller.instance_eval do
@title = 'New card'
@card = card
flash.now[:alert] = 'Your card needs a title'
render :action => 'new'
end
end
end
class CardsController < ApplicationController
def create
creator = CardCreator.new(CardPersistenceResponder.new(self))
creator.create(project.backlog, card_params)
end
end
@gma
Copy link
Author

gma commented Aug 10, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment