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 / commit.rb
Created July 24, 2012 14:30
Generate a pie chart showing commits by author in a git repository
#!/usr/bin/ruby
# Run in the root directory of a git repository with ruby commits.rb
# Depends upon the 'rmagic' and 'gruff' gems.
require 'rubygems'
require 'gruff'
commits = `git shortlog -ns`.split("\n").map { |e| e.strip.split("\t") }.map { |n, a| [a, n.to_i] }
@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 / github.css
Last active April 23, 2024 15:47
Github Markdown Stylesheet
/*
Copyright (c) 2017 Chris Patuzzo
https://twitter.com/chrispatuzzo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
@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;