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 }