Skip to content

Instantly share code, notes, and snippets.

@boone
boone / strip_nil_from_parameters.rb
Created June 1, 2012 18:08
Monkey patch for CVE-2012-2660 and CVE-2012-2694 on Rails 2.3.14
# Monkey patch for CVE-2012-2660 and CVE-2012-2694 on Rails 2.3.14
# put this file in your config/initializers directory
# comments/corrections: https://gist.github.com/2854095
# Strip [nil] from parameters hash
# based on a pull request from @sebbacon
# https://github.com/rails/rails/pull/6580
module ActionController
class Request < Rack::Request
@boone
boone / hash_copy.rb
Created June 6, 2011 19:27
Ruby unexpected behavior when using .clone or .dup on a hash (or array)
# Ruby unexpected behavior when using .clone or .dup on a hash (or array)
# create a hash and freeze it so it shouldn't be modified
MY_HASH = { :one => { :first => 'eins', :second => 'zwei' } }.freeze
puts MY_HASH.inspect # {:one=>{:first=>"eins", :second=>"zwei"}}
new_hash = MY_HASH.dup # copy the hash, unfrozen
new_hash[:one][:second] = 'dos'
@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