Skip to content

Instantly share code, notes, and snippets.

View grosser's full-sized avatar
🎯
Focusing

Michael Grosser grosser

🎯
Focusing
View GitHub Profile
@grosser
grosser / rails_2_hacks.rb
Created January 14, 2014 19:53
hacks that help getting a rails 2 app run like a rails 3 app
# test
if Rails::VERSION::MAJOR == 2
# make integration tests use rack response, so we can test our middlewares
# and not only the pure controller response
ActionController::Base.class_eval do
# this is usually done just-in-time by #process but we need to do it earlier
include ActionController::Integration::ControllerCapture
# then we hide last_instantiation from #process
@grosser
grosser / mil_with_mail.rb
Created January 14, 2014 19:29
Making rails 2 use mail instead of tmail
module MailWithMail
# stolen + made static from Rails 3.0 action_mailer/old_api.rb
def self.set_content_type(m, user_content_type, class_default, params)
case
when user_content_type.present?
user_content_type
when m.has_attachments?
if m.attachments.detect { |a| a.inline? }
["multipart", "related", params]
else
@grosser
grosser / unicorn_utils_sync_patch.rb
Created November 23, 2013 02:36
Make unicorn normally write to files even if they are not synced by @eac
# For Rails3, Rails.logger sync = false. Unicorn's log detection needs to accomodate this
module Unicorn
module Util
# :stopdoc:
def self.is_log?(fp)
append_flags = File::WRONLY | File::APPEND
! fp.closed? &&
fp.stat.file? &&
@grosser
grosser / audit.rb
Created July 12, 2013 01:43
Audit time taken by different methods
def audit(klass, method)
klass.class_eval do
define_method :"#{method}_with_audit" do |*args|
result = nil
time = Benchmark.realtime do
result = send(:"#{method}_without_audit", *args)
end
$time ||= {}
$time[klass] ||= {}
$time[klass][method] ||= 0
@grosser
grosser / lazydog.rb
Last active December 15, 2015 22:09
Non-blocking datadog metrics / reporting
require "dogapi"
require "celluloid/autostart"
require "singleton"
class LazyDog
include Celluloid
include Singleton
def emit_point(*args)
client.emit_point(*args)
@grosser
grosser / Gemfile
Last active December 12, 2015 01:08
Bundeling the same project with rails 2 and rails 3 (multiple gem versions but same gemfile) ``` ln -s Gemfile Gemfile.rails3 bundle BUNDLE_GEMFILE=Gemfile.rails3 bundle ``` using vendor/cache ? -> save disc space: ``` cd vendor/cache_rails3 ls ../cache | pru '%x{rm #{self} && ln -s ../cache/#{self}} if File.exist?(self)' ```
rails3 = ENV["BUNDLE_GEMFILE"].to_s.include?("rails3")
...
if rails3
# make bundler use a different cache dir
class << Bundler
def app_cache
root.join("vendor/cache_rails3")
end
@grosser
grosser / fix.rb
Last active December 11, 2015 02:59
Rails 2.3 fix for https://github.com/rails/rails/issues/8832 / CVE-2013-0155 prevent nil insertion, do a deep_munge for all non-browser input methods (which cannot produce empty hash or array or nil) ActiveRecord does not need the empty hash patch since e.g. `User.find_by_name({})` is ok
if Rails::VERSION::MAJOR == 2
ActionController::Base.class_eval do
def params_with_nil_fix
params = params_without_nil_fix
unless params.instance_variable_get(:@nil_fixed)
params.instance_variable_set(:@nil_fixed, true)
# normal form params cannot include nil/empty hash/empty array, just json/xml
deep_munge(params) unless ["application/x-www-form-urlencoded", "multipart/form-data", nil].include?(request.try(:content_type))
end
@grosser
grosser / rake_autocomplete.rb
Created August 19, 2012 15:50 — forked from cayblood/rake_autocomplete.rb
Bash autocomplete script for rakefiles
#!/usr/bin/env ruby
# Complete rake tasks script for bash
# Save it somewhere and then add
# complete -C path/to/script -o default rake
# to your ~/.bashrc
# Xavier Shay (http://rhnh.net), combining work from
# Francis Hwang ( http://fhwang.net/ ) - http://fhwang.net/rb/rake-complete.rb
# Nicholas Seckar <nseckar@gmail.com> - http://www.webtypes.com/2006/03/31/rake-completion-script-that-handles-namespaces
# Saimon Moore <saimon@webtypes.com>
`rm accounts/*`
ips = File.readlines('ips.txt')
accounts = File.readlines('accounts.txt')
mapping = {}
accounts.each_with_index.map do |subdomain, i|
ip = ips[i % ips.size].strip
mapping[ip] ||= []
mapping[ip] << subdomain.strip
Dir["tmp/*.txt"].each do |ip_file|
puts "-" * 50
puts ip_file
ip = File.basename(ip_file).sub('.txt','')
puts "ip #{ip}"
puts "result:"
puts `cat #{ip_file} | ruby script/patch_dns #{ip}`
raise unless $?.success?
end