Skip to content

Instantly share code, notes, and snippets.

View sbellware's full-sized avatar

Scott Bellware sbellware

View GitHub Profile
@sbellware
sbellware / rspec_patches.rb
Created December 13, 2010 20:28
For RSpec 2, in spec/support
module RSpec
module Core
module Hooks
def because(*args, &block)
scope, options = scope_and_options_from(*args)
hooks[:after][scope] << AfterHook.new(options, &block)
end
end
end
module Mocks
@sbellware
sbellware / gist:795297
Created January 25, 2011 17:51
An Alternative to All-or-Nothing Custom Form Builder Replacement

An Alternative to All-or-Nothing Custom Form Builder Replacement

Background

Replacing Rails' default form builder with a custom form builder is a typical way of adding custom form helpers to a Rails application.

The typical way of doing this is:

ActionView::Base.default_form_builder = MyCustomFormBuilder
function changes {
start="$1"
end="$2"
if [ -z "$2" ]; then
start="$1"^
end="$1"
fi
if [ -z "$1" ]; then
@sbellware
sbellware / gist:892438
Created March 29, 2011 14:19
Example Haml Helper for Creating a Submit Button with Nested Span for its Label
def submit_button(options={})
text = options.delete(:text) || "Submit"
options.merge! :type => :submit
capture_haml do
haml_concat(content_tag(:button, options) do
haml_concat content_tag(:span, text)
end)
end
end
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
require 'rake'
module ::Restbuy
class Application
include Rake::DSL
end
require 'rubygems'
require 'mongo'
db = Mongo::Connection.new.db("hack")
db['hack'].insert(:created_at => BSON::Timestamp.new(nil,nil))
module RSpec
module Core
module Hooks
def act(*args, &block)
scope, options = scope_and_options_from(*args)
hooks[:before][scope] << BeforeHook.new(options, &block)
end
def act_after(*args, &block)
scope, options = scope_and_options_from(*args)
hooks[:after][scope] << AfterHook.new(options, &block)
@sbellware
sbellware / gist:1232480
Created September 21, 2011 16:05
Sketches of Configuration API
class Thing
include Configured
setting :some_setting
setting :other_setting
end
class Configuration
def some_setting
'foo'
@sbellware
sbellware / gist:1236327
Created September 22, 2011 23:23
Transformer Prototype
class Foo
attr_accessor :bar
end
foo = Foo.new
foo.bar = 'blah'
transformer = Transformer.build :foo
transformed_foo = transformer.transform foo
@sbellware
sbellware / gist:1500079
Created December 20, 2011 03:18
Extend vs Include in Metaclass
module M
def foo
puts "M#foo"
end
end
class C
end