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
21 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.
spy-apps-software.com - the best site about spyware for Android and iOS, honest reviews on the most modern spy apps and more useful information about tracking cell phones!
Do you want the best website to hack a cell phone? you won't miss out on this deal. Check this hacking site https://hackwizards.com to hire a hacker.
When you want to check on a spy app, take a look here how to spy on a cell phone without having it hire a hacker to change grades
spy app without target phone
Read more on
How to spy on husband phones
I visit your blog often and endorse it to the individuals who expected to improve their knowledge without any problem. The way of creating is splendid and moreover, the substance is a decision. An obligation of appreciation is all together for that astuteness you give the peruses. Read more Read more
I am uncommonly happy to discover your post as it will be on top of my get-together of most cherished destinations to visit. Website
Hey, you used to compose awesomely, yet the last scarcely any posts have been kind of exhausting… I miss your extraordinary works. Past a few posts are slightly out of track! please! See more
I have as of late began a blog, the data you give on this webpage has helped me enormously. A debt of gratitude is in order for the entirety of your time and work. Website
This is really respectable and instructive, containing all information moreover extraordinarily influences the new development. An obligation of appreciation is all together for sharing it, Read more
Glad to read this blog article Are you interested in using facebook messenger hack view this site for more details? I hope you'll enjoy this one.
Post a Comment