Skip to content

Instantly share code, notes, and snippets.

This example shows how to setup an environment running Rails 3 beta 3 under 1.9.2-head with a 'rails3' gem set.
∴ rvm update --head
# ((Open a new shell)) or do 'rvm reload'
# If you do not already have the ruby interpreter installed, install it:
∴ rvm install 1.9.2-head
# Switch to 1.9.2-head and gemset rails3, create if it doesn't exist.
∴ rvm --create use 1.9.2-head@rails3
-- Used Ubuntu 10.04 Lucid Canonical, ubuntu@ ami-2d4aa444
sudo apt-get install ruby-full build-essential
ruby --version
sudo apt-get install apache2 apache2-mpm-prefork apache2-prefork-dev
sudo apt-get install rubygems
sudo gem install rubygems-update
sudo /var/lib/gems/1.8/bin/update_rubygems
sudo emacs ~/.bashrc
-- add the line below and save
export PATH=/var/lib/gems/1.8/bin:$PATH
@mattsnyder
mattsnyder / contain_within_matcher.rb
Created April 15, 2011 18:02
Contain Within Matcher for use in your view specs. Faster than css_select and assert_select.
# Must have Nokogiri gem installed and save this file in your spec/support dir
# Allows you to write cleaner/faster specs in your views (50% faster than css_select)
# Increased readability in your spec doc
# ex.
# rendered.should contain('my heading').within('h1') # searches all H1 tags for 'my heading'
# or
# rendered.should contain('my string') # searches entire rendered string for 'my string'
class Within
def initialize(expected, css_selector)
@expected = expected
@mattsnyder
mattsnyder / authentication_helpers.rb
Created June 17, 2011 14:07
Easy faking of user authentication in RSpec when using Devise
module AuthenticationHelpers
# Example usage: login mock_user(Editor)
def login(user)
request.env['warden'] = mock(Warden,
:authenticate => user,
:authenticate! => user)
end
def mock_user(type, stubs={})
mock_model(type, stubs).as_null_object
@mattsnyder
mattsnyder / Prefer.rb
Created July 8, 2011 13:16
Offer up a replacement should the prefered value not be present
def Prefer(value)
if value.present? then
value
else
block_given? ? yield : NullObject.new
end
end
@mattsnyder
mattsnyder / roman_numeral_calculator_describe_block.rb
Created July 20, 2012 12:32
Example rewrite of describe block
describe "Converting integers to roman numerals" do
end
@mattsnyder
mattsnyder / converting_roman_numerals_spec.rb
Created July 20, 2012 12:45
example specs for converting roman numerals
describe "Converting integers to roman numerals" do
describe "with simple conversions" do
Then { 1.should be_converted_to 'I' }
Then { 5.should be_converted_to 'V' }
Then { 10.should be_converted_to 'X' }
end
describe "with additive conversions" do
Then { 3.should be_converted_to 'III' }
@mattsnyder
mattsnyder / roman_numeral_custom_matcher.rb
Created July 20, 2012 12:41
Example custom matcher to use for testing roman numeral conversions
RSpec::Matchers.define :be_converted_to do |expected|
match do |actual|
@result = Calculator.convert(actual)
@result == expected
end
failure_message_for_should do
"Expected #{actual} to be converted to #{expected}, but got #{@result}"
end
@mattsnyder
mattsnyder / roman_numeral_calculator_spec.rb
Created July 20, 2012 13:03
Example of classname in description block
describe RomanNumeralCalculator do
describe "with simple conversions" do
Then { RomanNumeralCalculator.convert(1).should == 'I' }
end
end
@mattsnyder
mattsnyder / phone_number.rb
Created August 23, 2012 20:23
Phone Number Generator
def phone_number
o = [(0..9)].map{|i| i.to_a}.flatten;
(0..9).map{ o[rand(o.length)] }.join
end