Skip to content

Instantly share code, notes, and snippets.

View mindscratch's full-sized avatar

Craig Wickesser mindscratch

View GitHub Profile
@mindscratch
mindscratch / ajax_setup.js
Created February 22, 2012 16:50
JavaScriptMVC Stuff
// do this once dom is ready
$(document).ajaxSend(function(e, xhr, options) {
var token = $("meta[name='csrf-token']").attr("content");
xhr.setRequestHeader("X-CSRF-Token", token);
});
@mindscratch
mindscratch / mongoid_hash_support.rb
Created February 28, 2012 13:19
mongoid hash extension
# BSON stores document keys as strings, so Hash keys are strings
# when going into MongoDB and coming out. This extension allows
# a developer to not worry as to whether or not the keys are
# Strings or Symbols.
#
# This requires Mongoid 2.4.3+ and Rails 3.
#
# Instead of needing Rails you could just require ActiveSupport. In your Gemfile (or .gemspec)
# gem "activesupport", ">= 3.0.0"
#
@mindscratch
mindscratch / 1_intro_to_subject.rb
Created February 29, 2012 18:11 — forked from knzai/1_intro_to_subject.rb
A pattern for testing class methods in ruby with rspec explicit subjects
# RSpec's subject method, both implicitly and explicitly set, is useful for
# declaratively setting up the context of the object under test. If you provide a
# class for your describe block, subject will implicitly be set to a new instance
# of this class (with no arguments passed to the constructor). If you want
# something more complex done, such as setting arguments, you can use the
# explicit subject setter, which takes a block.
describe Person do
context "born 19 years ago" do
subject { Person.new(:birthdate => 19.years.ago }
it { should be_eligible_to_vote }
@mindscratch
mindscratch / uri.js
Created April 21, 2012 13:33 — forked from jlong/uri.js
URI Parsing with Javascript
var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
parser.protocol; // => "http:"
parser.host; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
@mindscratch
mindscratch / futz.rb
Created May 8, 2012 16:26
simple ruby threading example
%W(INT TRAP).each do |sig|
trap(sig) do
puts "caught: #{sig}"
raise "get outta here"
end
end
x = Thread.new {
(1..5).each do
puts "working"
@mindscratch
mindscratch / self_signed_cert.rb
Created June 7, 2012 14:30 — forked from nickyp/self_signed_cert.rb
create a self-signed certificate using ruby-openssl
We couldn’t find that file to show.
# usage:
# $ chrome index.html
function chrome() {
open $@ --args --allow-file-access-from-files
}
# usage:
# $ server
alias server='open http://localhost:8000 && python -m SimpleHTTPServer'
@mindscratch
mindscratch / aop.rb
Created August 2, 2012 01:44
Aquarium DSL #after_raising
require 'aquarium'
module Gizmo
class ExceptionHandling
include Aquarium::DSL
types = ['Gizmo::FindAll', 'Gizmo::Find', 'Gizmo::Make', 'Gizmo::Modify', 'Gizmo::Delete']
around :calls_to => [:call], :in_types => types do |jp, operation, *args|
puts "inside around advice..
jp.proceed
#!/bin/sh
set -e
if [ -d "generated" ] ; then
echo >&2 "error: 'generated' directory already exists. Delete it first."
exit 1
fi
mkdir generated
@mindscratch
mindscratch / rails31init.md
Created August 16, 2012 12:48 — forked from samnang/rails31init.md
Rails 3.1 with Rspec, Factory Girl, Haml, Simple Form, Database Cleaner, Spork, and Guard

Install Rails 3.1

gem install rails

generate new app, skipping Test::Unit file generation

rails new my_app -T

Set up Gemfile