Skip to content

Instantly share code, notes, and snippets.

View palkan's full-sized avatar
🗺️
RailsConf / RubyKaigi

Vladimir Dementyev palkan

🗺️
RailsConf / RubyKaigi
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 / 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_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 / 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 / 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 / gist:766b8d094c1e1f327c24
Last active June 8, 2017 15:56
rails issue #15468
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
gem 'pry-byebug'
GEMFILE
system 'bundle'
@palkan
palkan / keybase.md
Created July 4, 2017 13:04
keybase.md

Keybase proof

I hereby claim:

  • I am palkan on github.
  • I am palkan (https://keybase.io/palkan) on keybase.
  • I have a public key ASD9_7Qr5xvMYx75u8VApLRsuCjYGR87WquHVLTtuUahwAo

To claim this, I am signing this object:

@palkan
palkan / 01_rack_rewrite_config.rb
Created November 7, 2017 09:19
RackRewriteConfig
class RackRewriteConfig
class << self
# Configure named rewrite rule
def configure(name, &block)
raise ArgumentError, "Block is required" unless block_given?
rules[name] = block
end
# Apply named rule to the target
def apply(target, name, *args)
@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 / doc.md
Created August 17, 2018 22:22
[draft] Pundit to Action Policy

From Pundit to ActionPolicy:

  • Remove include Pundit from ApplicationController
  • Add alias authorize authorize!
  • Add authorize :current_user, as: :user
  • Add include ActionPolicy::Policy::Core to ApplicationPolicy
  • Update ApplicationPolicy#initialize:
def initialize(target, user:)