This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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"))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(binding [clojure.contrib.sql/do-commands #(prn %) | |
clojure.contrib.sql/do-prepared #(prn %1 %2)] | |
(blog.models/create-db)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace(:db) { task :reset! => [:drop, :create, :migrate] } |