Skip to content

Instantly share code, notes, and snippets.

@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) {
@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 / 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 / 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 / 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 / get_query_params.js
Created March 29, 2011 14:09
A simple function to return a hash with parameters, parsed from a query string.
// Returns a hash with the query parameters, parsed from the query string given
// (or the current location's query string, if no arguments given)
// Based on code found here:
// http://stackoverflow.com/questions/901115/get-querystring-values-in-javascript/2880929#2880929
function getQueryParams(queryString) {
var queryParams = {},
e,
r = /([^&=]+)=?([^&]*)/g,
d = function (s) { return decodeURIComponent(s.replace(/\+/g, ' ')); },
q = queryString || window.location.search.substring(1);
@mitio
mitio / folder_image_counts.sh
Created April 27, 2011 18:36
Display a folder tree with an image count for each folder containing any .jpg files.
#!/bin/sh
dir="$1"
identation="$2"
self="$0"
if test "$dir" = ""
then
dir="."
fi
@mitio
mitio / block_dotsvn.conf
Created May 26, 2011 16:24
Forbid accessing of .svn folders via HTTP
# Nginx
server {
# (...server definition skipped...)
location ~ "/\.svn" {
return 403;
}
}
# Apache's .htaccess
RewriteEngine On
@mitio
mitio / fix_postgresql_to_sqlite.rb
Created July 10, 2011 17:17
"One-liner" to fix tab-delimited data, imported from PostgreSQL into SQLite
Dir['app/models/*.rb'].each do |m|
m = File.basename(m).split('.').first
m = m.camelcase.constantize
next unless m.respond_to?(:all)
m.all.each do |i|
m.columns.each do |c|
if [:string, :text].include?(c.type)
v = i.send(c.name)
i.send("#{c.name}=", v.gsub(/\\r\\n/, "\n")) if v && v.kind_of?(String)
end
@mitio
mitio / jammit_sass_support.rb
Created July 21, 2011 17:27 — forked from seven1m/sass_with_jammit.rb
SASS support for Jammit in development mode (also with proper plugins support, for e.g. Compass)
# hack to auto compile sass when Jammit runs in development mode
# you can place this in an initializer
unless Rails.env.production?
require 'sass/engine'
module Jammit
module Helper
SASS_TIMESTAMPS = {}