Skip to content

Instantly share code, notes, and snippets.

View lulalala's full-sized avatar

lulalala lulalala

View GitHub Profile
@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"
login(user)
expect(page).to have_content("Logged in")
visit foo_path
@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!()
@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 / 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 / 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
it do
expect{
@return_result = foo()
}.to change(Foo, :count).from(1).to(0)
expect(@return_result).to eq(true)
end
@lulalala
lulalala / over_wrapping.rb
Created April 16, 2015 01:24
over wrapping
def foo(role)
if !teacher?(role)
...
def teacher?(role)
role == :teacher
@lulalala
lulalala / model.rb
Created December 11, 2014 02:45
Bug inside write_attribute
# insdie model
def write_attribute(attr_name, value)
super
# Association assignment take effect after super
case attr_name
when 'admin_editing'
pp "Inside write_attribute: #{self.admin_editing}"
end
end
module Global
def self.add_attribute(name)
value = yield.freeze
self.define_singleton_method(name) do
value
end
end
end
Global.add_attribute(:topic_api_markdown_renderer){