-
-
Save tooky/3062683 to your computer and use it in GitHub Desktop.
Moving response callbacks out of controller
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 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 |
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 CardPersistenceResponder < SimpleDelegator | |
def created(card) | |
redirect_to planning_path(card.project) | |
end | |
def create_failed(card) | |
flash.now[:alert] = 'Your card needs a title' | |
render :action => 'new', locals: { card: card, title: 'New card' } | |
end | |
end |
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 CardsController < ApplicationController | |
def create | |
creator = CardCreator.new(CardPersistenceResponder.new(self)) | |
creator.create(project.backlog, card_params) | |
end | |
end |
Author
tooky
commented
Jul 7, 2012
via email
You have to edit the views so you don't use ivars... But that's no bad
thing!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment