View gist:6804
# 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
View s3-copy-bucket.rb
#!/usr/bin/ruby
require 'rubygems'
require 'right_aws'
access_key = 'abc'
secret = 'xyz'
src = 'src'
dest = 'dest'
View grant-public-read-to-bucket.rb
#!/usr/bin/ruby
require 'rubygems'
require 'aws/s3'
include AWS::S3
AWS::S3::Base.establish_connection!(
:access_key_id => 'abc',
:secret_access_key => '123'
View has_fields.rb
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
View gist:15387
#!/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
View mysql-copydb.rb
#!/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
View mysql-copydb.rb
#!/usr/bin/env ruby
require 'rubygems'
require 'mysql'
require 'highline'
require 'optparse'
class CopyDatabase
def initialize(options = {})
View object-invoke.rb
# 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
View awful-named-scope.rb
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] } }
View gist:34948
#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.
#