Here is a simple way to share the same .bashrc / .zshrc / .bash_profile
across multiple computers, while still retaining unique settings in between
computers.
Suppose you want some special setting to apply only to your laptop.
First, create an empty file called .setup_00:
$ touch ~/.setup_00
Next, in your rc file, add the following if statement. Anything
inside that if statement will only apply to your laptop:
if [ -f '.setup_00' ]; then
echo "This message only shows on my laptop!"
fi
You can use this method to run any shell script uniquely on computers
that contain the .setup_00 file.
That’s it. It’s not fancy, but it works.
Comments? Click me!
Updated: Instad of .rvmrc, I now use .ruby-version and .ruby-gemset.
A little cheatsheet of Rails stuff I don’t want to forget. Many of these
tips come from
Michael Hartl’s Rails tutorial.
Setting Up Environment
Install RVM, Ruby, Bundler
Install RVM through instructions on rvm.io.
RVM now also eliminates the need to use bundle exec when used with bundler.
Install latest ruby. The following OpenSSL option is needed on Ubuntu:
$ rvm install 1.9.3-head --with-openssl-dir=$HOME/.rvm/
Aside:
If you ever get the following warning:
make: /usr/bin/gcc-4.2: No such file or directory
You will also need to run
$ rvm reinstall 1.9.3-head --with-gcc=clang
Create project gemset and be in it before installing Rails:
$ rvm use 1.9.3-head@projectname --create
Project-specific gemsets prevent big headaches. Don’t forget to set a
project rvmrc file after Rail app is generated (see below).
Install Rails
$ gem install rails
And if on Linux, also run:
$ sudo apt-get install libxslt-dev libxml2-dev libsqlite3-dev
Install node.js so that rails server can run properly in your development
environment.
Initialize Rails App
Set up Rails App Directory
Skip TestUnit if you’re using rspec:
$ rails new project_name --skip-test-unit
Now create two files: .ruby-version and .ruby-gemset. These files will
ensure that rvm, or whatever ruby envelope you use, know to switch to the
correct Ruby version (and gemset, in rvm). For example, if you’re using Ruby
2.0.0 and a gemset called my-project, create a .ruby-version file with the
following content:
2.0.0
And a .ruby-gemset file with:
my-project
Now cd in and out of the directory and make sure that rvm is switching to the
correct gemset with:
$ rvm gemset list
Set up Rspec
Add rspec, guard and spork to development / test environment in Gemfile:
group :development, :test do
gem 'rspec-rails', '~> 2'
# If using guard and spork:
gem 'guard-rspec', '~> 2'
gem 'guard-spork', '~> 1'
gem 'spork', '~> 0.9'
end
Make Rails use rspec instead of TestUnit:
$ rails g rspec:install
Speeding up Tests with Spork and Guard
Follow
these instructions.
Heroku / Deployment
Add PostgreSQL gem and production-environment-specific gems. In gem file:
group :production do
gem 'pg'
end
To run bundler without production gems
$ bundle install --without production
Bundler remembers this setting, no need to use it again.
Initialize Heroku:
$ heroku login
$ heroku create
Push to Heroku and rename:
$ git push heroku
$ heroku rename project_name
Maintenance
After Migrations
After running
$ rake db:migrate
The following come in handy:
Stop the Rails server. This is obvious, but forgetting to restart the server
after a migration is a sure way to waste time wondering why half your tests are
suddenly failing.
Annotate models and migrations (requires the annotate gem):
$ annotate
Tell the test database about the migration too:
$ rake db:test:prepare
Restart the Rails server.
Sometimes you want to reset your test database:
$ rake db:reset
$ rake db:test:prepare
Run Console in Sandbox Mode!
Don’t break things:
$ rails console --sandbox
Comments? Click me!
Yesterday I started using MathJax to render mathematical
equations on my blog. MathJax uses a Javascript library to render LaTex on the
client side, as a result, mathematical equations will not render if you are
reading this blog through an RSS feed or email.
I toyed with the idea of rendering equations as .pngs, but I like the
convenience of MathJax. From now on, posts that include mathematical equations
will include a warning, so readers on RSS know to check the original post.
Comments? Click me!