Skip to content

Instantly share code, notes, and snippets.

View aliang's full-sized avatar

Alvin Liang aliang

View GitHub Profile
@aliang
aliang / reserved_name_validator.rb
Created December 30, 2010 23:49
validate reserved names in Rails 3
# app/validators/reserved_name_validator.rb
class ReservedNameValidator < ActiveModel::EachValidator
RESERVED_NAMES = %w{
about account add admin api
app apps archive archives auth
blog
config connect contact create
delete direct_messages downloads
edit email
faq favorites feed feeds follow followers following
@aliang
aliang / publicize_methods.rb
Created December 12, 2013 20:35
Expose private methods for testing. Can accept a block, or just call in RSpec before/after blocks
class Class
def publicize_methods
@_saved_private_instance_methods = self.private_instance_methods
self.class_eval { public *@_saved_private_instance_methods }
if block_given?
yield
reprivatize_methods
end
end
<form>
Name: <input type="text" name="name"/>
Age: <input type="text" name="age"/>
<input type="submit" value="Submit"></input>
</form>
@aliang
aliang / each_by_page.rb
Last active December 16, 2015 20:19
helper to iterate all by pagination using kaminari
# 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
aliang / gist:340915
Created March 23, 2010 07:15 — forked from RStankov/gist:162593
bubble and delegate focus, blur, change with prototype
(function(){
function focusInHandler(e){
Event.element(e).fire("focus:in");
}
function focusOutHandler(e){
Event.element(e).fire("focus:out");
}
if (document.addEventListener){
document.addEventListener("focus", focusInHandler, true);
@aliang
aliang / gist:1393029
Created November 25, 2011 08:02
monkey patch to make pymongo not throw ridiculous AutoReconnect errors
# Stolen from http://paste.pocoo.org/show/224441/
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):
models = ActiveRecord::Base.descendants
models.each do |m|
last_id = m.last.id
m.connection.execute("ALTER SEQUENCE #{m.table_name}_id_seq RESTART WITH #{last_id}")";
end
@aliang
aliang / knife.rb
Created July 23, 2012 09:44 — forked from wilmoore/knife.rb
Base "knife" configuration for a standard chef-solo setup
# .chef/knife.rb
# SEE: http://wiki.opscode.com/display/chef/Troubleshooting+and+Technical+FAQ
# set some sensible defaults
current_dir = File.dirname(__FILE__)
user = ENV['OPSCODE_USER'] || ENV['USER']
log_level :debug
log_location STDOUT
node_name `hostname`
client_key ''
@aliang
aliang / knife.rb
Created March 22, 2012 21:37 — forked from jtimberman/knife.rb
Commented knife.rb for all the things
# 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
aliang / installation.sh
Created February 18, 2012 22:39 — forked from mikhailov/installation.sh
Nginx+passenger application config: ssl redirection, http headers, passenger optimal settings. see details: http://mikhailov.posterous.com/nginx
$ cd /usr/src
$ wget http://nginx.org/download/nginx-0.8.52.tar.gz
$ 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