Skip to content

Instantly share code, notes, and snippets.

@mislav
mislav / update_counters.rb
Created December 2, 2009 19:32
One solution to update out-of-sync association counters
def update_counter(record, *names)
updates = names.each_with_object({}) do |name, memo|
counter = "#{name}_count"
diff = record.send(name).count - record.send(counter)
memo[counter] = diff unless diff.zero?
end
record.class.update_counters(record.id, updates) if updates.any?
end
Community.paginated_each(:per_page => 100) do |record|
# Goal: Allow addition of instances to a collection in a factory-built object
# when those instances require references to the parent.
# Typically occurs in Rails when one model has_many instances of another
# See more at:
# http://stackoverflow.com/questions/2937326/populating-an-association-with-children-in-factory-girl
class Factory
def has_many(collection)
# after_build is where you add instances to the factory-built collection.
@rcrowley
rcrowley / unicorn.conf.rb
Created August 17, 2010 20:03
Unicorn logging to syslog
class Syslogger
def initialize
read, @write = IO.pipe
fork do
@write.close
$stdin.reopen read
exec *%w(logger -trails)
end
read.close
worker = Delayed::Worker.new(:min_priority => ENV['MIN_PRIORITY'], :max_priority => ENV['MAX_PRIORITY'])
begin
worker.say "Starting job worker"
trap('TERM') { worker.say 'Exiting...'; $exit = true }
trap('INT') { worker.say 'Exiting...'; $exit = true }
loop do
result = nil
@gaizka
gaizka / null_param_patch_for_rails_2_series.rb
Created June 5, 2012 16:57
Patch for Rails null param vulnerability (CVE-2012-2660) ported to Rails 2.3.x versions
# Adapted patch for CVE-2012-2660 rails vulnerability to Rails 2 versions
# https://groups.google.com/group/rubyonrails-security/browse_thread/thread/f1203e3376acec0f
#
# 1- Drop it at your_app/config/initializers/
# 2- Remember to pass your tests/specs
# 3- Profit!
module ActionController
class Request < Rack::Request
alias_method :normalize_parameters_with_null_vulnerability, :normalize_parameters
@asalant
asalant / setup.sh
Created June 7, 2012 20:41
Good Eggs dev workstation setup with chef soloist
# bash <(curl -s https://raw.github.com/gist/2891426)
read -p "Install Xcode (from the App store) and gcc for Lion (http://github.com/kennethreitz/osx-gcc-installer) and press enter to continue."
# setup your ssh keys for github
if ! [ -e "$HOME/.ssh/id_rsa.pub" ]
then
echo "Generating ssh key..."
read -p "Please enter the email you want to associate with your ssh key: " email
ssh-keygen -t rsa -C "$email"
<IfModule mod_fastcgi.c>
AddHandler fastcgi-script .fcgi
</IfModule>
<IfModule mod_fcgid.c>
AddHandler fcgid-script .fcgi
</IfModule>
Options +FollowSymLinks +ExecCGI
RewriteEngine On
@nbibler
nbibler / gist:5307941
Last active October 7, 2021 09:38
A .powrc file which works with RVM's .rvmrc or .ruby-version (+ .ruby-gemset) configuration files.
if [ -f "$rvm_path/scripts/rvm" ]; then
source "$rvm_path/scripts/rvm"
if [ -f ".rvmrc" ]; then
source ".rvmrc"
fi
if [ -f ".ruby-version" ]; then
rvm use `cat .ruby-version`
fi
@natew
natew / deploy.rb
Last active December 16, 2015 17:39
deploy.rb rolling restart with unicorn deploy.rb in your rails app /config/deploy.rb on your server: /etc/unicorn/site.conf /etc/init.d/unicorn /etc/nginx/sites-available/sitename.conf
require "bundler/capistrano"
load 'deploy/assets'
# Pretty colors
require 'capistrano_colors'
# rbenv and ssh forwarding
set :default_environment, { 'PATH' => "$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH" }
set :default_run_options, { :pty => true, :shell => '/bin/zsh' }
set :ssh_options, { :forward_agent => true }
@flarik
flarik / dot.powrc.sh
Created June 12, 2013 10:25
Pow's .porwrc config file for use with RVM's config files .rvmrc or .ruby-version (+ optional .ruby-gemset)
if [ -f "${rvm_path}/scripts/rvm" ]; then
source "${rvm_path}/scripts/rvm"
if [ -f ".rvmrc" ]; then
source ".rvmrc"
elif [ -f ".ruby-version" ] && [ -f ".ruby-gemset" ]; then
rvm use `cat .ruby-version`@`cat .ruby-gemset`
elif [ -f ".ruby-version" ]; then
rvm use `cat .ruby-version`