Skip to content

Instantly share code, notes, and snippets.

View dbrady's full-sized avatar

David Brady dbrady

View GitHub Profile
@dbrady
dbrady / snip.sh
Created May 21, 2011 18:44
Rails 3.1 edge nonworky?
# We did a bundle install, and rails seems to install, but weirdness then happens...
# Note that it is in the path, but doesn't run, and gem list doesn't show it as installed.
#
# Halp?
12:40:13 dbrady@shinything:~/shiny_systems/1on1/ian ruby-1.9.2-p180@ian
∫ which rails
/Users/dbrady/.rvm/gems/ruby-1.9.2-p180@ian/bin/rails
12:40:36 dbrady@shinything:~/shiny_systems/1on1/ian ruby-1.9.2-p180@ian
@dbrady
dbrady / reports_controller.rb
Created May 18, 2011 04:05
ReportsController - Rails 1.2, circa 2008
class ReportsController < ApplicationController
layout 'home'
def last_login
if current_user.can_view_login_report?
@execs = Acl.find(:all, :include => :user, :conditions => ["access_type='Airport' AND access_id=#{current_airport_id}"]).map { |a| a.user }
operators = Operator.find_all_by_airport_id(current_airport[:id])
@opers = Acl.find(:all, :include => :user, :conditions => ["access_type='Operator' AND access_id IN (?)", operators]).map { |a| a.user }
stores = Store.find_all_by_airport_id(current_airport[:id])
@vendors = Acl.find(:all, :include => :user, :conditions => ["access_type='Store' AND access_id IN (?)", stores]).map { |a| a.user }
@users = @execs + @opers + @vendors
@dbrady
dbrady / Gemfile
Created May 17, 2011 17:55
Crashy Gemfile
source :rubygems
gem "rails", "2.2.2"
gem "rspec-rails", "1.1.12"
gem "rspec", "1.1.12"
gem "rspec-rails", "1.1.12"
gem "trollop", "1.12"
gem "utility_belt", "1.1.0"
gem "will_paginate", "2.3.14"
gem "wirble", "0.1.3"
@dbrady
dbrady / tourbus.bash
Created May 4, 2011 21:40
tourbus in 3 steps
# Crush your website in 3 easy steps with TourBus!
# 1. get tourbus. Latest isn't in rubygems yet.
# If you don't have bundler, make sure you have the reqs.
git clone https://github.com/dbrady/tourbus.git &&
cd tourbus &&
bundle install &&
gem build tourbus.gemspec &&
gem install tourbus-2.0.0.gem
@dbrady
dbrady / about_scoring_projects.rb
Created April 13, 2011 23:36
Dice Scoring in a single expression
def score(dice)
(1..6).map {|i| (dice.count(i)/3) * i * 100 }.inject {|a,b| a+b} + (dice.count(1)/3) * 900 + (dice.count(1)%3)*100 + (dice.count(5)%3)*50
end
# Wait, what? Back that shit up a minute.
#
# Reconsider the rules of Greed as follows:
# 1. Score each triple of value k as k*100. This gives a correct score for all triples EXCEPT 1's--we're 900 points short.
# 2. Add in a 900-point bonus for each triple of 1's
# 3. Add 100 points for each 1 NOT in a triple
@dbrady
dbrady / backup_dir.el
Created April 4, 2011 21:01
Potty-train emacs to stop littering the fs tree with ~ files
;; Do civilized backup names. Added by dbrady 2003-03-07, taken from
;; http://emacswiki.wikiwikiweb.de/cgi-bin/wiki.pl?BackupDirectory
;;
;; Don't forget to mkdir ~/saves
(setq
backup-by-copying t ; don't clobber symlinks
backup-directory-alist
'(("." . "~/saves")) ; don't litter my fs tree
delete-old-versions t
kept-new-versions 6
@dbrady
dbrady / ps1.bash
Created March 22, 2011 20:39
Dave Brady's PS1
# ALARM: production/master RED: stage YELLOW: next CYAN: everybody else
export PS1="\D{%H:%M:%S} \[\033[37m\]\u@\h\[\033[32m\]:\w \$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\\033[36m(\1)/' | sed -e 's/(production)/\\033[1;37m\\033[41m(production)\\033[m/' | sed -e 's/(master)/\\033[1;37m\\033[41m(master)\\033[m/' | sed -e 's/(stage)/\\033[31m(stage)/' | sed -e 's/(next)/\\033[33m(next)/')\[\033[37m\]\[\033[0m\] \n∫ "
@dbrady
dbrady / node_js_prepended_comma.js
Created March 3, 2011 18:16
Example of how the Node.js community prepends commas
// Consider the humble object:
var dave = {
name: "Dave",
age: 39,
color: "green"
};
// If I add to this and forget to add another trailing comma, it's a
// syntax error:
var dave = {
@dbrady
dbrady / change_case.el
Created February 28, 2011 01:02
emacs lisp defuns to change the case (camelCase <-> snake_case) of the word under cursor, or an entire region if selected. NOTE: the snake-case defuns do not work, they just camelCase. Ooops.
;; Grateful thanks are given to Brian Marick (@marick) for helping me
;; write these. I got the original idea while reading through
;; http://xahlee.org/emacs/elisp_idioms.html, but couldn't work out
;; the elisp regexes. Brian Marick (@marick) stepped in and helped. He
;; took my tortured and broken camelcase-region and turned it into
;; something that actually worked. I then created
;; camelcase-word-or-region. I then wrote the snakecase versions but I
;; see now that all I did was copy the camelcase defuns over and,
;; meaning to go back and finish them Real Soon Now, forgot all about
;; them. I've had a quick look (2011-02-27) but elisp regexes are
@dbrady
dbrady / hanging_if_expression.rb
Created February 26, 2011 04:48
Semantically identical code, three idiomatic ways of expressing it
# Coders from from C often like this style:
log status == OK ? "OK" : "PROBLEM"
log "All done."
# But since if returns a value in ruby, you can spell it out:
log if status == OK
"OK"
else
"PROBLEM"
end