Skip to content

Instantly share code, notes, and snippets.

@ches
ches / snippet.sh
Created April 4, 2009 02:04
Bash alias to open Github page for project and active branch in current working directory.
# Bash alias to open Github page for project in current working directory.
# Will use the current active branch if it exists remotely, or falls back to master.
alias github='br=$(git branch --contains HEAD | sed -En "s/^\* //p"); if ! git ls-remote . | grep -q -e "refs/remotes/.*/${br}"; then br="master"; fi; open $(git config -l | sed -En "s%remote.origin.url=git(@|://)(github.com)(:|/)(.+/.+).git%https://\2/\4/tree/${br}%p")'
@ches
ches / lighter.rb
Last active August 30, 2015 11:14 — forked from macournoyer/lighter.rb
A tiny little command-line Campfire client using the Tinder gem.
# Lighter -- Campfire from the command line
# usage: ruby lighter.rb subdomain "Main Room" macournoyer@gmail
require "rubygems"
require "tinder"
require "readline"
require "highline/import"
class Lighter
def initialize(room)
@room = room
@ches
ches / environment.rb
Last active September 3, 2015 19:45 — forked from webmat/environment.rb
A little `config.github_gem` helper for back in the day when Rails used `config.gem`-style dependencies.
# Neater Github gem dependencies for Rails
module GithubGem
def github_gem(gem_name, opts={})
lib_name = gem_name.split('-', 2)[1]
self.gem gem_name, {:lib => lib_name, :source => 'http://gems.github.com'}.merge(opts)
end
end
Rails::Initializer.run do |config|
@ches
ches / expire_sessions.rake
Last active September 3, 2015 23:05
Use mysql to expire sessions without loading the Rails environment, so we can call this task from cron without it being unnecessarily expensive.
usage = <<DESC
Use mysql to expire sessions without loading the Rails environment, so we
can call this task from cron without it being unnecessarily expensive.
Takes path to Rails app and desired RAILS_ENV as arguments.
DESC
namespace :db do
namespace :sessions do
desc usage
@ches
ches / gist:243611
Created November 26, 2009 19:09 — forked from lukesutton/gist:107966
basic example of Warden authentication with Sinatra
Warden::Manager.serialize_into_session{|user| user.id }
Warden::Manager.serialize_from_session{|id| User.get(id) }
Warden::Manager.before_failure do |env,opts|
# Sinatra is very sensitive to the request method
# since authentication could fail on any type of method, we need
# to set it for the failure app so it is routed to the correct block
env['REQUEST_METHOD'] = "POST"
end
@ches
ches / db.rake
Created October 17, 2010 05:45
Rake task to open MongoDB console for a Rails app with Mongoid
namespace :db do
desc 'Open a MongoDB console with connection parameters for the current Rails.env'
task :console => :environment do
conn = Mongoid.master.connection
args = []
args << "--username=#{conn.username}" if conn.username rescue nil
args << "--password=#{conn.password}" if conn.password rescue nil
args << "--host=#{conn.host}"
args << "--port=#{conn.port.to_s}"
args << Mongoid.master.name
@ches
ches / gist:630625
Created October 17, 2010 07:23
.irbrc for logging goodies like SQL/Mongo queries to $stdout if in Rails 3 console
# .irbrc to log goodies like SQL/Mongo queries to $stdout if in Rails 3 console
if defined?(Rails) && Rails.respond_to?(:logger)
require 'logger'
Rails.logger = Logger.new($stdout)
if defined?(Mongoid)
Mongoid.logger = Rails.logger
end
end
@ches
ches / gist:718234
Created November 27, 2010 20:20 — forked from jpemberthy/gist:349242
Very simple taggable behavior for Mongoid
# Basic tagging system for mongoid documents.
# jpemberthy 2010
#
# class User
# include Mongoid::Document
# include Mongoid::Document::Taggable
# end
#
# @user = User.new(:name => "Bobby")
# @user.tag_list = "awesome, slick, hefty"
@ches
ches / spec_suites.rake
Created November 29, 2010 18:08
Set up Rake tasks to execute defined sets of specs
# Nice idea from:
# http://kpumuk.info/ruby-on-rails/my-top-7-rspec-best-practices/
#
# ~/test$ rake -T spec:suite
#
# (in /Users/kpumuk/test)
# rake spec:suite:acl # Run all specs in access control spec suite
# rake spec:suite:amazon # Run all specs in Amazon libraries spec suite
SPEC_SUITES = [
@ches
ches / eruby.snippets
Created December 2, 2010 00:36
UltiSnips snippets derived from converting Ruby on Rails TextMate bundle
# TextMate added these variables to cope with changes in ERB handling
# in different versions of Rails -- for instance, Rails 3 automatically
# strips whitespace so that it's no longer necessary to use a form like
# <% end -%>, but if you're still maintaining Rails 2 projects, you
# can't omit the minus sign and get the same behavior.
#
# The following regex replace substitutes the function below for the
# TextMate variable references after the snippets are converted:
#
# /\v\$\{(TM_RAILS_TEMPLATE_([^_]+)_RUBY_([^_\s]+))\}/`!p textmate_var('\1', snip)`/g