Skip to content

Instantly share code, notes, and snippets.

@ches
ches / knife
Created May 7, 2011 20:15
bash completion for Chef's knife command (not yet updated for 0.10)
# vim: ft=sh:ts=4:sw=4:autoindent:
# Author: Avishai Ish-Shalom <avishai@fewbytes.com>
# We need to specify GNU sed for OS X, BSDs, etc.
if [[ "$(uname -s)" == "Darwin" ]]; then
SED=gsed
else
SED=sed
fi
@ches
ches / gist:1054215
Created June 29, 2011 16:19
PySide homebrew formula build failure
[ches@lupin]$ brew install pyside
Also installing dependencies: qt, apiextractor, generatorrunner, shiboken
==> Downloading https://downloads.sourceforge.net/project/machomebrew/Bottles/qt-4.7.3-bottle.tar.gz
######################################################################## 100.0%
==> Pouring qt-4.7.3.tar.gz
==> Caveats
We agreed to the Qt opensource license for you.
If this is unacceptable you should uninstall.
==> Summary
/usr/local/Cellar/qt/4.7.3: 2561 files, 292M
@ches
ches / install-pygtk.sh
Created July 20, 2011 11:34
Install PyGTK via Homebrew and virtualenv
# This LOOKS pretty straightforward, but it took awhile to sort out issues with
# py2cairo and pygobject, so I hope I've saved you some time :-)
#
# This assumes you already subscribe to a nice clean virtualenvwrapper workflow
# -- see https://gist.github.com/771394 if you need advice on getting there.
# There are some optional dependencies omitted, so if you're going to be doing
# heavy development with these libs, you may want to look into them.
#
# We go to some configure option pains to avoid polluting the system-level
# Python, and `brew link`ing Cairo which is keg-only by default.
@ches
ches / assets.rake
Created August 14, 2011 07:29
Reusing Guard tasks via Rake
# There are a couple minor quirks (see https://github.com/guard/guard/issues/118),
# but mostly this is delightfully straightforward:
require 'guard'
namespace :assets do
desc 'Generate CSS from all source LESS files'
task :less do
Guard.setup
Guard::Dsl.evaluate_guardfile(:guardfile => 'Guardfile', :group => ['frontend'])
@ches
ches / tags_input.rb
Created August 26, 2011 08:09
SimpleForm (1.4.x) input for mongoid_taggable -- place in app/inputs
# Input builder to render tags from an Array field, for use with my fork
# of mongoid_taggable:
#
# https://github.com/ches/mongoid_taggable
#
class TagsInput < SimpleForm::Inputs::StringInput
map_type :tags, :to => :text_field
protected
@ches
ches / .pryrc
Created August 29, 2011 06:39
Enable Hirb in Pry, and hack disable/enable to work in-session
begin
require 'hirb'
rescue LoadError
# Missing goodies, bummer
end
if defined? Hirb
# Dirty hack to support in-session Hirb.disable/enable
Hirb::View.instance_eval do
def enable_output_method
@ches
ches / subject_prefixes.rb
Created September 7, 2011 07:27
Hack ActionMailer to always prefix email subjects. Gotta be a better way.
ActionMailer::Base.class_eval do
def mail_with_prefix(headers={}, &block)
unless Rails.env.production?
headers[:subject] = "[#{Rails.env.upcase}] " + (headers[:subject] || '')
end
mail_without_prefix(headers, &block)
end
alias_method_chain :mail, :prefix
end
@ches
ches / javascript_steps.rb
Created September 12, 2011 05:27
Capybara selector/Cucumber step to click any element with given text
# Brandon Keepers' "click on any element with given text" Capybara selector/Cucumber
# step pairing, adapted for Capybara 1.0+.
# http://collectiveidea.com/blog/archives/2010/08/03/clicking-any-element-with-cucumber-and-capybara/
Capybara.add_selector(:element) do
xpath { |locator| "//*[normalize-space(text())=#{XPath::Expression::StringLiteral.new(locator)}]" }
end
When 'I click "$locator"' do |locator|
msg = "No element found with the content of '#{locator}'"
find(:element, locator, :message => msg).click
@ches
ches / clipboard
Created September 15, 2011 06:34
Should ENV be trying File.path somewhere?
ruby-1.9.2-p290 :001 > require 'pathname'
=> true
ruby-1.9.2-p290 :002 > p = Pathname.new('/Users/ches')
=> #<Pathname:/Users/ches>
ruby-1.9.2-p290 :003 > ENV['foobar'] = p + 'src'
TypeError: can't convert Pathname into String
from (irb):3:in `[]='
from (irb):3
from /Users/ches/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'
@ches
ches / migration.rb
Created September 22, 2011 11:23
Migrating Mongoid model to natural primary key
# To run after the `key` declaration is added to the above User model
User.all.each do |user|
nu = user.dup
user.unset :username # clear unique index
nu.save
# Could we re-associate without duping? My attempts resulted in
# `dependent: :destroy` getting invoked when the original user is deleted
nu.notes << user.notes.clone