Skip to content

Instantly share code, notes, and snippets.

View justinko's full-sized avatar

Justin Ko justinko

View GitHub Profile
RSpec.configure do |config|
config.on_error do |example|
if example.example_group.metadata[:type] == :request
example.instance_eval { save_and_open_page }
end
end
end
class H < Array
def a
reject {|i| i == :a}
end
def b
select {|i| i == :b}
end
def c
@justinko
justinko / delegate.rb
Created May 21, 2011 21:46
A simple delegate to an array
require 'forwardable'
class BenArray
extend Forwardable
def initialize
@array = []
end
def_delegators :@array, :select, :reject, :<<
module RSpec
module Mocks
module AnyInstance
class Chains < Array
def add(chain)
push(chain)
chain
end
@justinko
justinko / gist:994238
Created May 26, 2011 22:22
Reverse Polish Calculator
module RPN
def self.start
loop { stack.add(Line.new) }
end
def self.stack
@stack ||= Stack.new
end
class Stack
describe PostsController do
describe "PUT #update" do
let(:post) { Factory(:post) }
context "when logged in as post's author" do
before { sign_in post.author }
it "allows the post to be updated" do
do_action
response.should be_success
@justinko
justinko / gist:1179343
Created August 29, 2011 20:34
Run a rake task in a separate process in your Rails app
desc 'Execute a task in the background'
task :background, :task do |t, args|
rake_path = `which rake`.chomp
app_path = Dir.pwd
cmd = "#{rake_path} #{args.task} --trace --rakefile #{app_path}/Rakefile >> #{app_path}/log/rake.log 2>&1 &"
puts 'Executing:'
puts " #{cmd}"
system cmd
end
@justinko
justinko / callback_disabler.rb
Created September 2, 2011 00:20
Disable raw filter callbacks in RSpec
# In spec/support/callback_disabler.rb
module CallbackDisabler
def self.store_callbacks(model, filters)
model = constantize(model)
filters.each do |filter|
model.send("_#{filter}_callbacks").each do |callback|
stored_callbacks[model] << {
filter: filter, kind: callback.kind, raw_filter: callback.raw_filter
}
module ViewDecorator
include RailsViewHelpers
def link_to_root
link_to # ...
end
end
# Most likely you won't do this because you need to
# pass some kind of data/state
def find_in_batches(model)
current_batch, count = 0, model.count
while count > 0 do
model.all.skip(current_batch * 1000).limit(1000).each do |doc|
yield doc
end
count -= 1000
current_batch += 1
end