Skip to content

Instantly share code, notes, and snippets.

View wuputah's full-sized avatar
🐘

Jonathan Dance (JD) wuputah

🐘
View GitHub Profile
# e is for efficient. There's not really a way to eager load associations yourself
# without AR thinking you're trying to assign or create something.
attr_accessor :echildren
def self.find_all_efficiently
categories = find(:all, :order => 'position ASC, name ASC').inject(Hash.new) do |h, c|
h[c.parent_id] ||= []
h[c.parent_id] << c
h
end
@wuputah
wuputah / s3-copy-bucket.rb
Created September 29, 2008 04:25
Copies contents of S3 bucket
#!/usr/bin/ruby
require 'rubygems'
require 'right_aws'
access_key = 'abc'
secret = 'xyz'
src = 'src'
dest = 'dest'
@wuputah
wuputah / grant-public-read-to-bucket.rb
Created September 29, 2008 22:34
Grants public read to an S3 bucket
#!/usr/bin/ruby
require 'rubygems'
require 'aws/s3'
include AWS::S3
AWS::S3::Base.establish_connection!(
:access_key_id => 'abc',
:secret_access_key => '123'
@wuputah
wuputah / has_fields.rb
Created October 1, 2008 01:12
has_fields plugin
module HasFields
def has_any?(*fields)
fields.each { |f| return true unless self.send(f).blank? }
false
end
def has_all?(*fields)
fields.each { |f| return false if self.send(f).blank? }
true
end
#!/usr/bin/env ruby
`find /var/www/vhosts/santosdumont.com/web_users/ -type f`.split("\n").each do |file|
basename = File.basename(file)
system("mv", file, "/var/www/vhosts/santosdumont.com/httpdocs/Resource/" + basename)
end
#!/usr/bin/env ruby
require 'rubygems'
require 'mysql'
from_database = 'something_production'
to_database = 'something_development'
connection = Mysql.connect('localhost', 'username', 'password', from_database)
connection.query_with_result = false
#!/usr/bin/env ruby
require 'rubygems'
require 'mysql'
require 'highline'
require 'optparse'
class CopyDatabase
def initialize(options = {})
# Challenge:
# change foo(bar) into bar.foo
#
# Why this is hard:
# method foo is a method of self, which we wouldnt
# have access to from within any method we create
class Object
named_scope :has_commissions, lambda { |amount| { :conditions => ["id IN (SELECT user_id FROM (SELECT user_id, SUM(amount) AS amount FROM commissions GROUP BY user_id HAVING amount > ?) AS a)", amount] } }
#BEGIN CONFIG INFO
#DESCR: 4GB RAM, InnoDB only, ACID, few connections, heavy queries
#TYPE: SYSTEM
#END CONFIG INFO
#
# This is a MySQL example config file for systems with 4GB of memory
# running mostly MySQL using InnoDB only tables and performing complex
# queries with few connections.
#