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
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 ()
{
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
comp_ssh_hosts=`cat ~/.ssh/known_hosts | \
cut -f 1 -d ' ' | \
sed -e s/,.*//g | \
grep -v ^# | \
uniq | \
grep -v "\[" ;
@aliang
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
# http://wholemeal.co.nz/blog/2011/04/05/rendering-from-a-model-in-rails-3/
class MyModel < ActiveRecord::Base
# Use the my_models/_my_model.txt.erb view to render this object as a string
def to_s
ActionView::Base.new(Rails.configuration.paths.app.views.first).render(
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:
#
# https://github.com/rails/rails/blob/11fd052aa815ae0255ea5b2463e88138fb3fec61/activerecord/lib/active_record/attribute_assignment.rb#L113-L218
#
# and DataMapper:
@aliang
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("http://www.whatismyip.com/automation/n09230945.asp")
req = Net::HTTP::Get.new(url.path)
req.add_field("X-Forwarded-For", "0.0.0.0")
# For content type, you could also use content_type=(type, params={})
@aliang
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
aliang / parse_query.rb
Created Jun 20, 2011
search query string parser, google-ish
View parse_query.rb
# via http://jablan.radioni.ca/post/4982485974/parsing-search-query-in-ruby
def parse_query s
s.scan(/((\S+)\:\s?)?([+-])?(("(.+?)")|(\S+))/).map{|match|
Hash[
[nil, :prefix, :plusminus, nil, nil, :phrase, :word].zip(match).select(&:all?)
]
}
end
@aliang
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
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 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?
yield
reprivatize_methods
end
end
View user_form.html
<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 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.