Tuesday, February 16, 2010

Rails 3: Loading rake tasks from a gem

Several times in the last year I have wished that a gem could have rake tasks that would be loaded into a Rails app. I looked into it several times but kept running into dead ends with Rails 2. Since the Rails 3 beta came out I decided to try again, this time with much more success.

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)
end
This 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
end
In 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"
end
Just 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

10 comments:

Nathan Colgate said...

Thanks for this. Saved me a lot of time. Small typo:

require 'my_plugin/raltie' if defined?(Rails)


Should be:

require 'my_plugin/railtie' if defined?(Rails)

Nathan said...

Thanks for the feedback, I fixed the typo.

rejeep said...

Thank you for this post. It's crazy hard to find information about how to do this in both Rails 2 and Rails 3. Great post!

karle said...

Seams there is no way to do this in Rails 2. As far as I can tell, you have to update your Rakefile to include the path to your tasks?

i.e. require 'yourgem/tasks' (or whatever folder your rake tasks are in).

Florent said...

Thanks for your post, was very helpful. I just had to move the tasks/ directory into lib/ to have it work.

Nicola Piccinini said...

thanks

Lava Infotech said...

Web Application Development
This is really interesting, You’re a very skilled blogger. I have joined your feed and stay up for in the hunt for extra of your fantastic post. Also, I have shared your website in my social networks.

siva said...

rake aborted!
cannot load such file -- testing_now_march_27/railtie


I am getting this above issue. can u help me to solve it.

siva said...

rake aborted!
cannot load such file -- testing_now_march_27/railtie


I am getting this above issue. can u help me to solve it.

Richard Parratt said...

One typo:
I think in "Gem file structure" it should be:
lib/tasks/my_plugin.rake

Also, this works fine on Rails 4.1. Thanks!