Skip to content

Instantly share code, notes, and snippets.


Alvin Liang aliang

View GitHub Profile
aliang / each_by_page.rb
Last active Dec 16, 2015
helper to iterate all by pagination using kaminari
View each_by_page.rb
# Probably should investigate how to chain this with scopes
# Also assumes mongoid with a default _id field, and the order_by method
class MyModel
def self.each_by_page(options = {})
per = options[:per] || 25
order_by = options[:order_by] || [:_id, 1]
i = 1
until (current_page = self.order_by(order_by).page(i).per(per)).empty?
current_page.each do |obj|
aliang / knife.rb
Created Jul 23, 2012 — forked from wilmoore/knife.rb
Base "knife" configuration for a standard chef-solo setup
View knife.rb
# .chef/knife.rb
# SEE:
# set some sensible defaults
current_dir = File.dirname(__FILE__)
log_level :debug
log_location STDOUT
node_name `hostname`
client_key ''
aliang / knife.rb
Created Mar 22, 2012 — forked from jtimberman/knife.rb
Commented knife.rb for all the things
View knife.rb
# Knife Configuration File.
# This is a Ruby DSL to set configuration parameters for Knife's
# general options. The default location for this file is
# ~/.chef/knife.rb. If multiple Chef repositories are used,
# per-repository configuration files can be created. A per repository
# configuration file must be .chef/knife.rb in the base directory of
# the Chef repository. For example,
# ~/Development/chef-repo/.chef/knife.rb
aliang /
Created Feb 18, 2012 — forked from mikhailov/
Nginx+passenger application config: ssl redirection, http headers, passenger optimal settings. see details:
$ cd /usr/src
$ wget
$ tar xzvf ./nginx-0.8.52.tar.gz
$ rm ./nginx-0.8.52.tar.gz
$ gem install s3sync capistrano capistrano-ext passenger --no-ri --no-rdoc
$ passenger-install-nginx-module
# Automatically download and install Nginx? 2. No: I want to customize my Nginx installation
# Where is your Nginx source code located?: /usr/src/nginx-0.8.52
# Where do you want to install Nginx to?: /opt/nginx
aliang /
Created Jan 22, 2012 — forked from remcoder/
Compile LESS to CSS automatically whenever a file is modified
#!/usr/bin/env python
# determines filename and extension, then delegates compiling the LESS files to lessc via a shell command
import sys, os.path
from subprocess import call
src = sys.argv[1]
base,ext = os.path.splitext(src)
aliang / gist:1507475
Created Dec 21, 2011
recursive globbing
View gist:1507475
def find_files(directory, pattern):
for root, dirs, files in os.walk(directory):
for basename in files:
if fnmatch.fnmatch(basename, pattern):
filename = os.path.join(root, basename)
yield filename
for f in find_files(".", "*.txt"):
print f
aliang / gist:1393029
Created Nov 25, 2011
monkey patch to make pymongo not throw ridiculous AutoReconnect errors
View gist:1393029
# Stolen from
from pymongo.cursor import Cursor
from pymongo.connection import Connection
from pymongo.errors import AutoReconnect
from time import sleep
def reconnect(f):
def f_retry(*args, **kwargs):
aliang / parse_query.rb
Created Jun 20, 2011
search query string parser, google-ish
View parse_query.rb
# via
def parse_query s
[nil, :prefix, :plusminus, nil, nil, :phrase, :word].zip(match).select(&:all?)
aliang / triangles.html
Created Jun 20, 2011
triangles without images
View triangles.html
<!DOCTYPE html>
<html lang="zh-CN">
<meta charset="utf-8" />
<style type="text/css">
.a{width:0;height:0;border-top:100px solid #0099ff;border-left:100px solid #ff9900;border-right:100px solid #F6402D;border-bottom:100px solid #8CC63E; float:left;}
width:0;height:0;border-top:100px solid #0099ff;border-left:100px solid transparent;border-right:100px solid transparent;border-bottom:0; margin-left:60px; float:left;}
.c{width:0;height:0;border-top:100px solid transparent;border-left:0;border-right:100px solid transparent;border-bottom:100px solid #0099ff; margin-left:60px; float:left;}
aliang / Mac SSH Autocomplete
Created Jun 14, 2011
Add auto complete to your ssh, put into your .bash_profile
View Mac SSH Autocomplete
_complete_ssh_hosts ()
comp_ssh_hosts=`cat ~/.ssh/known_hosts | \
cut -f 1 -d ' ' | \
sed -e s/,.*//g | \
grep -v ^# | \
uniq | \
grep -v "\[" ;