Skip to content

Instantly share code, notes, and snippets.

@vargonaut
vargonaut / gist:1277150
Created October 11, 2011 02:50
Create a presenter object on the fly in Rails
# Inspired by Ryan Bates pro RailsCast "Presenters from Scratch"
# This is a proof of concept for 'automagically' creating presenter objects
#
# This assumes you have presenter objects that follow the convention
# Object => ObjectPresenter
#
# It intercepts the 'render' method in the controller, and does some
# introspection to see if there is a presenter defined for the class
# of the instance variable being looked at. If so, it makes a presenter
# and adds it as an instance variable
@vargonaut
vargonaut / be_allowed_to_match.rb
Created May 10, 2011 04:41
Custom Matcher for Declarative Authorization authorization specs with RSpec
# in spec/spec_helper.rb, add this to make it work:
# require 'support/be_allowed_to'
# RSpec.configure |config|
# config.include(BeAllowedToMatcher)
# end
#
# allows: some_user.should be_allowed_to(:edit, thing)
# some_user.should be_allowed_to(:edit, :things)
# some_user.should be_allowed_to(:edit, thing, in_this_context)
# along with the should_not variants
@vargonaut
vargonaut / grep_routes.sh
Created April 20, 2011 21:25
grep rails rake routes for strings via command line function
function grep_routes() {
ruby -e "ARGV[0].split(10.chr).each{|x| puts x if !ARGV[1..-1].map{|a| x =~ /#{a}/}.include?(nil)}" "$(rake routes)" "$@"
}
# USAGE
# > grep_routes this
# > grep_routes this that the other
@vargonaut
vargonaut / emacs_tidbits.el
Created April 14, 2011 13:54
Little emacs setting to share with co-workers
;; keep a single instance of Emacs I can send crap to
;; allows me to do things like this in my bash profile
;; export GIT_EDITOR=/Applications/Emacs.app/Contents/MacOS/bin/emacsclient
;; send files to emacs from the command line as 'emacsclient <filename>'
(server-start)
;; 2 spaces instead of 4 for javascript
(setq js-indent-level 2)
;; turn on linum for mode xxx
@vargonaut
vargonaut / console
Created February 26, 2011 21:13
Homemade IRB Console, like Rails!
#!/usr/bin/env ruby
# console, generally inspired by rails console
#
# put in root of project, adjust file paths and the like
# chmod the file to be executable by you
# run:
# project_root >> ./console
@vargonaut
vargonaut / gist:703400
Created November 17, 2010 13:57
Pretty git-ified prompt in bash
# ~/.bash_profile
# give prompt as ~/foo(branch_name*)
# where the * indicates uncommitted work
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1='\[\033[36;40m\]\w\[\033[0;33m\]$(parse_git_branch)\[\e[0m\]$ '
require 'pp'
require 'irb/completion'
IRB.conf[:AUTO_INDENT]=true
require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"
# uses xpath to check for a form element based on the label,
# instead of using a css selector w/ label_for
# name should be the complete name in the html page.
# meaning Title: and not Title
# find the label that is name. Get the "for" of the label
# uses the for to check for the input with that as an id
Then /^I should see a "([^\"]*)" for "([^\"]*)"$/ do |type, name|
case type