Skip to content

Instantly share code, notes, and snippets.

View wojtha's full-sized avatar

Vojtěch Kusý wojtha

View GitHub Profile
@wojtha
wojtha / changelog.rb
Last active March 9, 2020 12:38
Changelog generator. Install to some $PATH location and make it executable.
#!/usr/bin/env ruby
# Example usage:
#
# script/changelog --init
#
# script/changelog v2.23.0 v2.23.1
#
# script/changelog v2.23.0 HEAD -r v2.23.1
#
@wojtha
wojtha / webpack_test_build.rb
Created August 6, 2019 13:59
Webpack build before the test suite is executed
# See https://gist.github.com/naps62/a7dcce679a45592714ea6477108f0419
# See https://github.com/rails/webpacker/issues/59
# See https://raw.githubusercontent.com/codeforamerica/open311status/4fe31fec5e70e55c313616d0a1e11f7f7f460528/spec/support/webpack.rb
module WebpackTestBuild
TS_FILE = Rails.root.join('tmp', 'webpack-spec-timestamp')
PACK_MANIFEST = Rails.root.join('public', 'packs', 'manifest.json')
JAVASCRIPT_FILES = Rails.root.join('app', 'javascript', '**', '*')
class << self

Description

Changes

@wojtha
wojtha / override_new.rb
Created August 3, 2018 15:56
How to dispatch `self.new` to subclasses and not end up with stack level too deep error...
module Steps
class Step
DEFAULT_VERSION = 2
def self.new(attrs = {})
version = attrs.fetch(:step_version, DEFAULT_VERSION)
klass = "StepV#{version}".constantize
klass.new(attrs)
end
end
@wojtha
wojtha / hash_deep_set.rb
Created July 31, 2018 09:43
Hash deep set in ruby
class Query
attr_reader :query
def initialize
@query = { b: 2 }
end
def deep_set(*keys, val)
key = keys.pop
keys.inject(query) { |hsh, k| hsh.key?(k) ? hsh[k] : hsh[k] = {}; hsh[k] }[key] = val
require 'jbuilder'
Post = Struct.new(:id, :title, :content, :comments)
Comment = Struct.new(:id, :author, :body)
class PostWithCommentsSerializer
attr_reader :object
def initialize(object)
@wojtha
wojtha / feature_flag.rb
Created February 21, 2018 08:59
Feature flags in 90 LOC for ruby apps (see https://gist.github.com/wojtha/43c68be62757d0b7030485efcf13583b for 20 LOC version)
# Class FeatureFlag defines and stores feature flags
#
# @example Configuration
#
# FEATURE = FeatureFlag.new do |feature|
# feature.define(:new_user_profile) do |user_id:|
# Admin.where(user_id: user_id).exists?
# end
#
# feature.define(:third_party_analytics) do
@wojtha
wojtha / mongo_retry.rb
Created January 16, 2018 10:25
Module Mongo::Retry provides retry support on mongo connection or operation failures
# Module Mongo::Retry provides retry support on mongo connection or operation failures
#
# @note Bit enhanced replacement of Mongo::Retryable (included Mongo Ruby Driver since 2.1.0)
#
# @see https://jira.mongodb.org/browse/SERVER-14322
# @see https://gist.github.com/bhbryant/1230938
# @see https://www.compose.com/articles/your-drivers-are-not-magic-testing-your-application-for-high-availability/
# @see https://gist.github.com/reidmorrison/1498297
# @see https://github.com/reidmorrison/mongo_ha
# @see https://github.com/mongodb/mongo-ruby-driver/blob/master/lib/mongo/retryable.rb
@wojtha
wojtha / activerecord_finder.rb
Created November 9, 2017 18:10
ActiveRecord::Finder implementation with chainable scopes!
module ActiveRecord
module Finder
module Scope
extend ActiveSupport::Concern
attr_reader :relation
def initialize(relation)
@relation = relation
end
@wojtha
wojtha / 00_active_support.rb
Last active November 1, 2017 23:19
Make Rails test.log more useful
# Based on https://ilikestuffblog.com/2008/06/18/find-tests-more-easily-in-your-testlog/
class ActiveSupport::TestCase
setup :log_example
private
def log_example
Rails.logger.info "\nStarting #{@method_name}\n#{'-' * (9 + @method_name.length)}\n"
end