views:

550

answers:

1

1) Is there a 'best' place for rake tasks inside of gems? I've seen them in /tasks, /lib/tasks, and I've seen them written as *.rb and *.rake -- not sure which (if any) is 'correct'

2) How do I make them available to the app once the gem is configured in the environment?

+2  A: 

Either /tasks and lib/tasks are a good place. I use /tasks when the tasks are meant to be used for working or managing the Gem itself, I use lib/tasks when the tasks are meant to be loaded by Gem users.

This is because users might have some problem in loading ruby files outside the lib folder which is automatically appended to the load path when the Gem is requires.

Also, I suggest you to use the *.rake extension. Nowadays almost every IDE associates .rake extension to ruby files and most advanced IDE can even parse the content as rake scripts. The .rake extension is a better choice for developers too because you can provide an immediate overview of the content inside the file.

About your second question, currently there's no way to have Rails loading your rake tasks when the plugin is packaged as Gem. You need to include the rake file in your application (in an other rake file or in the main Rakefile) or clone the rake tasks in your project.

If you install the plugin in the vendor folder, all *.rake files in both tasks and lib/tasks directories are automatically loaded into your Rails scope bu the following two lines.

Dir["#{RAILS_ROOT}/vendor/plugins/*/tasks/**/*.rake"].sort.each { |ext| load ext }
Dir["#{RAILS_ROOT}/vendor/plugins/*/lib/tasks/**/*.rake"].sort.each { |ext| load ext }
Simone Carletti
Some other resources for this: http://ggr.com/how-to-include-a-gems-rake-tasks-in-your-rails-app.html
J. Pablo Fernández