Per-project environment variables with Foreman

If you haven't heard of Foreman, it's a painless way to kickstart an app that depends on running multiple tools or processes. In short, if you are using an app with a lot of stuff that requires to be run like Redis, a rake task, and then the development server, then Foreman is for you. Check it out.

I've been working with increasing number of projects lately and managing environments for each of those has been a time sink. I do maintain a per-project gemset using rvmrc, which does a nice job of isolating gems. But when each of your projects need some environment variables, your bashrc or bash_profile is going to hell. 

Well, not actually. I dug up the Foreman docs and found something very useful. If you use Foreman, it supports reading environment variables from a .env file in the project directory. So if your current project uses $GIRLFRIEND_VERSION and $GIRLFRIEND_PATH, just create the following ".env" file in project directory.

And start foreman. Your project can now find your girlfriend's version and path ;)

UPDATE: It is very likely that env variables may be different for your team members. I choose to add .env to my global gitignore file and create a "sample.env" file in the directory, with list of env vars with default values and add it to the repo. Anyone who clones the repo can setup the .env with the values specific to their setup.

Posted
 

the handy bundle-exec

I found myself in a pretty confusing situation today when I was trying out Rails 3.1.rc1. In my Gemfile, I specified Compass to be installed from the github repo and bundler did just that. But I had to run Compass's executable in my rails app and I had no clue as to how to do it, because my rvm gemset for trying the RC version of Rails doesn't have Compass installed in the gem environment. So running the following command, generated a command not found error.

Bundler installs it local to the application, when you choose to install from the git repo, so the executables aren't in your gem path. In such situations, bundle-exec comes handy.

Will run the compass command with it's arguments in the scope of the bundle (I'm referring to the gem dependency environment managed by bundler not to git-bundle).

 

Posted
 

Rails 3.1.0 beta1

ERROR:  While generating documentation for activesupport-3.1.0.beta1

... MESSAGE:   incompatible character encodings: UTF-8 and ASCII-8BIT
... RDOC args: --op /home/akashmanohar/.rvm/gems/ruby-1.9.2-p180@rails31/doc/activesupport-3.1.0.beta1/rdoc lib --title activesupport-3.1.0.beta1 Documentation --quiet

That's what you get when you have rubygems 1.7.2. Just do a 

gem update --system

to get 1.8.0 (or whatever is the latest by your date) and the install will go smooth.

Could not find a JavaScript runtime (ExecJS::RuntimeError)

That happens when the ExecJS gem cannot find any javascript runtime installed on your computer. Just install nodejs for now and the error should disappear. I usually choose to install nodejs from the github repo. That way it's easier to get the latest and greatest quickly, rather than having to download and install a new pkg everytime.

And then go generate an app and run the dev server, it'll work. But just don't expect to checkout details of the changes to the frontend (the sass, coffee-script stuff). If you open app/assets/javascript/application.css you might notice a FIXME message. Things aren't that clear to me. Maybe I'll have to spend more time reading the available docs or I'll checkout the next beta (or the RC) and try creating an app with that. Like the release blog post on the rails blog mentions, there isn't enough documentation right now.

 

Posted
 

Quickly finding model associations in rails

In Jan-2011, at my internship, I got a chance to work on a really complex application. And I had to get upto speed with everyone else by understanding the app and the reasons behind certain decisions. The one i worked on had more than two dozen models. Thanks to my commandLineFu, I accidentally, found a trick to list associations between models in a rails app in a readable way. And it's very simple.

From the root of the app (can be done from anywhere, but i'll just say I'm in the root of the app).

grep -ri "has_many" ./

Assuming you don't have the text "has_many" anywhere else in the app. If you do, just change ./ to app/models
The same can be done for belongs_to

grep -ri "belongs_to" ./

You'll be surprised to find that bash (ubuntu guy here!) gives you a really nice readable view of the associations. Each line, when read fully, shouts out the association :)
Posted
 

Here's BillBob

BillMeBob.com
– Create invoice
– Get short-url
– Share it

A sarcastic line, that I didn’t want to signup for some other service to generate just one invoice turned into a quick one-week app – BillMeBob.com. I took off a week from my other always-thinking-of-it idea to create BillMeBob and get used to Rails3.

P.S: I turned 20 last week

Posted