Skip to content

Instantly share code, notes, and snippets.

@hobberwickey
Created February 26, 2015 16:58
Show Gist options
  • Save hobberwickey/28b66a46934930a6e521 to your computer and use it in GitHub Desktop.
Save hobberwickey/28b66a46934930a6e521 to your computer and use it in GitHub Desktop.
Bash scripts for creating activerecord models and migrations (for Sinatra and such)
#create_migration.sh - example: create_migration.sh create_my_migration
#!/bin/sh
seporator="_"
IFS=$seporator read -ra ADDR <<< "$1"
className=""
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
for i in "${ADDR[@]}"; do
cap=$"$(tr '[:lower:]' '[:upper:]' <<< ${i:0:1})${i:1}"
className=$className$cap
done
cat > $DIR/../lib/db/migrate/$(date +%s)$seporator$1.rb << EOF
class $className < ActiveRecord::Migration
def change
end
end
EOF
#create_model.sh - example create_model.sh test_model
seporator="_"
IFS=$seporator read -ra ADDR <<< "$1"
modelName=""
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
for i in "${ADDR[@]}"; do
cap=$"$(tr '[:lower:]' '[:upper:]' <<< ${i:0:1})${i:1}"
modelName=$modelName$cap
done
cat > $DIR/../lib/models/$1.rb << EOF
class $modelName < ActiveRecord::Base
end
EOF
cat > $DIR/../lib/db/migrate/$(date +%s)_create_$1.rb << EOF
class $className < ActiveRecord::Migration
def change
end
end
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment