Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am jonhoman on github.
  • I am hatch_jon (https://keybase.io/hatch_jon) on keybase.
  • I have a public key ASBMv7gweZy1ZMDTI6BdgBZGk1NrHEpvYYK5qj34C7J3Vgo

To claim this, I am signing this object:

@jonhoman
jonhoman / has_many_fields_for.rb
Created May 18, 2011 17:16
fields_for in a has_many relationship works, but not with a has_one relationship
# app/models/user.rb
class User < ActiveRecord::Base
has_many :profiles
accepts_nested_attributes_for :profiles
end
# app/models/profile.rb
class Profile < ActiveRecord::Base
belongs_to :user
end
@jonhoman
jonhoman / webmock_spec.rb
Created March 22, 2011 03:49
Example WebMock matcher
WebMock.should have_requested(:get, "www.example.com")
WebMock.should have_requested(:get, "www.example.com").twice
WebMock.should have_requested(:post, "www.example.com").with(:headers => 'Content-Type' => 'application/json')
@jonhoman
jonhoman / custom_step.rb
Created March 21, 2011 01:56
Cucumber VCR examples
Given /^I have a feed I want to edit$/ do
VCR.use_cassette("feed") do
@feed = Factory :feed
end
end
@jonhoman
jonhoman / feed_spec_failure.rb
Created March 21, 2011 01:54
VCR RSpec examples
describe Feed do
let :feed do
Factory :feed
end
it "is not valid without a name" do
feed.name = nil
feed.should_not be_valid
end
end
@jonhoman
jonhoman / gist:878871
Created March 21, 2011 01:21
Example VCR YAML
---
- !ruby/struct:VCR::HTTPInteraction
request: !ruby/struct:VCR::Request
method: :get
uri: http://api.bit.ly:80/v3/clicks?apiKey=R_504029652b3e3b641ac2ad781723c96a&hash=fXEv5E&login=jonhoman
body:
headers:
response: !ruby/struct:VCR::Response
status: !ruby/struct:VCR::ResponseStatus
code: 200
@jonhoman
jonhoman / twitter_controller_spec.rb
Created March 21, 2011 00:17
Hand-rolled Twitter OAuth Stubs
describe "GET 'connect'" do
before do
stub_oauth_request_token!
end
it "should redirect to twitter authorization url" do
get 'connect'
session[:request_token].should == 't'
session[:request_token_secret].should == 's'
let(:test) { puts "Hello" }
it "should print hello" do
test
end
it "should not print hello" do
end
#output
DEPRECATION WARNING: RAILS_ROOT is deprecated. Please use ::Rails.root.to_s. (called from join at /Users/obtiva/dev/personal/lifestreams/vendor/plugins/lazy_developer/lib/tasks/rcov.rake:3)
@jonhoman
jonhoman / ruby
Created December 31, 2010 20:34
Why is to_s needed?
puts build_date
# Sun Dec 19 10:35:26 -0600 2010
puts build_date.class
# Time
# This works
feed.update_attributes!(:name => name, :last_build_date => build_date.to_s)
# This fails with wrong number of arguments (1 for 0)