Skip to content

Instantly share code, notes, and snippets.

View georgebrock's full-sized avatar

George Brocklehurst georgebrock

View GitHub Profile
// Lazily load models via XHR in MVC-style JavaScript
var Model = function(params) {};
Model.load_and_retry = function(id, controller, controller_variable) {
var controller_method = this.load_and_retry.caller;
var data_loaded_callback = function(data) {
controller[controller_variable] = new Model(data);
controller_method.apply(controller);
};
/*
Get a warning when jQuery finds no matches for a selector
Handy for debugging, although I wouldn't want it in production
*/
jQuery.oldFind = jQuery.find;
jQuery.find = function(sel) {
var result = jQuery.oldFind.apply(jQuery, arguments);
if(typeof(sel) == "string" && result.length == 0) {
console.warn("Nothing matches selector: ", sel);
# A secure S3 storage engine for Paperclip.
# Usage:
#
# require "paperclip/storage/s3secure"
# has_attached_file :image, :storage => :S3secure, :s3_permissions => :private, …
module Paperclip::Storage::S3secure
def self.extended base
base.extend(Paperclip::Storage::S3)
end
class Hash
def hash_map()
self.inject({}) do |hash, (k, v)|
hash.merge(k => yield(k, v))
end
end
end
{:a => 1, :b => 2}.hash_map {|k,v| v**2 } # => {:a => 1, :b => 4}
@georgebrock
georgebrock / Rakefile
Created February 18, 2010 23:32
Rake tasks to manage Heroku deploys
namespace :deploy do
PRODUCTION_APP = 'myapp'
STAGING_APP = 'myapp-staging'
def run(*cmd)
system(*cmd)
raise "Command #{cmd.inspect} failed!" unless $?.success?
end
def confirm(message)
@georgebrock
georgebrock / java_script_helper.rb
Created March 26, 2010 15:10
Helpers for unobtrusive JavaScript in Rails applications
module JavaScriptHelper
# Call this in your views to indicate that a feature needs to be initialised
def use_javascript_for(feature)
@javascript_features ||= []
@javascript_features << feature
end
# Use this in your layout to add a list of classes to add to the body so that the JavaScript knows which features to initialised
def javascript_feature_classes
@georgebrock
georgebrock / goviasgrid.js
Created December 9, 2010 10:57
Bookmarklet to overlay a 960px grid on any web page.
javascript:if(%20!window.goviasGrid%20){window.goviasGrid%20=%20document.createElement(%22div%22);document.getElementsByTagName(%22body%22)[0].appendChild(window.goviasGrid);window.goviasGrid.style.background%20=%20%22url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8AAAAABCAYAAAARiscXAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAa0lEQVRIDe3XsQ2AIBhE4WMMF3A0mQlHcxmlPxO4YGUeCc2fR8FXkFAkbX2P1619HP2naGe7vrxNPSp+C6D4LeD1o/jhlwjw/iVa3uLnJskEv0TLW/zcJJngl2h5i5+bJBP8Eq2Xtmjq//YAvUkuG1WPHY0AAAAASUVORK5CYII=)%20repeat-y%2050%%200%22;window.goviasGrid.style.width%20=%20%22960px%22;window.goviasGrid.style.margin%20=%20%220%20auto%22;window.goviasGrid.style.height%20=%20%22100%%22;window.goviasGrid.style.position%20=%20%22fixed%22;window.goviasGrid.style.top%20=%20%220%22;window.goviasGrid.style.bottom%20=%20%220%22;window.goviasGrid.style.left%20=%20%220%22;window.goviasGrid.style.right%20=%20%220%22;window.goviasGrid.style.display%20=%20%22none%22;window.goviasGrid.style.zIndex%20=%20%2299999%22;window.goviasGrid.onclick%20=%20fun
@georgebrock
georgebrock / bind-with-preload.js
Created September 29, 2011 21:55
bindWithPreload jQuery plugin
// jQuery extension to bind events that depend on an AJAX request.
//
// As soon as the event is bound it will start preloading the data from the server.
// If the event occurs while the data is still loading, your callback will be called
// as soon as the data finishes loading.
//
// Example:
// $('a.more').bindWithPreload('click', '/foo.json', function(data) {
// alert(data.message);
// });
@georgebrock
georgebrock / socks.sh
Created September 29, 2012 20:05
OS X SOCKS proxy script
#!/usr/bin/env bash
# The name of the wireless network has changed between versions of OS X, so we
# need to find the right name:
service=`networksetup -listallnetworkservices | grep -Ei 'wi-fi|airport'`
networksetup -setsocksfirewallproxy $service localhost 9000
networksetup -setsocksfirewallproxystate $service on
echo 'Connecting to proxy.'
@georgebrock
georgebrock / bisecttest.sh
Created October 17, 2012 08:35
git bisect script for Rails issue 7950
date=`git log -n1 --pretty=format:%ai`
cd ../activerecord-deprecated_finders
git checkout `git rev-list -n1 --before="$date" master`
cd ../rails
unset AR_DEPRECATED_FINDERS
grep active_record_deprecated_finders Gemfile && export AR_DEPRECATED_FINDERS=../activerecord-deprecated_finders
export JOURNEY=../journey