Skip to content

Instantly share code, notes, and snippets.

@jjb
jjb / gist:6878300
Created October 8, 2013 02:09
If you are uploading something to S3 using the aws-sdk gem (https://github.com/aws/aws-sdk-ruby) and getting timeout errors (AWS::S3::Errors::RequestTimeout: Your socket connection to the server was not read from or written to within the timeout period. Idle connections will be closed.), do this!
AWS.config(:http_open_timeout => 99999, :http_read_timeout => 99999)
foo = Foo.new
foo.bar = [1,2,3]
foo.save
foo.reload
foo.bar.class # Array
foo.bar # [1,2,3]
foo.bar = {a:1, b:2}
foo.save
class CreateFoos < ActiveRecord::Migration
def change
create_table :foos do |t|
t.timestamps
end
add_column(:foos, :bar, :json)
end
end
class Foo < ActiveRecord::Base
serialize :bar, JSON
end
class CreateFoos < ActiveRecord::Migration
def change
create_table :foos do |t|
t.json :bar # this blows up in Rails 3
t.timestamps
end
end
end
def calculate_thing_ratio
sub_things / total_things
end
def divisible_by_3?(number)
number%3 == 0
end
def commaifier(number)
reversed = number.to_s.reverse
result = ""
number.to_s.size.times do |i|
@jjb
jjb / 1. code.rb
Created March 12, 2013 03:47
Demonstration of why private methods are problematic for specing, and also potentially a design smell, for discusion on rubyparley
class Product
attr_accessible :price
attr_accessible :name
def long_listing
"The price of #{name} is #{human_price}"
end
def short_listing
"#{name}: #{human_price}"
describe "#bar" do
let(:bar){FactoryGirl.create(:bar)}
let(:foo){FactoryGirl.create(:foo, bar_id: bar.id)}
subject{foo.bar}
it{should eq bar}
end
describe "#bar" do
let(:foo){FactoryGirl.build_stubbed(:foo)}
let(:bar_id){double("bar_id")}
let(:bar){double("bar")}
subject{foo.bar}
before do
foo.stub(:bar_id){bar_id}
Foo.stub(:find).with(bar_id){bar}
end