Skip to content

Instantly share code, notes, and snippets.

@teeparham
teeparham / devise.rb
Created February 11, 2011 21:53
Devise initializer - set display from
Devise.setup do |config|
# ==> Mailer Configuration
# Configure the e-mail address which will be shown in DeviseMailer.
config.mailer_sender = '"John Smith" <john@example.com>'
# ...
end
@teeparham
teeparham / aws-ses.rb
Created February 11, 2011 21:57
AWS SES - with display from
gem install aws-ses
# in irb:
require 'aws/ses'
ses = AWS::SES::Base.new :access_key_id => "your-key",
:secret_access_key => "your-secret-key"
ses.addresses.list.result
@teeparham
teeparham / deploy.rb
Created February 15, 2011 01:22
Capistrano task to GET site after deploy
task :ensure_alive do
puts "GET #{find_servers.first}:"
system("curl -silent http://#{find_servers.first} > /dev/null")
end
after "deploy", "ensure_alive"
@teeparham
teeparham / self-signed-ssl-cert.sh
Created March 9, 2011 23:37
create self-signed SSL certificate
openssl genrsa -out yoursite.key 1024
openssl req -new -key yoursite.key -out yoursite.csr
openssl x509 -req -days 2000 -in yoursite.csr -signkey yoursite.key -out yoursite.crt
sudo cp yoursite.crt /etc/ssl/certs/
sudo cp yoursite.key /etc/ssl/private/
# Apache
a2enmod ssl headers
@teeparham
teeparham / development.rb
Created March 17, 2011 17:22
Rails 3 routes with SSL
SSL_PROTOCOL = 'http'
@teeparham
teeparham / .irbrc
Created March 31, 2011 19:14
pimp my irb
require 'rubygems'
require 'wirble'
require 'hirb'
begin
IRB::Irb.class_eval do
def output_value
ap @context.last_value
end
end
@teeparham
teeparham / user.rb
Created April 14, 2011 16:39
User with paperclip attachment using proc for default_url
has_attached_file :avatar,
:styles => { :medium => "100x100#", :thumb => "45x45#" }
:default_url => lambda { |a| "/images/avatar_:style_#{a.instance.default_image_number}.png" }
def default_image_number
id.to_s.last
end
# this example allows you to have 10 random default avatars:
# avatar_thumb_0.png, avatar_thumb_1.png, etc.
@teeparham
teeparham / to_time
Created May 5, 2011 23:22
activesupport to_time
Loading development environment (Rails 3.0.7)
ruby-1.9.2-p180 :001 > s = Time.now.to_s
=> "2011-05-05 17:11:19 -0600"
ruby-1.9.2-p180 :002 > s.to_time
=> 2011-05-05 17:11:19 UTC <----- wrong
ruby-1.9.2-p180 :003 > s.to_time :local
=> 2011-05-05 17:11:19 -0600 <----- better, sort of
ruby-1.9.2-p180 :004 > Time.parse s
=> 2011-05-05 17:11:19 -0600 <----- plain ruby wins
@teeparham
teeparham / active_record_caching.rb
Created May 27, 2011 04:56 — forked from ahoward/active_record_caching.rb
simple active record caching
# for some reason active record doesn't include a simple way to serialize to
# cache. this simple trick will take you far
#
# usage:
#
# user = User.find(id)
#
# key = user.to_cache
#
# user = User.from_cache(key)
@teeparham
teeparham / rails_cash.rb
Created June 3, 2011 03:45
Rails object caching helpers for ActiveRecord & ActionController
module Rails
class << self
# retrieve keyed item from cache
# store block results if not in cache
#
# usage:
# cash('user-first-10') { User.limit(10) }
def cash(key, options=nil)
object = cache.read(key)