Skip to content

Instantly share code, notes, and snippets.

View lulalala's full-sized avatar

lulalala lulalala

View GitHub Profile
it do
expect{
@return_result = foo()
}.to change(Foo, :count).from(1).to(0)
expect(@return_result).to eq(true)
end
@lulalala
lulalala / books_controller.rb
Created June 9, 2015 15:31
Simple controller
# This is generated using toy_scaffold,
# and is meant for learning purposes only.
# It aims for more clarity for beginners, therefore
# sacrifices some best practices and DRY principle.
class BooksController < ApplicationController
# GET /books
# View file is at app/views/books/index.html.erb
def index
@books = Book.all
@lulalala
lulalala / time_elapsed.rb
Created July 22, 2015 02:09
sprockets time elapsed
start_time = Time.now.to_f
time_elapsed = lambda { ((Time.now.to_f - start_time) * 1000).to_i }
logger.info "#{msg} 404 Not Found (#{time_elapsed.call}ms)"
@lulalala
lulalala / gist:90c0f82fb0177bffad52
Created July 22, 2015 03:08
so many rack wrappers
#<Rack::BodyProxy: @body=
#<Rack::BodyProxy: @body=
#<Rack::BodyProxy: @body=
#<Rack::BodyProxy: @body=
#<Rack::BodyProxy: @body=
#<Rack::BodyProxy: @body=
#<Sprockets::StaticAsset
@lulalala
lulalala / gist:47b71b7f6014153a0351
Created October 7, 2015 03:18
Active Model errors landmine
book.errors.keys
[:title]
book.errors[:base].present?
false
book.errors.keys
[:title, :base]
# and someone does this
if book.errors.keys.include?(:base)
boom!()
login(user)
expect(page).to have_content("Logged in")
visit foo_path
@lulalala
lulalala / millisecond.rb
Created October 23, 2015 04:50
Rails 4.2 millisecond not persisted
foo.save
foo.end_at.strftime('%H:%M:%S.%L')
"20:38:34.447"
foo.reload.end_at.strftime('%H:%M:%S.%L')
"20:38:34.000"
require 'nokogiri'
require 'open-uri'
(1..91).each do |i|
f = Fountain.create(issue:i, title:i)
end
(1..91).each do |i|
f = Fountain.find_by(issue:i)
i_s = "%02d" % i
# controller
@open_id = params[:open].to_i
# spec
expect(assigns(:open_id)).to eq(@params[:open].to_i)
irb(main):016:0> Money.new(1.5) == 0.015.to_money
=> true
irb(main):017:0> Money.new(1.5).instance_variable_get(:@fractional).to_s
=> "1.5"
irb(main):018:0> 0.015.to_money.instance_variable_get(:@fractional).to_s
=> "2.0"