Skip to content

Instantly share code, notes, and snippets.

# http://en.wikibooks.org/wiki/SQL_dialects_reference/Functions_and_expressions/Math_functions/Numeric_functions
def rand_function
@rand_function ||= (
if [ "SQLite", "PostgreSQL" ].include? ActiveRecord::Base.connection.adapter_name then
"random"
else
"rand"
end
)
end
#!/usr/bin/env ruby
##
# alert_me
#
# A simple delay timer to send a growl notification at specific time with a
# message. Useful for when you need to check on something later
#
# echo "Go look at system 42" | alert_me in 2 hours
#
# Returns a copy of the attributes hash that only includes those
# attributes that have values.
def attributes_with_values
returning Hash.new do |with_values|
@attributes.each_pair do |name, value|
if value⋅
with_values[name] = value
else
column = column_for_attribute(name)
if column.has_default? and !column.null then
@copiousfreetime
copiousfreetime / gist:59067
Created February 5, 2009 22:34
Postgres Partitioning with RETURNING on insert
-- A method to have RETURNING work if you are partitioning data using trigger.
-- The method to this madness is:
--
-- 1) Use the normal trigger mechanism to insert the data into the child tables, but
-- Instead of the trigger function returning NULL so that the row does not get⋅
-- inserted into the master table, it returns the row inserted into the child
-- table
--
-- 2) Postgres will insert the new row from the trigger into the master table
--
require 'rubygems'
require 'activerecord'
class ActiveRecord::Base
alias_method '__initialize__', 'initialize'
def initialize options = nil, &block
returning( __initialize__(options, &block) ) do
options ||= {}
@copiousfreetime
copiousfreetime / spec_log.rb
Created February 16, 2009 17:02
capture per test logging with logging and rspec
require 'rubygems'
require 'spec'
require 'stringio'
require 'logging'
Logging::Logger['root'].level = :all
module Spec
module Log
def self.io
@io ||= StringIO.new
#
# This exists as a way to make active record use SQL literals in attributes when
# creating or updating records
#
module ActiveRecord
module ConnectionAdapters
class Column
class Literal < ::String
def quoted_id() self end
end
# Overwrite the quote column name since it quotes everything, but pk can return
# a multiple columname for a pk, and when that is quoted it does not match any
# column.
if ActiveRecord::Base.connection.adapter_name == "PostgreSQL" then
module ActiveRecord
module ConnectionAdapters
class PostgreSQLAdapter
if instance_methods.include?('quote_column_name') then
unless instance_methods.include?( 'orig_quote_column_name' )
@copiousfreetime
copiousfreetime / gist.rb
Created February 16, 2009 22:26
Altered gist.rb to take the filename on the command line
#!/usr/bin/env ruby
=begin
INSTALL:
curl http://github.com/defunkt/gist/raw/master/gist.rb > gist &&
chmod 755 gist &&
sudo mv gist /usr/local/bin/gist
# Please install the Engine Yard Capistrano gem
# gem install eycap --source http://gems.engineyard.com
require "eycap/recipes"
set :keep_releases, 5
set :application, 'sinatra'
set :repository, 'git://github.com/copiousfreetime/integrity.git'
set :user, 'jeremy'
set :password, 'VBGYjMiZ5A'
set :deploy_to, "/data/#{application}"