Skip to content

Instantly share code, notes, and snippets.

@tomlea
tomlea / xbox
Created October 27, 2014 03:01
Control your XBox One from the OS X command line!
#!/usr/bin/env bash
if [ "x$1" == 'xoff' ]
then
$0 turn off
sleep 1
say yes
else
say X box $*
fi
module ActiveRecord
module ConnectionAdapters
module DatabaseStatements
#
# Run the normal transaction method; when it's done, check to see if there
# is exactly one open transaction. If so, that's the transactional
# fixtures transaction; from the model's standpoint, the completed
# transaction is the real deal. Send commit callbacks to models.
#
# If the transaction block raises a Rollback, we need to know, so we don't
@tomlea
tomlea / hero.sh
Created August 1, 2013 16:02
When you have multiple heroku apps, for one repo, you're going to need a hero! hero production run rake db:migrate
function _hero_getRemoteNames(){
git config --get-regexp remote.*.url heroku.com |
sed -E 's/^remote\.([^\.]*)\.url .*$/\1/'
}
function _hero_herokuAppFor(){
git config --get remote.$1.url |
sed -E 's/^git@heroku.com:([^.]*)\.git/\1/'
}
@tomlea
tomlea / quick_bulk_select.rb
Created January 6, 2012 12:06
Select Some Columns
# Introduction:
We need to select large numbers of items (300k or more), based on active record associations (and selectors, and all that faff), but we only care about the ids.
This is easily solved by bla_bla_bla_scope.map(&:id). This however takes about 16s of CPU grinding ruby to get to the final answer via AR objects. AR objects that contain only an ID.
Why not just use the SQL generated by the scopes and other voodoo to just pump the SQL through? That's what this does, in the most complex way imaginable!
# Usage:
@tomlea
tomlea / start_sql_servers.sh
Created March 3, 2011 13:51
Start a MySQL master/slave pair.
rm -R $HOME/Desktop/server-{1,2}
mkdir -p $HOME/Desktop/server-{1,2}
mysql_install_db --datadir=$HOME/Desktop/server-1 && mysqld --server-id 1 -h ~/Desktop/server-1 --socket ~/Desktop/server-1/mysql.sock -P 10001 --log-bin &
mysql_install_db --datadir=$HOME/Desktop/server-2 && mysqld --server-id 2 -h ~/Desktop/server-2 --socket ~/Desktop/server-2/mysql.sock -P 10002 --master-port=10001 --master-host=127.0.0.1 --master-user=root &
alias sql-master="mysql -h127.0.0.1 -P10001 -uroot"
alias sql-slave="mysql -h127.0.0.1 -P10002 -uroot"
alias sql-master-admin="mysqladmin -h127.0.0.1 -P10001 -uroot"
javascript:
if( !window.goviasGrid ){
window.goviasGrid = document.createElement("div");
document.getElementsByTagName("body")[0].appendChild(window.goviasGrid);
window.goviasGrid.style.background = "url(http://tomlea.co.uk/assets/the-grid.png) repeat-y 50% 0";
window.goviasGrid.style.width = "100%";
window.goviasGrid.style.height = "100%";
window.goviasGrid.style.position = "fixed";
window.goviasGrid.style.top = "0";
window.goviasGrid.style.bottom = "0";
AU-Colleen
AU-Jon
BE-Hendrik
BE-Minna
BE-Sofie
BR-Lygia
CA-Antonine
CA-Felix
CN-Pan
CZ-Zdenech
# A secure S3 storage engine for Paperclip.
# Usage:
#
# require "paperclip/storage/s3secure"
# has_attached_file :image, :storage => :S3secure, …
module Paperclip::Storage::S3secure
def self.extended base
base.extend(Paperclip::Storage::S3)
base.options[:s3_permissions] = :private
def sort_order
# Google was indexing "random" search orders, it parsed our javascript, badly.
SORT_METHODS[params[:sort_order]] or raise_redirect_to(@product, :status => 301)
end
def segment
segment = params[:segment]
segment = segment.to_sym if %w[all other].include?(segment)
segment = nil if segment == :all
# Bad or expired segments need 301ing
module RaisableRedirections
def self.included(other)
other.send(:around_filter, :handle_raisable_redirections)
end
protected
def raise_redirect_to(*args)
raise Redirect.new(*args)
end