Skip to content

Instantly share code, notes, and snippets.

@carpodaster
Created January 5, 2012 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carpodaster/1565573 to your computer and use it in GitHub Desktop.
Save carpodaster/1565573 to your computer and use it in GitHub Desktop.
Converts errors.on(:foo) to errors[:foo] when upgrading to Rails 3
#!/usr/bin/env bash
#
# This small piece of sed converts the obj.errors.on(:foo) calls in
# a single file to the new obj.errors[:foo] syntax as introduced in Rails 3
FILE=$1
function check_and_backup_file() {
if [ -e "$FILE" ]; then
cp $FILE $FILE.orig
else
echo "E: file '$FILE' does not exist"
exit 1
fi
}
function remove_backup_file() {
if [ -e "$FILE.orig" ]; then
rm $FILE.orig
fi
}
function convert() {
sed 's/\.errors\.on(:\([a-z_]*\))/\.errors\[:\1\]/g' <$FILE.orig >$FILE
}
function block_helpers() {
sed 's/<% fields_for/<%= fields_for/' <$FILE.orig >$FILE
}
function human_name() {
sed 's/\.human_name/.model_name.human/' <$FILE.orig >$FILE
}
function request_uri() {
sed 's/request\.request_uri/request.fullpath/' <$FILE.orig >$FILE
}
function assert_template() {
# assert_template "streets/index.html(.erb)" -> assert_template /^streets\/index\.html/
# sed -E 's/assert_template "([a-z_]+)(\\/)([a-z_]+)\\.([a-z]+)(\\.erb)*"/assert_template \\/\\^\\1\\\\\\2\\3\\\\.\\4\\//g' <$FILE.orig >$FILE
sed -E "s/assert_template (\"|')([a-z_]+)\.([a-z]+)(\.erb)*(\"|')/assert_template \/\^\2\\\.\3\//g" <$FILE.orig >$FILE
}
function usage() {
echo "Usage: $0 FILENAME"
}
if [ -z "$FILE" ]; then
usage
else
check_and_backup_file
# convert
# block_helpers
human_name
# request_uri
# assert_template
fi
remove_backup_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment