views:

47

answers:

3

This question came about because the cells gem specifies template directories using File.join('app','cells'). That works fine until you run Rails as a daemon (scripts/server -d). The daemon switches directories to / which leaves the cells template paths pointing to the wrong absolute path.

My solution was to set the default paths to File.join(RAILS_ROOT, 'app', 'cells'). This works in Rails, but the unit tests for the plugin fail because RAILS_ROOT isn't defined. Using File.join(File.dirname(__FILE__),'..' ... also works but requires about 6 levels of '..' which seems wrong.

So my question is what is the proper way to specify the path to a directory under 'app' in a Rails plugin? Or is there something else wrong that would cause daemonizing Rails to fail to find the relative paths?

+1  A: 

I suggest moving your changes out of the plugin and into an initializer. In the initializer override the method that uses File.join('app','cells'). This has several benefits.

  1. You are not modifying third-party code directly so you are more likely not to have to worry about re-applying changes on an upgrade.
  2. By not modifying the plugin itself the plugin unit tests will still pass.
  3. You are able to use RAILS_ROOT which I think is the right solution.
Randy Simon
That turned out to be the right way to approach it. There doesn't seem to be a standard way of handling relative paths for gems for which Rails is optional. In this case, it's more of a documentation issue; the gem should probably not provide the defaults so everyone has to configure it and you don't hit issues with daemon vs. non-daemon.
Brian Deterling
A: 

Providing no default path at all would break almost every app using cells out there :-)

Maybe we should always use the absolute path?

Nick