Basically you are trying to include a rake task in your rails app, the tricky part is you are trying to include the rake task from the gem it is inside of, which itself is being included in the rails app.
Gem file structure
my_plugin.gemspec lib/my_plugin.rb lib/my_plugin/railtie.rb tasks/my_plugin.rake
lib/my_plugin.rb
module MyPlugin require 'my_plugin/railtie' if defined?(Rails) endThis file is automatically included as part of including the gem using bundler. All we need to do in this file is require our railtie.
lib/my_plugin/railtie.rb
require 'my_plugin' require 'rails' module MyPlugin class Railtie < Rails::Railtie railtie_name :my_plugin rake_tasks do load "tasks/my_plugin.rake" end end endIn this file we create a railtie class inside of our module, our new class has to be a descendant of the Rails::Railtie class. The 'rake_tasks' method is defined in the Rails::Railtie class, it takes a block that it then runs during the initialization of the rake environment in your app.
lib/tasks/my_plugin.rake
desc 'my plugins rake task' task :do_something do puts "the rake task did something" endJust a really simple rake task.
Using your new rake task
After you have built your gem and included it in your rails app using bundler the task is integrated just like any of the default rails tasks.
$ rake -T # ... # rake doc:rerails # Force a rebuild of the RDOC files # rake do_something # my plugins rake task # ... $ rake do_something # the rake task did something
Resources
http://weblog.rubyonrails.org/2010/2/9/plugin-authors-toward-a-better-future
https://gist.github.com/af7e572c2dc973add221