Skip to content

Instantly share code, notes, and snippets.

@kml
kml / virtus_attribute_coercer_call_patch.rb
Last active December 13, 2016 22:51
Don't raise and rescue exception - just check
# encoding: utf-8
# Source: https://gist.github.com/kml/da4f7cf70008986b3ba5
# Issue: https://github.com/solnic/coercible/issues/16
require "virtus/attribute/coercer"
# Coercible::Coercer::Object#to_string doesn't know how to coerce nil
# https://github.com/solnic/virtus/blob/master/lib/virtus/attribute/coercer.rb#L31
@kml
kml / assets-nginx.conf
Last active February 26, 2016 20:48
Custom proxy cache server for assets speedup in *development* environment.
# Custom proxy cache server for assets speedup in *development* environment.
#
# Usage:
# Foreground (for tests):
# nginx -c $(pwd)/assets-nginx.conf -g 'daemon off; error_log stderr;'
#
# Background:
# nginx -c $(pwd)/assets-nginx.conf
# nginx -c $(pwd)/assets-nginx.conf -s <SIGNAL>
# signals: stop, quit, reopen, reload
@kml
kml / .ruby-version
Last active August 5, 2016 08:56
NestedAttributes
2.3.1
@kml
kml / rails-boot-default-options.rb
Last active December 13, 2016 22:36
config/boot.rb with changed rails server defaults
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup'
require "rails/commands/server"
Rails::Server.class_eval do
def default_options
super.merge(Port: 3000, Host: "127.0.0.1")
end
end
@kml
kml / i18n.coffee
Created May 20, 2016 18:43
Simple I18n implementation for JavaScript
# Example usage:
# I18n.t("application.hello")
# I18n.t("hello", scope: "application")
# Application.t("hello")
#
# Application.t("missing")
# Error: translation missing: pl.application.missing
#
# I18n.locale = "ru"
# Application.t("hello")
# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment', __FILE__)
app = Rack::Builder.new do
map "/ping" do
run lambda { |_env| [200, {'Content-Type' => 'text/plain'}, ['pong']] }
end
map "/" do
# https://github.com/rest-client/rest-client/blob/ac388df8b904b0ff839ac73496af5b75ab683076/lib/restclient/request.rb#L575
# RestClient logs messages using RestClient.log << "message"
# that ignores default logger formatter
class RestClientLogAdapter
def initialize(logger: Logger.new(nil), level: :info)
@logger = logger
@level = level
end
def <<(message)
@kml
kml / bson_bignum_patch.rb
Last active December 8, 2016 10:23
Modifies ::BSON::ByteBuffer#get_int64 to return Fixnum not Bignum
# ByteBuf#getInt64 should return Fixnum not Bignum
# Issue: https://jira.mongodb.org/browse/RUBY-1189
# Example: http://pastie.org/10978801#1,14,17,19,25
# Gist: https://gist.github.com/kml/4c76c22df26a1fa372c97aff93e873ab
require "bson"
unless BSON::ByteBuffer.new(2200000227.to_bson.to_s).get_int64.is_a?(Bignum)
abort "#{__FILE__}: BSON::ByteBuffer#get_int64 already fixed! Remove this patch."
end
@kml
kml / pry_pretty_print.rb
Created December 13, 2016 22:35
Pry-based pretty-printer for Ruby objects.
require "pry"
def pp(value)
pager = Pry::Pager::NullPager.new(STDOUT)
Pry::ColorPrinter.pp(value, pager, Pry::Terminal.width! - 1)
end
alias ap pp
# config/initializers/apartment.rb
require "apartment/reloader"
# skip Apartment::Reloader for assets
# (Apartment::Reloader is used only in development)
# https://github.com/influitive/apartment/blob/development/lib/apartment/railtie.rb#L43
# https://github.com/influitive/apartment/blob/development/lib/apartment/reloader.rb
Apartment::Reloader.prepend(Module.new do
def call(env)