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
41 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
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
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.
Unprecedented post yet I was pondering whether you could make fairly more regarding this matter? I'd be thankful in the event that you could clarify genuinely further. Justified, despite any trouble Wall Sawing Concrete
I feel extremely appreciative that I read this. It is extremely useful and enlightening and I truly took in a ton from it. https://insighthikingexpertreview.wordpress.com
Thankful for making the push to inspect this, I feel firm about this and value focusing fundamentally more regarding this matter. If conceivable, as you gain authority, would you mind invigorating your page with fundamentally more nuances? It's especially valuable for me. https://bestcityindustry.hpage.com/
You want to know what up! if you're being lied to, want to see all the messages, phone calls and places they've been. Click and let's get to it, Get professional hacker
Thankful for making the push to inspect this, I feel firm about this and value focusing fundamentally more on this topic. must visit this link
This is a serious flawless post you shared, I like the post, thanks for sharing. See more
I have been looking at a couple of your records and I can state incredibly unprecedented stuff. I will bookmark your blog near me
I really appreciate essentially scrutinizing the aggregate of your weblogs. Fundamentally expected to exhort you that you have people like me who appreciate your work. near me
Much appreciation to you for this bewildering post, I am stunning I saw this site on Google. Substance, truly, nonetheless, the entire site is furthermore amazing. Like this
Much appreciation to you for this confusing post, I am stunning I saw this site on Google. Substance, truly, regardless, the entire site is staggering. Like this
Pleasant post. I learn something a lot harder on unmistakable web journals ordinary. Most normally it is invigorating to see content off their authors and utilize a little something from their site. I'd would prefer to utilize a few while utilizing content on my weblog whether you wouldn't fret. Normally I'll give a connection on your web blog. Much obliged for sharing. Like this
I truly appreciate basically investigating the total of your weblogs. Generally expected to urge you that you have individuals like me who like your work. The Grocers
Extraordinary post yet I was considering whether you could make genuinely seriously in regards to this matter? I'd be appreciative if you could explain truly further. slot
Thanks for the detailed article on this topic. I would like to see more such awesome articles from you. Download GBWhatsApp APK
Nice post I loved itt.. Keep giving such kind of information in future. Also you can get the new and best features of FMWhatsapp which are coming in 2022-
FMWhatsapp APK
I was also trying to add it in the rails app but failed. Thanks to your bog, i got success as well. Thank you for sharing this post with us. fmwhatsapp
If you want to customize the look and feel of your WhatsApp messaging app, you can change its default theme colors by installing FMWhatsApp.
Thanks for your thoughts. It was helpful cause now i learned exactly how to loading rake tasks from a gem. I think you shoud post this information on Instagram where a large amount of users could see it and remember, that you can always buy instagram followers to quickly boost their ammount.
Awesome, excellent weblog with incredible enlightening substance. This is a truly fascinating and useful substance.buy facebook likes
new themes, new features much more than whatsapp when you use fm whatsapp apk
Thanks for sharing. Minecraft 1.20 APK descargar to get the best version of the game
I thoroughly enjoyed reading it.FM Whatsapp
Post a Comment