Skip to content

Instantly share code, notes, and snippets.

View softcraft-development's full-sized avatar

Craig Walker softcraft-development

View GitHub Profile
{"commit"=>"Update Recipe",
"authenticity_token"=>"7P84gn9DtSosfxahaIan6ZH/zoExgYEbtgmGHLnK18c=",
"_method"=>"put",
"id"=>"test",
"recipe"=>{"name"=>"test",
"ingredients_attributes"=>{"0"=>{"name"=>"test",
"amount"=>"1",
"id"=>"13",
"_destroy"=>"1"}}}}
def test_update_nested
ingredient = Factory.create(:ingredient)
put :update,
:id=>ingredient.recipe.friendly_id,
:recipe=> {
:name=> ingredient.recipe.name,
:ingredients_attributes=>{
"0"=>{
:name=>ingredient.name,
:amount=>ingredient.amount,
# Goal: Allow addition of instances to a collection in a factory-built object
# when those instances require references to the parent.
# Typically occurs in Rails when one model has_many instances of another
# See more at:
# http://stackoverflow.com/questions/2937326/populating-an-association-with-children-in-factory-girl
class Factory
def has_many(collection)
# after_build is where you add instances to the factory-built collection.
# Out of the box, Ruby's Rational class doesn't know how to multiply or divide a BigDecimal;
# You get: "TypeError: Rational can't be coerced into BigDecimal" when you try.
class Rational
alias :"old_/" :"/"
def /(a)
begin
# Keep existing behavior if we can
send(:"old_/", a)
rescue TypeError
@softcraft-development
softcraft-development / hash_evoke.rb
Created June 13, 2010 23:45
Retrieve a value from a Hash. If the key is not in the hash, return the given default value and save it to the hash
class Hash
# Pass either a default value or a block to return it (a la Hash#fetch()).
# If both are passed, the block will take precedence over the default value
def evoke(key, default = nil)
if include?(key)
self[key]
else
self[key] = block_given? ? yield : default
end
end
@softcraft-development
softcraft-development / t
Created August 1, 2011 23:30
Craig's individual test runner
#!/usr/bin/env ruby
require "rubygems"
require 'activesupport'
file_name = ARGV[0].underscore.gsub(/_test$/, "")
test_name = ARGV[1].gsub(" ", "_") if ARGV[1]
paths = [
"test/unit/#{file_name}_test.rb",
"test/unit/helpers/#{file_name}_test.rb",
@softcraft-development
softcraft-development / git-switch
Created May 11, 2012 14:15
A ruby shell script to switch git branches based on a regex
#!/usr/bin/env ruby
search = Regexp.new(ARGV[0])
output = `git branch`
matching = output.lines.select{ |l| l.match(search) }
if matching.size == 0
STDERR.puts "No branch matches #{ARGV[0]}"
elsif matching.size == 1
`git checkout #{matching[0].strip}`
else
puts "Multiple branches matched:"
module A
def a_method
"a method"
end
end
module B
extend A
def b_method
module InstanceCache
def self.included(receiver)
receiver.extend(ClassMethods)
end
module ClassMethods
# Override me for good times
def find_by_cache_key(cache_key)
find(cache_key)
@softcraft-development
softcraft-development / gist:1c3964402b099893bd61
Last active August 29, 2015 14:11
Testing _.extend() for underscore/lodash
describe "Underscore Extend", ->
describe "simple objects", ->
it "bequeaths properties", ->
obj =
prop: "value"
expect(_.extend({}, obj).prop).toEqual("value")
it "bequeaths methods", ->
obj =
meth: () ->