Skip to content

Instantly share code, notes, and snippets.

@boone
boone / gist:802504
Created January 30, 2011 03:49
validation method dealing with _destroy attribute from accepts_nested_attributes_for
class Project < ActiveRecord::Base
has_many :tasks
accepts_nested_attributes_for :tasks, :reject_if => :all_blank, :allow_destroy => true
def validate
# require a minimum of one task
undestroyed_task_count = 0
tasks.each { |t| undestroyed_task_count += 1 unless t.marked_for_destruction? }
# monkey patch to allow authlogic's *_credentials cookies set the HttpOnly bit
# put this file in config/initializers/authlogic.rb and set the value after
# instantiating your session model, e.g.
# @user_session = UserSession.new(params[:user_session])
# @user_session.httponly = true
module Authlogic
module Session
module Cookies
module Config
@boone
boone / should_have_file_column.rb
Created August 17, 2009 14:14
Shoulda macro for file_column plugin
# file_column Shoulda macro, based off the Paperclip method given here:
# http://giantrobots.thoughtbot.com/2008/3/18/for-attaching-files-use-paperclip#comment--614050918
# pass the extra option :magick => true to test the extra method for the columns that use RMagick
def self.should_have_file_column(attachment, options = {})
klass = described_type
fields = ["#{attachment}", "#{attachment}=", "#{attachment}_temp", "#{attachment}_temp=",
"#{attachment}_dir", "#{attachment}_just_uploaded?", "#{attachment}_options",
"#{attachment}_relative_dir", "#{attachment}_relative_path"]
fields << "#{attachment}_magick_after_assign" if options[:magick]
should "have_file_column #{attachment}" do
@boone
boone / find_num.rb
Created December 7, 2008 16:24
Using grep from Ruby
# original slow method to find a number plus a divider (|)
# at the start of a line
def find_num(file, num)
found = false
File.read(file).each do |line|
if line.chomp.split('|', -1)[0] == num
found = true
break
end
end
# truncate string by byte limit
#
# str - String
# limit - Maximum number of bytes
#
# Returns a String
def limit_string(str, limit)
limited_string = ""
str.each_char do |c|

Keybase proof

I hereby claim:

  • I am boone on github.
  • I am boone (https://keybase.io/boone) on keybase.
  • I have a public key whose fingerprint is 7713 6FEA DE34 477B 241F E9EB 9D8A 85BC B79C C67E

To claim this, I am signing this object: