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 / activeadmin-cancan.rb
Created July 6, 2012 02:30
Deep integration of CanCan into ActiveAdmin
# blog post:
module ActiveAdmin
module ViewHelpers
# lib/active_admin/view_helpers/auto_link_helper.rb
def auto_link(resource, link_content = nil)
content = link_content || display_name(resource)
if can?(:read, resource) && registration = active_admin_resource_for(resource.class)
begin
@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 / 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 / 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 / rake_benchmark.rb
Created July 14, 2012 22:36
Benchmarking a rake task
# Put this in Rakefile (doesn't matter where)
require 'benchmark'
class Rake::Task
def execute_with_benchmark(*args)
bm = Benchmark.measure { execute_without_benchmark(*args) }
puts " #{name} --> #{bm}"
end
alias_method :execute_without_benchmark, :execute
@harley
harley / gist:3118186
Created July 15, 2012 19:07
remove old branches that have been merged
# adapted from http://devblog.springest.com/a-script-to-remove-old-git-branches
# This has to be run from master
git checkout master
# Update our list of remotes
git fetch
git remote prune origin
# Remove local fully merged branches
@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=**/*