Skip to content

Instantly share code, notes, and snippets.

View tuzz's full-sized avatar

Chris Patuzzo tuzz

View GitHub Profile
@tuzz
tuzz / foo.rb
Created June 11, 2012 18:44
Associations on collections - ActiveRecord
# Currently, you have to roll your own associations if you want to chain scopes through collections.
# I'm interested to see if there is an existing (generalised) way to do this with AR.
# If not, feedback on the value and interface (method chaining?) for collection associations would be appreciated.
# gem install activerecord
# gem install sqlite3
require 'active_record'
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
@tuzz
tuzz / tag_migrator.rb
Created August 3, 2012 16:04
A fairly comprehensive acts-as-taggable-on tag migrator.
class TagMigrator
class << self
def list_tags(search_term = nil)
puts Tag.where('name like ?', "%#{search_term}%").order(:name).map(&:name)
end
def migrate!(old_name, new_name)
if old_name.downcase == new_name.downcase
rename!(old_name, new_name)
@tuzz
tuzz / dig.rb
Created August 3, 2012 21:18
Lookup hostnames of machines on your local subnet
1.upto(254).map.with_object({}) { |i, o| n = `dig -x 192.168.1.#{i} +short`; o[i] = n unless n.empty? }
@tuzz
tuzz / authenticate.rb
Created August 11, 2012 14:49
Bread and butter http basic authentication method for both ActionController and Capybara
def http_authenticate
user, pass = ENV['USERNAME'], ENV['PASSWORD']
if request
basic = ActionController::HttpAuthentication::Basic
request.env['HTTP_AUTHORIZATION'] = basic.encode_credentials(user, pass)
end
page.driver.browser.authorize user, pass
end
@tuzz
tuzz / syntax.css
Created August 12, 2012 11:33
Github Syntax Highlighting Stylesheet
.highlight .err {
color: #a61717;
background-color: #e3d2d2;
}
.highlight .k {
font-weight: bold;
}
.highlight .o {
@tuzz
tuzz / fix.rb
Created August 15, 2012 00:13
Heroku Pygments fix
# Heroku doesn't get on too well with Pygments:
# LoadError: Could not open library '/usr/local/lib/libpython2.7.a': /usr/local/lib/libpython2.7.a: invalid ELF header
# To fix:
# Gemfile
gem 'rubypython', '=0.5.1'
# config/initializers/pygments.rb
RubyPython.configure :python_exe => 'python2.6'
@tuzz
tuzz / no_cache.rb
Created August 17, 2012 09:17
Fix most AJAX no-layout caching problems
def my_action
response.headers['Cache-Control'] = 'no-store' if request.xhr?
# ...
end
@tuzz
tuzz / memoizer.js
Created August 21, 2012 10:57
JavaScript memoized recursion
var MyApp = {};
MyApp.memoizer = function (memo, formula) {
var recur, result;
recur = function (n) {
result = memo[n];
if (typeof result !== 'number') {
result = formula(recur, n);
memo[n] = result;
@tuzz
tuzz / loader.js
Created August 22, 2012 18:24
A modular approach to writing behavioural JavaScript for a web application
// Dependencies: jQuery
// Declare a namespace object for your application to avoid clobbering global variables.
var MyNamespace = {};
// Define a 'loader' method that attaches modular functions to DOM elements.
MyNamespace.loader = function (el) {
$('[data-module]', el).each(function () {
@tuzz
tuzz / gist:3603259
Created September 2, 2012 19:04
An application script that opens files in iTerm's vim
on run {input, parameters}
if (count of input) > 0 then
tell application "System Events"
set runs to false
try
set p to application process "iTerm"
set runs to true
end try
end tell
tell application "iTerm"