Skip to content

Instantly share code, notes, and snippets.

View jqr's full-sized avatar

Elijah Miller jqr

View GitHub Profile
def render_optional_error_file(status)
begin
code = ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE[status]
render :template => "/statics/#{code}"
rescue
super
end
end
# An array of String subclasses does not sort using <=> on the subclass, WTF?
class StringSubclass < String
def <=>(other)
puts "using <=> in #{self.class}: #{caller.first}"
super
end
end
StringSubclass.new <=> StringSubclass.new
Benchmark = function(description, timed_function) {
var start = new Date()
timed_function();
console.debug(description + ': ' + (new Date() - start) + 'ms');
}
before "deploy:symlink", "deploy:check_app_dependencies"
namespace :deploy do
desc "Checks for required dependencies by starting the application"
task :check_app_dependencies, :roles => :app do
app_env = fetch(:rails_env, "production")
run "cd #{release_path}; RAILS_ENV=#{app_env} ./script/runner ''"
# TODO: anything required for rollback?
end
end
# Preloads models in a subdirectory of RAILS_ROOT/app/models so STI can find
# them properly.
Dir.glob(File.join(RAILS_ROOT, 'app', 'models', '**/*.rb')) do |file|
require file
end
class Hash
# Flattens a hash by joining keys with joiner. Meant for use with String or
# Symbol keys. Resulting hash keys take on the type of the parent key.
#
# { 'a' => { 'b' => 1 } }.flatten_with_joined_keys
# # => {"a_b"=>1}
# { :a => { :b => { :c => { :d => 1 } } } }.flatten_with_joined_keys('')
# # => {:abcd=>1}
def flatten_with_joined_keys(joiner = '_')
output = {}
A friend started donating to each open source project he uses on client
work[1], and I’m trying to follow the same practice. Except you don’t
seem to have pledgie enabled on any of your projects, gah! To each his
own, but please let me know if you enable pledgie.
[1] http://opensoul.org/2009/1/9/give-back-to-the-community
class UserNotifier < ActionMailer::Base
def self.without_deliveries
initial = self.perform_deliveries
self.perform_deliveries = false
yield
self.perform_deliveries = initial
end
end
def equalize_lists(one, two, empty_list_item = '<li>&nbsp;</li>')
difference = one.size - two.size
if difference == 0
[one, two]
elsif difference > 0
[one, two + [empty_list_item] * difference]
elsif difference < 0
[one + [empty_list_item] * difference.abs, two]
end
end
# Spawned from this thread: http://rubyflow.com/items/1990
module Enumerable
# Wraps an enumerable in an easy interface for selecting subsets. Comparison
# and method calls are supported.
#
# [0, 1, 2, 3].filter > 1 # => [2, 3]
# [0, 1, 2, 3].filter.zero? # => [0]
#
# Warning: Do not use this with the != operator, use filter_not instead.