Skip to content

Instantly share code, notes, and snippets.

View airblade's full-sized avatar

Andy Stewart airblade

View GitHub Profile
def reify_has_manys(model, lookback)
model.class.reflect_on_all_associations(:has_many).each do |assoc|
# Get live children.
children = model.send assoc.name
# Ensure child models are using PaperTrail.
if children.first.respond_to? :version_at
# Handle updated children, and children created since the parent's version.
children_then = []
children.each do |child|
if (child_as_it_was = child.version_at(created_at - lookback.seconds))
@airblade
airblade / application_helper.rb
Created April 7, 2011 10:55
Nested layouts with HAML in Rails 3
module ApplicationHelper
def inside_layout(layout = 'application', &block)
render :inline => capture_haml(&block), :layout => "layouts/#{layout}"
end
end
<MedlineCitation Owner="NLM" Status="MEDLINE">
<PMID Version="1">19663746</PMID>
<DateCreated>
<Year>2009</Year>
<Month>08</Month>
<Day>11</Day>
</DateCreated>
<DateCompleted>
<Year>2010</Year>
<Month>03</Month>
@airblade
airblade / gist:990699
Created May 25, 2011 09:58
Taxonomy names matching "streptococcus pneumonia"
1313 | Streptococcus pneumoniae | | scientific name |
1313 | Streptococcus pneumoniae (Klein 1884) Chester 1901 | | authority |
10728 | Streptococcus pneumoniae bacteriophage HB-3 | | synonym |
10728 | Streptococcus pneumoniae phage HB-3 | | scientific name |
12402 | phage EJ-1 #note host Streptococcus pneumoniae | | misspelling |
120574 | Streptococcus pneumoniae bacteriophage MM1 | | synonym |
170187 | Streptococcus pneumoniae TIGR4 | | scientific name |
171101 | Streptococcus pneumoniae (strain ATCC BAA-255 / R6) | | synonym |
171101 | Streptococcus pneumoniae R6 | | scientific name |
171101 | Streptococcus pneumoniae str. R6 | | synonym |
@airblade
airblade / new-git.sh
Created June 9, 2011 17:50 — forked from topfunky/new-git.sh
Create a new git repository on a remote server. Assumes that you're sitting in an initialized Git repo.
function new-git () {
ssh git@example.com "mkdir $1.git && cd $1.git && git --bare init"
git remote add origin git@example.com:$1.git
git push origin master
git config branch.master.remote origin
git config branch.master.merge refs/heads/master
git config push.default current
}
@airblade
airblade / gist:1218752
Created September 15, 2011 07:37
Passenger 3.0.9: failure to compile Apache2 module
sudo passenger-install-apache2-module
Welcome to the Phusion Passenger Apache 2 module installer, v3.0.9.
This installer will guide you through the entire installation process. It
shouldn't take more than 3 minutes in total.
Here's what you can expect from the installation process:
1. The Apache 2 module will be installed for you.
2. You'll learn how to configure Apache.
@airblade
airblade / config.ru
Created December 15, 2011 09:08 — forked from mislav/config.ru
Shortest simplest sweetest web "hello {NAME}" in Ruby
run ->(e){ p=Hash[*e['QUERY_STRING'].split(/[&=]/)]; [200, {'Content-type'=>'text/html'}, ["Hello #{p['name']}!"]] }
@airblade
airblade / pre-commit
Created February 6, 2012 14:51
Git pre-commit hook to prevent direct commits to staging or production branches.
#!/bin/sh
BRANCH=$(git name-rev HEAD | cut -d' ' -f2)
if [ "$BRANCH" = "staging" -o "$BRANCH" = "production" ]
then
echo "You must not commit to staging or production."
exit 1
fi
@airblade
airblade / nginx
Created February 7, 2012 09:41
Nginx configuration for Rails 2.3 apps
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon (Centos)
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
@airblade
airblade / crontab
Created March 6, 2012 09:27
Nginx and Unicorn configuration for Rails 3.0.x apps.
@reboot cd /var/www/apps/myapp/current && bundle exec unicorn -c /var/www/apps/myapp/current/config/unicorn.rb -E production -D