This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Post < ActiveRecord::Base | |
validates_length_of :title, :minimum => 4 | |
def custom_method | |
title.scan(/./).join(' ').upcase | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def nil_blank_count(object) | |
object.attributes.values.select(&:present?).size | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Untested | |
module WithLogger | |
attr_writer :logger | |
def logger | |
@logger ||= Rails.logger | |
end | |
end | |
SomeClass.extend WithLogger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
NewerOlder