Skip to content

Instantly share code, notes, and snippets.


Adam Cooper adamcooper

View GitHub Profile
View gist:30580
class User < ActiveRecord::Base
has_many :cars
class Car < ActiveRecord::Base
belongs_to :user, :counter_cache => true
View Paperclip and VestalVersions
This is a brief description of how to get VestalVersions and Paperclip to play nicely.
The paperclip_initializer.rb extends paperclip for a new option :keep_old_files which means that paperclip won't remove the file when a new file is uploaded. In order for this to work, the model also needs to have to have specific settings:
#1. The version needs to be included in the file path
#2. A method to get the actual path to the file. (The file may not be at the current version)
You can view both these settings in the model_files.rb file below.
Note: If you dealing with public files then you will need to include the :id_and_version option in the :url and :path. Then in the view you need to reference the model_file.versioned_file_path.
adamcooper / resque.rb
Created Nov 8, 2010
Process multi-jobs per fork of resque
View resque.rb
Resque.after_fork do |job|
jobs_performed = 0
kill_fork_at = + (ENV['MINUTES_PER_FORK'].to_i * 60)
worker = job.worker
first_job = job
worker.procline "Processing #{job.queue} since #{} until #{kill_fork_at}"
# rely on parent error handling
View development.smtp.example.yml
:domain: ""
:perform_deliveries: true
:address: ''
:authentication: :plain
:enable_starttls_auto: true
View 20110811124516_migrate_product_type_category_groups.rb
ProductType.all.each do |product_type|
# Check if product type has banyan category group
product_type_group = product_type.category_group
# Create banyan category group for product type if not exist
if product_type_group.nil?
product_type_group = => + " Category Group")
product_type_group.group_categorizable = product_type!
View 0001-fixing-the-bundle-install-issue.patch
From 003273df39c7e65b27197930064c77f2141969d5 Mon Sep 17 00:00:00 2001
From: Adam Cooper <>
Date: Sun, 16 Oct 2011 18:21:15 -0700
Subject: [PATCH] fixing the bundle install issue
hubruby.gemspec | 2 +-
lib/hubruby.rb | 2 +-
lib/version.rb | 3 +++
3 files changed, 5 insertions(+), 2 deletions(-)
View test.rb
class Redis
attr_accessor :server
class RedisNamespace
attr_accessor :redis
def initialize(namespace, options = {})
self.redis = options[:redis] || :internal
adamcooper / gist:5315262
Created Apr 4, 2013
Don't use "and" in ruby.
View gist:5315262
>> [1,2,3, 4].each {|x| puts x; break if x == 1}
=> nil
>> [1,2,3, 4].each {|x| puts x; puts 'broken' and break if x == 1}
View input.rb
gem 'rails', '3.2.13' # change as required
require 'active_record'
# Print out what version we're running
puts "Active Record #{ActiveRecord::VERSION::STRING}"
# Connect to an in-memory sqlite3 database (more on this in a moment)
:adapter => 'sqlite3',
:database => ':memory:'
View course.rb
class Course
attr_accessor :title, :location
def initialize(params = {}); end
def add_teacher(teacher); end
def add_student(student); end
def for_each_student; end
attr_accessor :teachers, :students