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 / deploy.rb
Created November 23, 2009 09:03
cap avoid too fast restart of mongrel-cluster
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"
@remvee
remvee / autocomplete_select.js
Created December 4, 2009 14:25
JS onchange hack for input[type=hidden]
// 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);
}
@remvee
remvee / application.js
Created December 4, 2009 15:42
JS App.with_resource
// 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]) }
});
}
}
@remvee
remvee / with_scopes.rb
Created December 8, 2009 11:34
combine scopes from a hash
# 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.
@remvee
remvee / post.rb
Created December 24, 2009 13:11
Simple all words named_scope
# 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
}
@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")))