Skip to content

Instantly share code, notes, and snippets.

View harley's full-sized avatar

Harley Trung harley

View GitHub Profile
# All is good, just a tiny suggestion:
# app/controllers/dashboard_controller.rb: line 18
# Instead of:
@watched_objects = current_user.user_config.watched_data_objects.split(", ").map{|id| DataObject.find(id)}.flatten.group_by(&:data_type)
# Taking of .find syntax accepting list if id's, consider finding them all at once (please test it first)
# also .flatten is not needed)
DataObject.find(current_user.user_config.watched_data_objects.split(", ")).group_by(&:data_tytpe)
@harley
harley / Gemfile
Created March 8, 2012 22:41
Rails Lightweight Stack. Most of this is detailed on Crafting Rails Applications - http://pragprog.com/book/jvrails/crafting-rails-applications
source :rubygems
# We are not loading Active Record, nor Active Resources etc.
# We can do this in any app by simply replacing the rails gem
# by the parts we want to use.
gem "actionpack", "~> 3.2"
gem "railties", "~> 3.2"
gem "tzinfo"
# Let's use thin
@harley
harley / survey_dsl.rb
Created March 29, 2012 01:59 — forked from huned/survey_dsl.rb
a survey DSL as valid ruby syntax
include Survey::DSL
survey 'Finances Questionnaire' do
q "Do you have a Durable Power of Attorney for Finances?" do
yes do
a "Who is the designated agent for handling your finances?" do
field 'Full Name'
phone 'Phone Number'
email 'Email'
@harley
harley / clone_production.sh
Created July 8, 2012 22:45
clone database from production to staging
# remote repo aliases assumed:
# git remote add production production_heroku_app_git_repo
# git remote add staging staging_heroku_app_git_repo
# add addons if not already
# heroku addons:add pgbackups --remote staging
# heroku addons:add pgbackups --remote production
heroku pgbackups:capture --remote production
heroku pgbackups:restore DATABASE `heroku pgbackups:url --remote production` --remote staging
@harley
harley / gist:3101503
Created July 12, 2012 22:30
vim shortcut to convert ruby ruby 1.8 hashrocket to 1.9 colon syntax
nmap _hr :%s/\v:(\w+) \=\>/\1:/g<cr>
nmap _h :s/\v:(\w+) \=\>/\1:/g<cr>
@harley
harley / bits.zsh
Created July 12, 2012 22:27
useful zsh scripts
# Copied and adapted from https://github.com/matschaffer/zsh-matschaffer/blob/master/tools.zsh
# Bundle grep, search for text in bundled gems
bgrep() {
grep -r "$@" $(bundle show --paths)
}
# Find files or directories like $1
# Shortcut for: find <dir> -name *<search>*
flike() {
local search dir
@harley
harley / .vimrc
Created September 28, 2012 09:04
Rnavcommand for backbone files in vim
" Shortcuts for backbone.js via the asset pipeline
" using backbone-on-rails gem and generate with backbone namespace
autocmd User Rails Rnavcommand bmodel app/assets/javascripts/backbone/models -suffix=.js.coffee -glob=*
autocmd User Rails Rnavcommand bcollection app/assets/javascripts/backbone/collection -suffix=.js.coffee -glob=*
autocmd User Rails Rnavcommand brouter app/assets/javascripts/backbone/routers -suffix=.js.coffee -glob=*
autocmd User Rails Rnavcommand btemplate app/assets/backbone/templates -suffix=.jst.hbs -glob=**/*
autocmd User Rails Rnavcommand bview app/assets/javascripts/backbone/views -suffix=.js.coffee -glob=**/*
@harley
harley / vagrant.rb
Last active October 15, 2015 17:27
capistrano 3: deploy to local vagrant
# file: config/deploy/vagrant.rb
# this allows for `cap vagrant deploy`
set :rails_env, :production
set :server_hostname, "192.168.33.10"
server 'vagrant', user: 'vagrant', roles: %w{app web db}, primary: true
set :ssh_options, {
forward_agent: true,
host_name: "127.0.0.1",
port: "2222",
keys: ['.vagrant/machines/default/virtualbox/private_key']
.0.0-p247 :074 > class Q
2.0.0-p247 :075?> def test
2.0.0-p247 :076?> puts "no block. but you can pass a block to me"
2.0.0-p247 :077?> end
2.0.0-p247 :078?> def btest(&block)
2.0.0-p247 :079?> puts "with block"
2.0.0-p247 :080?> block.call
2.0.0-p247 :081?> end
2.0.0-p247 :082?> def ctest
2.0.0-p247 :083?> if block_given?
@harley
harley / vim sort ip addresses
Last active January 2, 2016 13:59
Sort by IP addresses in vim
# source: http://stackoverflow.com/questions/9067559/sorting-ip-addresses-in-vim
:%s/\<\d\d\?\>/0&/g|%&&|sor r/\(\d\{3}\)\%(\.\d\{3}\)\{3}/|%s/\<00\?\ze\d//g