Skip to content

Instantly share code, notes, and snippets.

def my_method(arg1, arg2)
Foo.typecheck arg1
Bar.typecheck arg2
puts "The method finished!"
end
@jjb
jjb / gist:5043361
Last active December 14, 2015 06:29
There are a few nice guides to timezones in Rails apps (http://www.elabs.se/blog/36-working-with-time-zones-in-ruby-on-rails, http://danilenko.org/2012/7/6/rails_timezones/), but these might be overwhelming if you just want to quickly set and work with a timezone without caring about configuring it app-wide. So, here you go:
# First do this
Time.zone = 'EST'
# Now you can do this
Time.zone.now
# and this!
Time.zone.local(2013, 1, 1)
# rails/activerecord/arel work with it:
class Foo
def bar
Bar.find(bar_id)
end
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
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
@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}"
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|
def calculate_thing_ratio
sub_things / total_things
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
class Foo < ActiveRecord::Base
serialize :bar, JSON
end