Skip to content

Instantly share code, notes, and snippets.

View remvee's full-sized avatar

Remco van 't Veer remvee

  • 52°N, 5°E
View GitHub Profile
@remvee
remvee / gist:271245
Created January 7, 2010 14:15
MySQL EXISTS vs IN
# MySQL basic ..IN (SELECT..
SELECT * FROM foos WHERE foo IN (SELECT foo FROM bars);
# can be rewritten to the much faster:
SELECT * FROM foos WHERE EXISTS (SELECT 1 FROM bars WHERE bars.foo = foos.foo);
@remvee
remvee / translation_scope.rb
Created January 12, 2010 11:40
Some scoping for Rails I18n
module ActionView
module Helpers
module TranslationHelper
# Override translate method to consider translate_scope.
def translate(key, options = {})
I18n.translate(key, {:raise => I18n.raise_on_missing, :scope => (translate_scope.dup << options.delete(:add_scope)).compact}.merge(options))
end
alias :t :translate
# Append scopes to the current translation scope.
@remvee
remvee / anonymize.rake
Created February 6, 2010 13:36
make all email address in datamodel @example.com addresses
namespace :anonymize do
desc "Replace all email addresses by an example.com variant."
task :email => :environment do
ActiveRecord::Base.connection.tables.map(&:singularize).map(&:camelize).map do |m|
m.constantize rescue nil
end.compact.select do |m|
m.column_names.include?("email")
end.each do |m|
m.all.each do |v|
v.update_attribute(:email, "#{v.email.sub('@', '-')}@example.com") unless v.email.blank? || v.email =~ /@example.com$/
@remvee
remvee / application_controller.rb
Created March 5, 2010 10:27
basic authentication for staging environment
class ApplicationController < ActionController::Base
helper :all
before_filter :authenticate if RAILS_ENV == 'staging'
private
def authenticate
authenticate_or_request_with_http_basic do |username, password|
username == STAGING_USERNAME && password == STAGING_PASSWORD
end
end
@remvee
remvee / *scratch*
Created March 8, 2010 08:43
bind f1, f2, f3 and f4 in emacs term
(defun term-send-function-key ()
(interactive)
(let* ((char last-input-event)
(output (cdr (assoc char term-function-key-alist))))
(term-send-raw-string output)))
(defconst term-function-key-alist '((f1 . "\e[OP")
(f2 . "\e[OQ")
(f3 . "\e[OR")
(f4 . "\e[OS")))
@remvee
remvee / noname.clj
Created March 16, 2010 20:20
multi method for rendering an object to html
(defmulti htmlify class)
(defmethod htmlify java.util.Map [value]
(html
[:dl
(map #(html [:dt (first %)]
[:dd (htmlify (last %))]) value)]))
(defmethod htmlify java.util.List [value]
(html
@remvee
remvee / models.clj
Created April 15, 2010 12:41
debug hack for clojure.contrib.sql
(binding [clojure.contrib.sql/do-commands #(prn %)
clojure.contrib.sql/do-prepared #(prn %1 %2)]
(blog.models/create-db))
@remvee
remvee / wxh.rb
Created May 1, 2010 09:36
read jpeg till width and height are known
def examine(io)
class << io
def readbyte; readchar; end unless method_defined?(:readbyte)
def readint; (readbyte << 8) + readbyte; end
def readframe; read(readint - 2); end
def readsof; [readint, readbyte, readint, readint, readbyte]; end
def next
c = readbyte while c != 0xFF
c = readbyte while c == 0xFF
c
@remvee
remvee / setup-lisp.el
Created June 1, 2010 12:51
launch lein swank and connect with slime
;; easy lein swanking
(defun lein-swank ()
(interactive)
(shell-command "lein swank&" "*lein-swank*")
(with-current-buffer "*lein-swank*"
(let ((timeout 10))
(while (and (> timeout 0)
(not (progn (goto-char (point-min))
(search-forward-regexp "Connection opened on local port +\\([0-9]+\\)" nil t)))
(not (progn (goto-char (point-min))
@remvee
remvee / Rakefile
Created July 28, 2010 09:43
rake db:reset is kind of pointless
namespace(:db) { task :reset! => [:drop, :create, :migrate] }