Skip to content

Instantly share code, notes, and snippets.

def insert!(model, attributes={})
attributes = attributes.stringify_keys
begin
record = model.new {|r| r.send(:attributes=, attributes, false) }
def record.callback(*args)
# inhibit all callbacks
end
record.save(false)
rescue ActiveRecord::StatementInvalid
if $!.message =~ /Column '(.+?)' cannot be null/
class Post < ActiveRecord::Base
validates_length_of :title, :minimum => 4
def custom_method
title.scan(/./).join(' ').upcase
end
end
def nil_blank_count(object)
object.attributes.values.select(&:present?).size
end
# Untested
module WithLogger
attr_writer :logger
def logger
@logger ||= Rails.logger
end
end
SomeClass.extend WithLogger
@Roman2K
Roman2K / gist:16852
Created October 15, 2008 03:49
Test helper for painless record creation
# Test helper for painless record creation. Details at:
# http://roman.flucti.com/painless-record-creation-with-activerecord
#
def insert!(model, attributes={})
attributes = attributes.stringify_keys
begin
record = model.new(attributes)
def record.callback(*args)
# inhibit all callbacks
end
#!/usr/bin/env ruby
# Sort entries by content size. Usage:
# sortbysize [<directory>='.']
#
# This script is documented at:
# http://roman.flucti.com/sorting-directories-by-content-size-with-ruby
#
class Entry < Struct.new(:size, :unit, :name)