Skip to content

Instantly share code, notes, and snippets.

View stve's full-sized avatar

Steve Agalloco stve

View GitHub Profile
scopes[:your_composite_scope_name] = proc{ Scope.new(your.combined.scopes.here, {}) }
def self.your_composite_scope_name(*args)
scopes[:your_composite_scope_name].call(self, *args)
end
# If your workers are inactive for a long period of time, they'll lose
# their MySQL connection.
#
# This hack ensures we re-connect whenever a connection is
# lost. Because, really. why not?
#
# Stick this in RAILS_ROOT/config/initializers/connection_fix.rb (or somewhere similar)
#
# From:
# http://coderrr.wordpress.com/2009/01/08/activerecord-threading-issues-and-resolutions/
@stve
stve / gist:262974
Created December 24, 2009 02:30 — forked from ezmobius/gist:262085
require 'rubygems'
require 'redis'
require 'json'
require 'eventmachine'
class RedisLoop
class << self
attr_accessor :queues
def start(opts={}, &blk)
EM.run {
namespace :harmony do
desc "Munges the data"
task :munge => :environment do
docs_with_publish = Item.collection.find({'publish' => {'$exists' => true}}).to_a
puts "Item count: #{Item.count}"
puts "Items with publish key: #{docs_with_publish.size}"
docs_with_publish.each do |hash|
hash.delete('publish')
#!/usr/bin/env ruby
require 'socket'
host = ARGV[0] || 'localhost'
port = ARGV[1] || '6379'
trap(:INT) {
exit
}
@stve
stve / gist:269916
Created January 6, 2010 01:18 — forked from mwhuss/gist:255725
Static rack site on heroku
use Rack::Static, :urls => ["/stylesheets", "/images"], :root => "public"
run lambda { |env| [200, { 'Content-Type' => 'text/html', 'Cache-Control' => 'public, max-age=86400' }, File.open('public/index.html', File::RDONLY)] }
class Array
def to_hash
Hash[*inject([]) { |array, (key, value)| array + [key, value] }]
end
alias :to_h :to_hash
end
class Hash
def select(&block)
super(&block).to_hash
#! /bin/sh
### BEGIN INIT INFO
# Provides: redis-server
# Required-Start: $syslog
# Required-Stop: $syslog
# Should-Start: $local_fs
# Should-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: redis-server - Persistent key-value db
# this script will migrate and reorganize a Paperclip attachments directory to utilize :id_partition
# Quickly put together by mario@orbitalvoice.com
# Assumes that your images to migrate < 1 000 000
#
# Usage: ruby paperclip_partition_id_migrate.rb TARGET_DIR=/path/to/attachments/:class/:style
require '/opt/ruby-enterprise/lib/ruby/1.8/fileutils.rb'
def add_leading_zeros(i)
# app/controllers/posts_controller.rb
class PostsController < ApplicationController
def index
@posts.all_cached # Retrive only at once by every 5 minutes
expires_in 5.minutes, :private => false, :public => true
end
end
# app/models/post.rb
Class Post