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
12 comments:
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)
Thanks for the feedback, I fixed the typo.
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!
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).
Thanks for your post, was very helpful. I just had to move the tasks/ directory into lib/ to have it work.
thanks
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.
rake aborted!
cannot load such file -- testing_now_march_27/railtie
I am getting this above issue. can u help me to solve it.
rake aborted!
cannot load such file -- testing_now_march_27/railtie
I am getting this above issue. can u help me to solve it.
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!
How can I access the rake task function in my application?
Like,
MyPlugin.function.
I highly recommend you to view this page to know more about WebWatcher app.
Post a Comment