Skip to content

Instantly share code, notes, and snippets.

@tooky
Forked from gma/card_creator.rb
Created July 6, 2012 20:52
Show Gist options
  • Select an option

  • Save tooky/3062683 to your computer and use it in GitHub Desktop.

Select an option

Save tooky/3062683 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 < 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
class CardsController < ApplicationController
def create
creator = CardCreator.new(CardPersistenceResponder.new(self))
creator.create(project.backlog, card_params)
end
end
@mattwynne
Copy link
Copy Markdown

Tidy! Does that locals thing actually work with pages?

@tooky
Copy link
Copy Markdown
Author

tooky commented Jul 7, 2012 via email

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