Skip to content

Instantly share code, notes, and snippets.

View nicholasjhenry's full-sized avatar

Nicholas Henry nicholasjhenry

View GitHub Profile
@blowmage
blowmage / _readme.md
Created November 6, 2012 16:54
Presenters in Rails, using modules

Presenters in Rails

I have bemoaned the lack of a ViewModel in Rails many times, and I prefer using Presenters to simulate a ViewModel. But it turns out there is an object that does represent the state of the view in Rails apps. Its an instance of ActionView::Base that the controller calls view_context. We don't have much control of this object, but it seems like a logical place to put our view-specific behavior.

This code is an attempt at creating Presenters using modules. The module will be mixed into the view_context object. This is very similar to how a Decorator module will be mixed into a model object, only instead of being specific to the model is it specific to the view.

This means that testing your presenter is no different than testing any other module. So relying on dependencies such as other methods or instance variables can make testing difficult.

@tooky
tooky / gist:3959276
Created October 26, 2012 14:59 — forked from Gregg/gist:968534
Code School Screencasting Framework

Screencasting Framework

The following document is a written account of the Code School screencasting framework. It should be used as a reference of the accompanying screencast on the topic.

Why you should care about screencasting?

You're probably aren't going to take the time to read this document if you're not interested, but there are a lot of nice side effects caused by learning how to create quality screencasts.

  1. Communicating more effectively - At Envy Labs we produce screencasts for our clients all the time. Whether it's demoing a new feature or for a presentation for an invester, they're often much more effective and pleasent than a phone call or screen sharing.
require_relative 'deferrable'
require_relative 'slime'
class WidgetsController
include ControllerSlime
def create
creator = WidgetCreator.new(attributes: params[:widget])
creator.done do |widget|
anonymous
anonymous / gist:3753571
Created September 20, 2012 02:10
@ChristinGorman gave this talk at JavaZone: https://vimeo.com/49484333 It's quite good, short, energetic, enthusiastic,
intelligent, and completely misses the point.
While it's true that the code she produces is much better than the original, and is quite easy to understand; it fails one
critical test. It's not polite.
Polite code is like a well written newspaper article. It allows you to bail out early. A well written article has a
headline, a synopsis, and a set of paragraphs that begin with the high level concepts and get more and more detailed as you
read through the article. At any point you can decide: "I get it! I don't need to read further." Indeed, this is how most
people read newspapers or magazines. The articles are polite, because they allow you to get out quickly.
@myronmarston
myronmarston / shock.rb
Created September 14, 2012 23:13
Alternate pattern for Shock costing from Sandi Metz's GoGaRuCo Talk
class Shock
def intialize(type)
@type = type
end
def cost
coster_for(type).compute
end
private
@subelsky
subelsky / dependency_injection_container.rb
Created September 10, 2012 14:28
Code examples for Ruby Dependencies, Notifications, and Adjustments (Ruby DNA)
require "dim"
AppContainer = Dim::Container.new
AppContainer.register(:env) { ENV['ADWORK_ENV'] || "development" }
AppContainer.register(:production?) { |c| c.env == 'production' }
AppContainer.register(:development?) { |c| c.env == 'development' }
AppContainer.register(:test?) { |c| c.env == 'test' }
AppContainer.register(:root) { File.expand_path(File.dirname(__FILE__)+"/../..") }
AppContainer.register(:logger) do |c|
WorkflowEventsController < ApplicationController
# . . .
def create
workflow_event = WorkflowEvent.new(params[:workflow_event])
@some_model = SomeModel.find(params[:some_model_id]
SomeWorkflow.new(current_user, @some_model).handle(workflow_event) do |result|
result.on_success { render :template => 'success' }
result.on_failure { render :template => 'failure' }
end
end
@therabidbanana
therabidbanana / application_controller.rb
Created July 11, 2012 14:41 — forked from gmoeck/some_controller.rb
Simplified version of the application design I'm heading toward (actually in Sinatra, but that doesn't really matter here)
class ApplicationController
# Sharing the setup of processors, since many actions will use them
# - thinking of switching this out for a facade
def trigger_processor(name, listening_controller)
processor_klass = processors[name]
processor = processor_klass.new(params)
processor.add_listener(EventNotifier.new)
processor.add_listener(listening_controller)
processor.process(current_user)
end
class CommentsController < ApplicationController
def create
app.create_comment(params)
end
private
def app
MyRailsApp.new
class CommentsController < ApplicationController
def create
translator.translate params
end
private
def translator
CommentRequestTranslator.new(processor)