Skip to content

Instantly share code, notes, and snippets.

@tcrayford
Forked from mattwynne/sketch.rb
Created June 29, 2012 16:03
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 tcrayford/3018823 to your computer and use it in GitHub Desktop.
Save tcrayford/3018823 to your computer and use it in GitHub Desktop.
sketch for Matt Wynne
class RubbishController
protected
def redirect_to(path)
puts "redirecting_to #{path}"
end
def render(renderable)
puts "rendering #{renderable}"
end
def current_user
Object.new
end
def organizations_path(org)
"/organizations/#{org.to_param}"
end
end
class ValidationError < RuntimeError
end
class IgnorePrivate
def initialize(thing)
@thing = thing
end
def method_missing(message, *args, &block)
@thing.send(message, *args, &block)
end
end
class Organization
def to_param
"42"
end
def save!
if rand > 0.5
raise ValidationError
end
end
end
class OrganizationCreator
def initialize(ui)
@ui = ui
end
def create_for(user, org_name)
# real creation logic here
begin
organization = Organization.new
organization.save!
@ui.organization_creation_succeeded(organization)
rescue ValidationError
@ui.organization_creation_failed(organization)
end
end
end
class OrganizationCreationFeedback
def initialize(controller)
@controller = controller
end
def organization_creation_succeeded(org)
@controller.redirect_to(@controller.organizations_path(org))
end
def organization_creation_failed(org)
@controller.render(:error)
end
end
class OrganizationsController < RubbishController
def create
params = { organization: 'Hello' }
feedback = OrganizationCreationFeedback.new(IgnorePrivate.new(self))
creator = OrganizationCreator.new(feedback)
creator.create_for(current_user, params[:organization])
end
end
controller = OrganizationsController.new
controller.create
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment