PLUGIN: default_find_options
Ahoy!
Rails has a lot of features but the core team is very cautious about adding any new functionality. Part of what has made it such a good framework is that they don't allow features in that aren't necessary or highly useful. This means that most of the cool add-ons we'd like to see have to end up as plugins. And plugins are really hard to make, right? Well, if you follow along with this tutorial you'll be a plugin author in just a few minutes.
What we're going to do is create a new Rails plugin that allows certain models to specify how they are loaded from the database by default.
The Concept
How things work now:
Offline
Awesome tutorial! And the example plugin is actually useful, woohoo! I love it when examples are useful. 
Just one note, I don't think the switch from find to find_every is insignificant. I haven't done any testing, but I don't think overriding "find" like that will work. What about the options which aren't part of a hash? Such as :all, :first, and ids?
I think overriding find_every's the right way to go from the start.
Offline
Yeah, I guess 'insignificant' isn't quite the right word for it. More like, "something you wouldn't normally need until way later when it would kick your ass".
"Find" is an interesting method. It can be overwridden just as in the example above but all it really does is look at the first argument and, based on that, sends the options to one of three other methods. ALL of which eventually end up in "find_every".
Offline
Really nice and interesting tutorial! I didn't know that writing his own plugin can be as easy. Thanks for that danger.
Offline
danger wrote:
I think I tried it with just "find" to see it if would work and it was highly functional. Didn't want to leave people with a broken plugin though :-)
It's just that find expects the first argument to be either :first, :all, or an id, but you are sending a hash as the first argument in the example:
So I'm not sure how that would work. find_every takes a hash so there's no problem there. I guess it's not that big of a deal since the end result works.
Offline
Great tutorial. Take it one step further and explain how to publish and advertise your plugin. That part is usually left out of these types of tutorials.
Offline
nice tutorial - its really good to found this howto since I need to write my own plugin
Offline
sethladd wrote:
Great tutorial. Take it one step further and explain how to publish and advertise your plugin. That part is usually left out of these types of tutorials.
Agreed! Both about this being an excelent tutorial and that it would be helpfull if you explained how to publish and advertise our plugins. Either way, keep up the good work!
Offline
I'm not really sure how much there is to say about publishing your plugins. Basically it's a two-step process:
1) Host your plugin in an svn repository somewhere.
2) Announce the plugin on your blog.
If it's useful, people will use it. If it's not - well, at least you can play around with it.
Offline
This one is good if you want to do some modification to the find method, but what about tutorial how to do login plugin or something similar?
I couldn't manage to find any tutorials telling how to do it?
And Thanks
Offline
Heya, thanks a ton.
Offline
I was having a few problems with this plugin (I pulled from your repository, thank you.)
1. find(params[:id]), not an uncommon task at all, was translating to "SELECT * FROM my_table ORDER BY my_column", leaving out the ID altogether. This appears to be a bug, which I fixed by rewriting to use with_scope instead.
2. (pretty minor) I'd like to use hash syntax to allow me to pass multiple in one default_find_option call.
I managed this by changing lib/ar_default_options.rb to:
Offline
Very interesting post, and also very innovative. I appreciate your effort, I followed each and every step dictated here.
Person.find(:all) shows the sorted people according to their ages but when i tried dynamic
finders like find_by_name, find_by_gender gives me wrong records
Person.find_by_name doesn't worked correctly???
Last edited by vibha (2008-10-22 12:36:24)
Offline