Skip to content

Instantly share code, notes, and snippets.

View durran's full-sized avatar

Durran Jordan durran

View GitHub Profile
class User
include Mongoid::Document
has_many :cars do
def find_or_foobar_by(attrs)
find_or_create_by(attrs)
end
end
end
@durran
durran / gist:1075777
Created July 11, 2011 12:57
Mongoid identity map and fibers
# See: https://github.com/mongoid/mongoid/blob/identity-map/spec/unit/mongoid/identity_map_spec.rb#L367
# In order for the identity map to work when using fibers the
# entire unit of work must happen on the same fiber since it's using
# thread locals for storage.
# The following example is correct:
fiber = Fiber.new do
Person.find(id)
Person.find(id) # This query hits the identity map.
@durran
durran / benchmarks.txt
Created August 2, 2011 16:55
Mongoid Benchmarks
2.1.0-stable branch
Creating indexes...
Starting benchmark...
user system total real
[ Root Document Benchmarks ]
[ 1000 ]
#create 0.420000 0.010000 0.430000 ( 0.431558)
#each 0.040000 0.010000 0.050000 ( 0.040263)
@durran
durran / simple_form.rb
Created September 16, 2011 11:25
Mongoid with simple form.
module SimpleForm
class FormBuilder
alias _find_association_reflectio find_association_reflection
def find_association_reflection(association) #:nodoc:
if reflection = _find_association_reflectio(association)
ActiveModelMetadataProxy.new reflection
end
end
@durran
durran / resque
Created December 1, 2011 14:08
Resque Config Rackspace (init.d)
#! /bin/sh
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Resque worker"
NAME=resque
DAEMON=/usr/local/rvm/bin/rvm
DAEMON_ARGS="1.9.3@application exec rake resque:work"
DAEMON_DIR="/data/application"
PIDFILE=/var/run/$NAME/resque.pid
USER=vader
@durran
durran / redis.monitrc
Created December 1, 2011 14:11
Redis Monit Config
check process redis
with pidfile /var/run/redis.pid
start program = "/etc/init.d/redis-server start"
stop program = "/etc/init.d/redis-server stop"
if failed host 127.0.0.1 port 6379 then restart
if 5 restarts within 5 cycles then timeout
@durran
durran / post-receive
Created December 1, 2011 14:03
Post Receive Hook Rackspace (staging)
#!/bin/sh
sudo rm -rf /usr/local/rvm/gems/ruby-1.9.3-p0@application/cache/
set -e
cd ..
unset GIT_DIR
env -i git reset --hard
sudo /usr/local/rvm/bin/rvm 1.9.3@application exec bundle install
RAILS_ENV=staging RAILS_GROUPS=assets /usr/local/rvm/bin/rvm 1.9.3@application exec rake assets:precompile
/usr/local/rvm/bin/rvm 1.9.3@application exec rake db:seed
@durran
durran / unicorn
Created December 1, 2011 14:07
Unicorn Config Rackspace (init.d)
#! /bin/sh
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Unicorn server"
NAME=unicorn
RVM=/usr/local/rvm/bin/rvm
RVM_EXEC_ARGS="1.9.3@application exec"
RAILS_DIR=/data/application
DAEMON=$RVM
DAEMON_ARGS="$RVM_EXEC_ARGS $RAILS_DIR/bin/unicorn -c $RAILS_DIR/config/unicorn.rb -E staging -D"
@durran
durran / nginx
Created December 1, 2011 14:09
Nginx Config Rackspace (init.d)
#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx
# Include nginx defaults if available
if [ -f /etc/default/nginx ]; then
. /etc/default/nginx
fi
@durran
durran / nginx.monitrc
Created December 1, 2011 14:10
Nginx Monit Config
check process nginx
with pidfile "/var/run/nginx.pid"
start program = "/etc/init.d/nginx start"
stop program = "/etc/init.d/nginx stop"
if failed host 31.222.185.182 port 80 protocol http then restart
if 5 restarts within 5 cycles then timeout