Skip to content

Instantly share code, notes, and snippets.

@mislav
mislav / Gemfile.rb
Created February 22, 2010 20:54
Bundler setup for Rails 2.3.x apps
## example Gemfile
source :rubygems
group :rails do
gem 'rake', '< 0.9', :require => nil
gem 'rails', '~> 2.3.5', :require => nil
gem 'builder', '~> 2.1.2'
gem 'memcache-client', '>= 1.7.4', :require => nil
gem 'tzinfo', '~> 0.3.12'
gem 'i18n', '>= 0.1.3'
@agibralter
agibralter / culerity-hooks.rb
Created April 2, 2010 18:31
Cucumber hooks for culerity + rvm
Before("@culerity,@celerity,@javascript") do |scenario|
unless @env_rvm_jruby
@env_rvm_jruby = {}
require 'yaml'
rvm_info = YAML::load(`bash -l -c 'source ~/.rvm/scripts/rvm; rvm jruby ; rvm info'`).values.first
rvm_info['environment'].each do |k, v|
@env_rvm_jruby[k] = v
end
@env_jruby_path = rvm_info['binaries']['ruby'].gsub(%r{^(.*)/ruby$}, '\1')
end
@jdhollis
jdhollis / resque_async_handling.rb
Created June 9, 2010 21:43
handle_asynchronously for Resque
module Resque
module AsyncHandling
# To disable (in config.after_initialize):
# Resque::AsyncHandling.enabled = false
mattr_accessor :enabled
self.enabled = true
def handle_asynchronously(original_method, params = { })
if enabled
now_method = "#{ original_method }_now"
# Copy this to features/support/ssl_fix.rb to make Capybara work with sites that switch between HTTP and HTTPS
module Capybara::Driver::RackTest::SslFix
[:get, :post, :put, :delete].each do |method|
define_method method do |*args|
args[0] = path_to_ssl_aware_url(args[0])
super(*args)
end
end
# In your test_helper.rb
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || retrieve_connection
end
end
@paulirish
paulirish / gist:526168
Created August 16, 2010 00:42 — forked from anonymous/>
<!DOCTYPE html>
<!-- Helpful things to keep in your <head/>
// Brian Blakely, 360i
// http://twitter.com/brianblakely/
-->
<head>
<!-- According to Heather Champ, former community manager at flickr,
you should not allow search engines to index your "Contact Us"
@mislav
mislav / application_with_config.rb
Created August 24, 2010 17:43
Read custom config from settings.yml into a Rails 3 app
# needs the "hashie" gem in Gemfile
require 'erb'
module Movies
class Application < Rails::Application
# read from "settings.yml" and optional "settings.local.yml"
settings = ERB.new(IO.read(File.expand_path('../settings.yml', __FILE__))).result
mash = Hashie::Mash.new(YAML::load(settings)[Rails.env.to_s])
@mnutt
mnutt / Instrument Anything in Rails 3.md
Created September 6, 2010 06:50
How to use Rails 3.0's new notification system to inject custom log events

Instrument Anything in Rails 3

With Rails 3.0 released a few weeks ago I've migrated a few apps and I'm constantly finding useful new improvements. One such improvement is the ability to log anything in the same way that Rails internally logs ActiveRecord and ActionView. By default Rails 3 logs look slightly spiffier than those produced by Rails 2.3: (notice the second line has been cleaned up)

Started GET "/" for 127.0.0.1 at Mon Sep 06 01:07:11 -0400 2010
  Processing by HomeController#index as HTML
  User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1
  CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1

Rendered layouts/_nav.html.erb (363.4ms)

// 1: how could you rewrite the following to make it shorter?
if (foo) {
bar.doSomething(el);
} else {
bar.doSomethingElse(el);
}
@paulirish
paulirish / gist:601751
Created September 28, 2010 20:50
localStorage and sessionStorage should accept objects
// desire: localStorage and sessionStorage should accept objects
// in fact they do according to spec.
// http://code.google.com/p/chromium/issues/detail?id=43666
// but so far that part isnt implemented anywhere.
// so we duckpunch set/getItem() to stringify/parse on the way in/out
// this seems to work in chrome
// but fails in FF (cant redefine setItem as a function, it just tostring's it)