Skip to content

Instantly share code, notes, and snippets.

@ddollar
ddollar / test.pl
Created July 23, 2008 15:07 — forked from maio/test.pl
use strict;
print "Hello World!\n";
desc "Migrate all of the databases"
task :migrate_all => :environment do
Customer.connect_each do |customer|
puts "Migrating #{customer.environment}"
ActiveRecord::Migrator.migrate "db/migrate/"
end
end
# :PUBLISHER: markdown, shell, { command: 'rdiscount' }
# :BRACKET_CODE: '[ruby]', '[/ruby]'
# :TEXT:
#
# Have you ever started a long operation and walked away from the computer, and
# come back half an hour later only to find that the process is hung up waiting
# for some user input? It's a sub-optimal user experience, and in many cases it
# can be avoided by having the program choose a default if the user doesn't
# respond within a certain amount of time. One example of this UI technique in
# the wild is powering off your computer - most modern operating systems will
@ddollar
ddollar / template.rb
Created July 20, 2009 15:51
Rails Template
# Rails Template
require 'open-uri'
def download(from, to = from.split("/").last)
file to, open(from).read
rescue
puts "Can't get #{from} - Internet down?"
exit!
end
# truncates html text to the desired text length.
# Like the Rails _truncate_ helper but doesn't break HTML tags or entities.
#
# accepts options:
# :link_to => url
def truncate_html(text, max_length = 400, options = {})
elipsis = '…'
tag_delimiter_count = 0
in_html_entity = false
character_count = 0
named_scope :matches, lambda { |value| {
:select => sanitize_sql(["addresses.*, match(value) against(? in boolean mode) as relevancy", wildcardize(value)]),
:conditions => ["match(value) against(? in boolean mode)", wildcardize(value)],
@ddollar
ddollar / hax.rb
Created October 26, 2009 19:50 — forked from wycats/hax.rb
Bundler Preinitializer
require "#{File.dirname(__FILE__)}/../vendor/bundler_gems/environment"
class Rails::Boot
def run
load_initializer
extend_environment
Rails::Initializer.run(:set_load_path)
end
def extend_environment
@ddollar
ddollar / gist:227359
Created November 5, 2009 20:43 — forked from atmos/gist:227332
require File.dirname(__FILE__) + '/spec_helper'
describe "The library itself" do
Spec::Matchers.define :have_no_tab_characters do
match do |filename|
@failing_lines = []
File.readlines(filename).each_with_index do |line,number|
@failing_lines << number + 1 if line =~ /\t/
end
@failing_lines.empty?
@ddollar
ddollar / snippet.rb
Created November 12, 2009 23:54 — forked from anonymous/snippet.rb
require 'rubygems'
require 'rack'
require 'ruby-debug'
class Rack::Nest
attr_reader :app
def initialize(app, options={}, &block)
@path = options[:path]
# Person has_many cards has_many transaction_feeds.
# I would like to find all of the people with at least one transaction feed with the is_ach bool set to true:
SELECT DISTINCT(people.id) AS person_id FROM `people`
INNER JOIN `cards` ON cards.person_id = people.id
LEFT JOIN `transaction_feeds` ON transaction_feeds.card_id = cards.id
WHERE (transaction_feeds.is_ach = 1)
GROUP BY person_id
HAVING COUNT(transaction_feeds.id) = 0;
# This works.