Skip to content

Instantly share code, notes, and snippets.

@kml
kml / rails_eager_load_i18n_translations_and_unicode_database.rb
Last active October 15, 2015 11:49
Eager load data before first request
# encoding: utf-8
# Source: https://gist.github.com/kml/79bd8cedf64caadd18ca
require "active_support/i18n_railtie"
# Eager load translations.
# https://github.com/svenfuchs/i18n/blob/master/lib/i18n/backend/simple.rb
# rails c
# I18n.backend.initialized?
@kml
kml / README.md
Last active October 13, 2015 08:33
RecordPlayer

Record responses and play them from local cache.

Installation:

git clone https://gist.github.com/4368cae5de3f7384fd08.git recordplayer
mkdir ~/.recordplayer

Usage:

@kml
kml / Gemfile_puma_single_worker_pidfile_patch.rb
Last active December 9, 2015 14:01
Create pidfile before loading the application. Thanks to this monit/god knows that process exists and will not try to run other instance event when initialization takes a long time.
gem "puma", require: false, platform: :jruby
# Issue: https://github.com/puma/puma/issues/785
begin
require "puma/single"
class ::Puma::Single
alias unpatched_run run
class ClosedStruct < OpenStruct
# https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L176
def method_missing(mid, *args)
return super if @table.has_key?(mid.to_sym)
raise NoMethodError, "undefined method `#{mid}' for #{self}"
end
end
require "active_attr"
require "memoist"
class Model
include ActiveAttr::Model
extend Memoist
# by default ActiveAttr calls typecaster every time attribute reader is called
# this patch adds memoization
# https://github.com/cgriego/active_attr/blob/v0.8.5/lib/active_attr/attributes.rb#L199
@kml
kml / mongoid_extensions_strict_document.rb
Last active August 29, 2015 14:25
Mongoid::Extensions::StrictDocument
# encoding: utf-8
# https://gist.github.com/kml/ce164e1e0c1bde2ab96d
require "mongoid"
module Mongoid
module Extensions
module DefaultScopeOnlyId
def self.included(base)
jruby 1.7.20 (1.9.3p551) 2015-05-04 3086e6a on Java HotSpot(TM) 64-Bit Server VM 1.7.0_79-b15 +jit [darwin-x86_64]
>> $LOAD_PATH << File.expand_path('../vendor', __FILE__)
>> require "frak-0.1.7-SNAPSHOT-standalone.jar"
=> true
>> Java::clojure.lang.RT
=> Java::ClojureLang::RT
>> require "jrclj"
=> true
@kml
kml / rake_disable_db_tasks.rb
Created May 22, 2015 09:06
Rakefile - disable db: tasks in production
# Idea from: http://www.developingandstuff.com/2014/06/disable-dangerous-rake-tasks-in.html
if Rails.env.production?
Rake::Task.tasks.each do |task|
next unless task.name.start_with?("db:")
Rake::Task[task].enhance do
abort "This task is disabled in production."
end
end
@kml
kml / gist:d7a0f27fe0c7c477f2f6
Last active December 13, 2016 20:34
ActiveModel::Model - show nested errors
class PersonData
include ActiveModel::Model
include Virtus.model
attribute :pesel, String
attribute :id_type, String
validates :pesel, presence: true
validates :id_type, presence: true
end
@kml
kml / regexp_breaking_ruby.rb
Last active December 15, 2022 19:44
Regexp breaking Ruby
# encoding: utf-8
require 'minitest'
require 'minitest/autorun'
describe "regexp" do
let(:regexp) { /(\s*(<p>\s*<\/p>)\s*)+\Z/mi }
it "cleans up trailing empty tag from simple string" do
"X <p> </p> ".gsub(regexp, "").must_equal "X"