Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Michael Grosser grosser

🎯
Focusing
View GitHub Profile
@grosser
grosser / mil_with_mail.rb
Created Jan 14, 2014
Making rails 2 use mail instead of tmail
View mil_with_mail.rb
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 Nov 23, 2013
Make unicorn normally write to files even if they are not synced by @eac
View unicorn_utils_sync_patch.rb
# 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 Jul 12, 2013
Audit time taken by different methods
View audit.rb
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 Dec 15, 2015
Non-blocking datadog metrics / reporting
View lazydog.rb
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 Dec 12, 2015
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)' ```
View Gemfile
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 Dec 11, 2015
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
View fix.rb
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 Aug 19, 2012 — forked from cayblood/rake_autocomplete.rb
Bash autocomplete script for rakefiles
View rake_autocomplete.rb
#!/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>
View gist:3045364
`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
View gist:3045258
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
@grosser
grosser / Reporting.txt
Created May 11, 2012 — forked from webmat/benchmark.rb
Benchmark Your Bundle
View Reporting.txt
gem install pru
bundle exec ruby benchmark.rb > report
cat report | pru 'split(/\s+/)' 'reject{|a| a.size != 6 }.map{|a| [a[0], a[1]] }.sort_by(&:last).reverse.map{|a| "#{a[0].ljust(21)} -> #{a[1]}" }.join("\n")' | head -n20
haml -> 0.320000
prawn -> 0.270000
mail -> 0.240000
webrat -> 0.230000
newrelic_rpm -> 0.210000