Skip to content

Instantly share code, notes, and snippets.

View ckdake's full-sized avatar

Chris Kelly ckdake

View GitHub Profile
test "getters/setters for shipping methods should work" do
listing = Factory.create(:listing)
assert !listing.shipping_methods.include?('CATBUS')
assert !listing.ship_via_catbus
listing.ship_via_catbus = true
assert listing.ship_via_catbus
assert listing.shipping_methods.include?('CATBUS')
listing.ship_via_catbus = false
assert !listing.ship_via_catbus
end
@ckdake
ckdake / gist:1035814
Created June 20, 2011 15:22
make find_or_create_by methods work right with validations
# This and the next method are unfortunately 'magic'
# They set default paramaters for these two find_or_create_by_ method_missing methods
def self.find_or_create_by_email(params)
params = { email: params } unless params.is_a?(Hash)
super(params.reverse_merge(
password: Digest::MD5.hexdigest("#{rand(1024)}Time.now"),
username: Digest::MD5.hexdigest("#{rand(1024)}Time.now")
))
end
@ckdake
ckdake / Velocity 2011 Notes.textile
Created June 17, 2011 17:52
Velocity 2011 Notes

Velocity 2011

Tuesday

OpenStack workshop – Ron Pedde (Rackspace Hosting), Todd Willey (OpenStack), Matt Ray (Opscode)

@ckdake
ckdake / s3_size.rake
Created June 6, 2011 15:50
rake task to show size of all files in a s3 account
require 'fog'
desc "Tell us how much space is in use on S3"
namespace :s3 do
task usage: :environment do
size = 0
connection = Fog::Storage.new(
provider: 'AWS',
aws_access_key_id: ENV['aws_access_key_id'],
# Unfortunately the Rails 2 version of DJ doesn't support before/after hooks. This hacks those in
# Stick this in an initalizer, and then any before(job) and after(job) methods in your Job classes will get called
module Delayed
module Backend
module ActiveRecord
class Job < ::ActiveRecord::Base
def invoke_job
payload_object.before(self) if payload_object.respond_to?('before')
payload_object.perform
ruby-1.9.2-p0 adella:benchmarks$ ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.5.0]
ruby-1.9.2-p0 adella:benchmarks$ php -v
PHP 5.3.3 (cli) (built: Aug 22 2010 19:41:55)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
ruby-1.9.2-p0 adella:benchmarks$ cat foo.rb
msg = "Hello world"
@ckdake
ckdake / clone_with_modifications.rb
Created January 27, 2011 19:11
monkey patch to Ancestry to add tree cloning
module Ancestry
# https://github.com/stefankroes/ancestry
module InstanceMethods
# Clone an object and all children
# => replacing values with those from attributes if present
# => setting parent to new parent if present
# => setting the "original_id_field_name" if present to the id of the original object
#
# Example use_case:
// Form dirtyness tracking
$('form *').change( function() {
window.formDirty = true;
});
$('form input').click( function() {
if (this.name == "commit") {
window.formDirty = false;
}
});
window.onbeforeunload = function() {
@ckdake
ckdake / gist:784743
Created January 18, 2011 16:55
find_or_create_tree_by_name
# Addon for classes using https://github.com/stefankroes/ancestry
# Create the full tree for this root/parent/child if it doesnt yet exist
# .save! is required to generate ancestry fields so that .children works
def self.find_or_create_tree_by_name(root_name = nil, parent_name = nil, child_name = nil)
if root_name.present?
root = self.find_or_create_by_name(root_name)
root.save!
if parent_name.present?
parent = root.children.find_or_create_by_name(parent_name)
parent.save!
module DeviseHelper
def devise_error_messages!
unless resource.errors.empty?
content_tag :div, :id => "error_explanation", :escape => false do
content_tag(:h2, "The following errors were encountered:") +
content_tag(:ul, resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join.html_safe)
end.html_safe
end
end
end