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 :deploy do | |
task :start, :roles => :app do | |
sudo "mongrel_rails cluster::start -C #{mongrel_conf}" | |
end | |
task :stop, :roles => :app do | |
sudo "mongrel_rails cluster::stop -C #{mongrel_conf}" | |
y = { | |
"port" => 3000, "servers" => 2, "pid_file" => "tmp/pids/mongrel.pid" |
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
// make sure you call field.fire('hidden:change') when a hidden field changes | |
document.observe('dom:loaded', function() { | |
$$('input[type=hidden]').each(function(e) { | |
var onchange = e.getAttribute('onchange') | |
if (onchange) { | |
if (typeof(onchange) != 'function') { | |
eval("onchange = (function(event){(function(){" + onchange + "}).bind(Event.element(event))()});"); | |
} | |
e.observe('hidden:change', onchange); | |
} |
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
// Usage: App.with_resource('user', 567, function(user) { $('report_user_name').value = user.name }) | |
var App = { | |
with_resource: function(name, id, func) { | |
new Ajax.Request("/" + name + "s/" + id + ".json", { | |
method: 'GET', | |
onSuccess: function(transport) { func(transport.responseJSON[name]) } | |
}); | |
} | |
} |
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
# Allow query usings scopes and arguments from a hash. Useful for | |
# making index actions. | |
# | |
# Example: | |
# | |
# User.with_scopes(:search => "Fred") | |
# | |
# Where +search+ is a named scope on +User+ which excepts a parameter. | |
module WithScopes | |
# Determine if named scope exists. |
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
# Matches posts containing all words in the body. | |
named_scope :search, lambda { |q| | |
if connection.adapter_name == 'MySQL' | |
q = q.split(/\W/).map{|q| "[[:<:]]#{Regexp.escape(q)}[[:>:]]"} | |
{:conditions => [(['posts.body REGEXP ?'] * q.size).join(" AND "), *q]} | |
else | |
raise NotImplementedError | |
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
# 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"))) |