Skip to content

Instantly share code, notes, and snippets.

View palkan's full-sized avatar

Vladimir Dementyev palkan

View GitHub Profile
@palkan
palkan / active_record_one_love.rb
Created March 7, 2017 09:28
ActiveRecord OneLove
module ActiveRecord
# Share connection between threads to make it possible to wrap system tests in a transaction.
# Also synchonize queries to avoid concurrent writes/reads.
#
# For PostgreSQL adapter.
module OneLove
class << self
attr_reader :connection
def connection=(conn)
@palkan
palkan / rspec-hell.rb
Last active April 18, 2017 17:59
RSpec Hell: run examples concurrently (using threads)
module RSpecHell
def run_examples(reporter)
return super unless metadata[:hell] && !(metadata[:parent_example_group] && metadata[:parent_example_group].key?(:hell))
pool_size = ENV['HELL'].to_i
q = Queue.new
ordering_strategy.order(descendant_filtered_examples).each { |ex| q << ex }
workers = []
results = []
@palkan
palkan / event_profiler.rb
Last active April 3, 2017 18:28
RSpec Events Profiler
module RSpec
class EventProfiler # :nodoc:
# Add #duration method to floats
module FloatDuration
refine Float do
def duration
t = self
format("%02d:%02d.%03d", t / 60, t % 60, t.modulo(1) * 1000)
end
end
@palkan
palkan / factory_doctor.rb
Created March 27, 2017 15:42
FactoryDoc: detect useless data generation in tests
module FactoryGirl
module Doctor
module FloatDuration
refine Float do
def duration
t = self
format("%02d:%02d.%03d", t / 60, t % 60, t.modulo(1) * 1000)
end
end
end
@palkan
palkan / Gemfile
Last active April 27, 2024 02:02
FactoryProf: profiler for your FactoryGirl
# if you want to render flamegraphs
gem "stackprof", require: false # required by flamegraph
gem "flamegraph", require: false
@palkan
palkan / factory_default.rb
Created March 28, 2017 13:18
FactoryDefault: re-use associated objects in factories
module FactoryDefault
module CreateDefaultMethod
def create_default(name, *args, &block)
res = create(name, *args, &block)
FactoryDefault.register(name, res)
res
end
end
module RunnerExt
@palkan
palkan / aggregate_failures.rb
Created March 28, 2017 16:54
Rubocop: RSpec/AggregateFailures
require 'rubocop/rspec/language'
module RuboCop
module Cop
module RSpec
class AggregateFailures < RuboCop::Cop::Cop
GROUP_BLOCKS = RuboCop::RSpec::Language::ExampleGroups::ALL
EXAMPLE_BLOCKS = RuboCop::RSpec::Language::Examples::ALL
def on_block(node)
@palkan
palkan / any_fixture.rb
Created March 28, 2017 21:49
AnyFixture: make DB fixtures from blocks
module AnyFixture
INSERT_RXP = /^INSERT INTO ([\S]+)/
class Cache
attr_reader :store
delegate :clear, to: :store
def initialize
@store = {}
@palkan
palkan / sti_update.rb
Last active June 1, 2017 10:18
STI update
module StiUpdate
def as(type)
if self.type == type
self
else
klass = type.camelize.constantize
if klass.nil?
self
else
became = self.becomes!(klass)
@palkan
palkan / 01_readme.md
Last active January 15, 2024 10:32
Docker Dev