Skip to content

Instantly share code, notes, and snippets.

@mitio
mitio / without_forgery_protection.rb
Created November 16, 2010 18:57
Small helper to allow generating forms without authenticity_token (when you have protect_from_forgery globally active).
# lib/forgery_protection_helpers.rb
module ForgeryProtectionHelpers
def without_forgery_protection
return unless block_given? && respond_to?(:controller)
original_value = controller.allow_forgery_protection
controller.allow_forgery_protection = false
result = yield
controller.allow_forgery_protection = original_value
result
end
@mitio
mitio / i18n_module_helpers.rb
Created June 30, 2010 17:48
I18n.with_locale(:en) { do_stuff } # and others...
module I18n
def self.with_locale(locale)
old_locale, result = I18n.locale, nil
I18n.locale = locale
result = yield
I18n.locale = old_locale
result
end
def self.each_locale
@mitio
mitio / du-for-files-newer-than.php
Created June 28, 2010 16:43
Simple command-line tool to print the total file size of files, newer than a given date.
#!/usr/bin/php
<?
if ($argc < 3) {
error_log("Usage:\n\t" . __FILE__ . " newer-than-date path [path [path...]]");
exit(1);
}
$accumulated_size_in_bytes = 0;
$newer_than_timestamp = strtotime($argv[1]);
@mitio
mitio / gist:443959
Created June 18, 2010 17:45
Bash initialization scripts.
# environment variables for scripts and the like
export RAILS_ENV=production
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# don't put duplicate lines in the history. See bash(1) for more options
export HISTCONTROL=ignoredups
# ... and ignore same sucessive entries.
export HISTCONTROL=ignoreboth
@mitio
mitio / fix_css_formatting
Created June 8, 2010 14:22
Simple command-line tool to prettify a bit CSS files.
#!/usr/local/bin/php
<?php
$replacements = array(
"/:([^ ])/" => ": \$1",
"/(\\w)\\{/" => "\$1 {",
"/\\}\n([^\n])/" => "}\n\n\$1",
);
if ($argc < 2) {