Skip to content

Instantly share code, notes, and snippets.

View shunchu's full-sized avatar

Shun Chu shunchu

View GitHub Profile
@shunchu
shunchu / go-to-git-branch-on-github-repo.sh
Last active August 29, 2015 14:05
Go to git branch on Github repo
# copy/paste in ~/.bash_profile and invoke it by typing "gh" in commandline
# Opens the github page for the current git repo/branch in your browser
function gh() {
giturl=$(git config --get remote.origin.url)
if [ "$giturl" == "" ]
then
echo "Not a git repository or no remote.origin.url set"
exit 1;
@shunchu
shunchu / go-to-git-branch-in-bitbucket-repo.sh
Created August 21, 2014 22:29
Go to git branch in Bitbucket repo
# copy/paste into ~/.bash_profile and invoke it by typing "bb" in commandline
# Opens the bitbucket page for the current git repo/branch in your browser
function bb() {
giturl=$(git config --get remote.origin.url)
if [ "$giturl" == "" ]
then
echo "Not a git repository or no remote.origin.url set"
exit 1;
@shunchu
shunchu / .vimrc
Created September 2, 2014 01:18
.vimrc
" Use Vim settings, rather then Vi settings (much better!).
" This must be first, because it changes other options as a side effect.
set nocompatible
runtime autoload/pathogen.vim
call pathogen#infect()
call pathogen#helptags()
" EDITOR SETTINGS ========================================================
" ========================================================================
@shunchu
shunchu / add-attrs-to-jbuilder-collections
Created October 23, 2014 20:13
Add attributes to JBuilder collections
# this adds an additional `user_ids` array to each of the `organizations` collection
json.organizations do |json|
json.array! @organizations do |org|
json.merge! org.attributes
json.user_ids org.users.pluck(:id)
end
end
@shunchu
shunchu / add associated object ids in activerecord.rb
Last active August 29, 2015 14:08
add associated object's IDs as arrays for each record in collection
# add associated object's IDs as arrays for each record in collection
Organization.joins(:users)
.select("organizations.*, '#{User.joins(:organization).pluck(:id)}' as user_ids")
.group('organizations.id')
# => { organizations: [{ id: 1, name: 'some name',... user_ids:[1,2,3] }] }
@shunchu
shunchu / Auto focus first form field
Created February 25, 2012 01:15
Auto focus first form field with a set of matching criteria
// focus first form field element matching these criteria
$(':input:visible:enabled:not(.no-focus,
select, input[readonly=readonly],
input[type=checkbox],
input[type=radio],
input[type=submit]):first').focus();
@shunchu
shunchu / Javascript callbacks with args
Created March 19, 2012 18:02
Passing arguments in JavaScript callbacks
http://stackoverflow.com/questions/3458553/javascript-passing-parameters-to-a-callback-function
function tryMe (param1, param2) {
    alert(param1 + " and " + param2);
}
function callbackTester (callback) {
    callback (arguments[1], arguments[2]);
}
callbackTester (tryMe, "hello", "goodbye");
@shunchu
shunchu / dynamic_proxy.rb
Created June 5, 2012 07:17 — forked from nusco/dynamic_proxy.rb
Spell: Dynamic Proxy
# ====================
# Spell: Dynamic Proxy
# ====================
# Forward to another object any messages that don’t match a method.
class MyDynamicProxy
def initialize(target)
@target = target
end
@shunchu
shunchu / box-sizing-with-100percent.css
Created June 19, 2012 20:32
Define "100% - 50px" in css
/* Have padding and border that cut into the element's width instead expanding it. */
textarea {
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box; /* Opera/IE 8+ */
}
/* reference: http://css-tricks.com/box-sizing/ */
@shunchu
shunchu / pg.rake
Created July 8, 2012 10:25 — forked from royratcliffe/pg.rake
Rake task to dump PostgreSQL structure using a compatible pg_dump
namespace :pg do
namespace :structure do
# Does exactly the same as db:structure:dump but with one important
# difference. It tries to use a compatible version of pg_dump. If you see
# the following error message, use pg:structure:dump instead.
#
# pg_dump: server version: 9.1.0; pg_dump version: 9.0.4
# pg_dump: aborting because of server version mismatch
# rake aborted!
# Error dumping database