Skip to content

Instantly share code, notes, and snippets.

@MTen
Last active April 14, 2020 00:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MTen/8310116 to your computer and use it in GitHub Desktop.
Save MTen/8310116 to your computer and use it in GitHub Desktop.
This is a flag hack for the "rails new <name_of_app>" command. It needs to be added to your .bash_profile and it will remove TestUnit, add the rspec-rails gem to your Gemfile, change directory into your app, re-bundle, and then generate a rspec folder. The syntax is "rails new <name_of_app> -rspec" I added pg to this and config database.yml
Adding a -rspec flag to rails. Also establishes and
rails() { if [[ $1 == "new" && $3 == "-rspec" ]]; then add_rspec $2; else command rails "$@"; fi; }
function add_rspec(){
command rails new $1 -d postgresql -T; #delete -d postgresql if you don't want pg
command chmod 664 $1/Gemfile;
echo -e "\n#Because rspec is better than Test::Unit. Or so I'm told.\ngem 'rspec-rails'" >> $1/Gemfile;
command chmod 644 $1/Gemfile;
command cd $1;
command bundle install;
db:username $1 #delete this if you don't want your config setup
command rake db:create; #delete this if you're not using pg or a database.
command rails generate rspec:install
}
function db:username() {
echo -e "
development:
adapter: postgresql
encoding: unicode
database: $1_development
pool: 5
username: #add your postgresql username here in the .bash_profile
password:
test:
adapter: postgresql
encoding: unicode
database: $1_test
pool: 5
username: #add your postgresql username here in the .bash_profile
password:
production:
adapter: postgresql
encoding: unicode
database: $1_production
pool: 5
username: #add your postgresql username here in the .bash_profile
password:
" > config/database.yml
@MTen
Copy link
Author

MTen commented Jan 9, 2014

Commented Version

Adding Rspec Rails

add this to your .bash_profile at /Users/(user name)/.bash_profile where you keep your other alias(alia? alias's's's's? aliases? :D )

usage syntax: rails new <name_of_app> -rpsec (ie: rails new monkey -rspec)

Args: func $1 $2 $3

rails() { if [[ $1 == "new" && $3 == "-rspec" ]]; then add_rspec $2; else command rails "$@"; fi; }

above add_rspec grabs the $2 (aka second rails argument) and then passes it through as add_rspec's first argument (aka $1)

function add_rspec(){
command rails new $1 -T; #running the rails new without TestUnit. if you want you can add -d postgresql here too.
command chmod 664 $1/Gemfile; #modifying Gemfile so it can be edited.

#if you're feeling bold YOU CAN PUT ANY GEM YOU WANT in the following echo-e"" and make your own rails gem functions.
#Just be sure to make new tests like the if above to evaluate the command prompt statements.

echo -e "\n#Because rspec is better than TestUnit. Or so I'm told.\ngem 'rspec-rails'" >> $1/Gemfile; #inserting code into the Gemfile.
command chmod 644 $1/Gemfile; #reverting the file permissions to prevent any security vulnerability.
command cd $1; #changing into the app directory.
command bundle install; #rebundling for rspec.
command rails generate rspec:install #generating rspec for the app.
}

Future .bash_profile commands

Automatically removes index from a new rails app. Need to see if this has to be kept below or above. Will finish it in my spare time.

rails() { if [[ $1 == "new"]]; then rails_delete_index; else command rails "$@"; fi}

function rails_delete_index{}

EXTRA! STOP HERE / DELETE THIS IF YOU DON'T WANT THIS COMMAND

Uncomment the following to quickly destroy test apps or directories when developing.

type ".destroyapp" INSIDE the directory you want to be recursively destroyed.

function .destroyapp() {

_d="$(pwd)"; #stores the absolute directory in the variable _d

command cd ..; changes directory up one level

command rm -r $_d; calls the on the saved variable to desomate it and all of it's children.

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment