Skip to content

Instantly share code, notes, and snippets.

Alvin Liang aliang

  • Recovers
  • Burlingame, CA
Block or report user

Report or block aliang

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
aliang / Mac SSH Autocomplete
Created Jun 14, 2011 — forked from jordandcarter/Mac SSH Autocomplete
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 "\[" ;
aliang / mymodel.rb
Created Jun 13, 2011
render from model in Rails 3
View mymodel.rb
# yes, sometimes you need to do this. you get pilloried in a forum if you
# ask about it, though!
# this code taken from
class MyModel < ActiveRecord::Base
# Use the my_models/_my_model.txt.erb view to render this object as a string
def to_s
View virtus-multiparameter_attributes.rb
# Rails datetime_select and similar use multiparameter attributes which are
# these dawful things from the bowels of activerecord and actionpack. This
# module extends virtus models to coerce multiparameter attributes back together
# before assigning attributes.
# Here's the implementation for ActiveRecord:
# and DataMapper:
aliang / custom_headers.rb
Created Mar 9, 2010
setting custom headers with ruby net/http
View custom_headers.rb
# this was surprisingly difficult to find, the documentation and API is poor
require "net/http"
require "uri"
url = URI.parse("")
req =
req.add_field("X-Forwarded-For", "")
# For content type, you could also use content_type=(type, params={})
aliang / shared_examples_for_active_model.rb
Created Jul 7, 2016
ActiveModel::Lint tests for MiniTest 5
View shared_examples_for_active_model.rb
shared_examples_for "ActiveModel" do
require 'minitest/assertions'
require 'active_model/lint'
include MiniTest::Assertions
include ActiveModel::Lint::Tests
# This is the main difference between MiniTest 4 and 5.
# We must keep a counter of assertions.
attr_accessor :assertions
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 / reserved_name_validator.rb
Created Dec 30, 2010
validate reserved names in Rails 3
View reserved_name_validator.rb
# app/validators/reserved_name_validator.rb
class ReservedNameValidator < ActiveModel::EachValidator
about account add admin api
app apps archive archives auth
config connect contact create
delete direct_messages downloads
edit email
faq favorites feed feeds follow followers following
aliang / publicize_methods.rb
Created Dec 12, 2013
Expose private methods for testing. Can accept a block, or just call in RSpec before/after blocks
View publicize_methods.rb
class Class
def publicize_methods
@_saved_private_instance_methods = self.private_instance_methods
self.class_eval { public *@_saved_private_instance_methods }
if block_given?
View user_form.html
Name: <input type="text" name="name"/>
Age: <input type="text" name="age"/>
<input type="submit" value="Submit"></input>
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|
You can’t perform that action at this time.