Skip to content

Instantly share code, notes, and snippets.

# encoding: utf-8
=begin
Mongoid::Errors::ReadonlyDocument:
message:
Attempted to persist the readonly document 'Offer'.
summary:
Documents loaded from the database using #only cannot be persisted.
resolution:
Don't attempt to persist documents that are flagged as readonly.
require "mongo/collection/view"
# Add default "comment" for every request to MongoDB.
# https://github.com/mongodb/mongo-ruby-driver/blob/master/lib/mongo/collection/view.rb#L132
Mongo::Collection::View.class_eval do
alias :mongo_collection_view_initialize :initialize
def initialize(collection, filter = {}, options = {})
filter["$comment"] ||= default_comment
mongo_collection_view_initialize(collection, filter, options)
# encoding: utf-8
require "bson/object_id"
BSON::ObjectId.class_eval do
alias to_json to_s
def as_json(*args)
to_s
end
require "active_support/inflector"
# http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-titleize
module LocaleDependentTitleize
def titleize(word)
return humanize(underscore(word)) if I18n.locale == :pl
super
end
end
@kml
kml / rest-client-log-response-patch.rb
Created December 13, 2016 22:41
Log responses from RestClient
module RestClientLogResponsePatch
def process_result(res, &block)
response = super
log_response_with_body(response) unless res.is_a?(Net::HTTPRedirection)
response
rescue RestClient::Exception => exception
log_response_with_body(exception.response)
raise exception
end
@kml
kml / rails_asset_precomiled_patch.rb
Created December 13, 2016 22:39
Don't check if asset is precompiled on development.
# config/initializers/assets.rb
if Rails.env.development?
Rails.application.config.assets.precompile = []
# Don't check if asset is precompiled on development,
# because building "precompiled_assets" list is taking quite long time.
#
# Default: https://github.com/rails/sprockets-rails/blob/89ba006950caba495954fbb1626919bfe4ee7838/lib/sprockets/railtie.rb#L31
# "Called from asset helpers to alert you if you reference an asset URL that
# 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)
@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
@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
# 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)