We all have seen those great plugins that can simplify your life with
just a few (or even a single) lines. They all use the extensible class
definition of Rails and such stuff, and it would be a dream to be able
to do such plugins yourself.
And exactly that is what I’d like to learn. To be a little more precise:
I want to create a plugin that extends a controller and a model class
with some methods dynamically based on a few configurations by the user.
E.g.:
I have a controller NewsController and a model News. In the controller I
put something like that:
class NewsController < ApplicationController
hello_world_plugin :my_name # E.g. :josh
end
Now my plugin should add the following method to NewsController:
def hello_world
render :action => :my_name # :my_name should be replace with the
specified value, e.g. :josh
end
And it should alos add a method to my News model:
def hello_my_sweet_world
Some other stuff
end
I guess I find out how the architecture of a plugin works (by taking a
look at existing plugins), but I’m quite un-experienced in stuff like
extending classes and such. What I need are references to tutorials or
something like that; I have the PickAxe and Agile 2nd Ed at home…
We all have seen those great plugins that can simplify your life with
just a few (or even a single) lines. They all use the extensible class
definition of Rails and such stuff, and it would be a dream to be able
to do such plugins yourself.
And exactly that is what I’d like to learn.
The hard part of writing a plugin is identifying what to write, not how
to
write it. Basically identify what is currently difficult in the Rails
world,
and unsolved by the plugin community so far.
Writing the plugin is the easy part. However I have seen some
ambivalence
to plugins. Some plugins broke with Rails versions, or the plugins
themselves weren’t mature enough (features vs testing). Current wisdom
seems
to be to use plugins sparingly, or thoroughly validate them yourself.
Its
really no different to any other development - if a problem happens in
your
application, it is best if it happens in your code and not a library or
plugin that you are using.
I am not trashing plugin authors here, I am advocating that you do not
overlook the necessary steps to mature it (testing, community feedback,
more testing, test with different rails versions and then do some
testing)
You aren’t going to be extending a controller and a model with just
one line in a controller, since controllers aren’t that directly
connected to models. For some tips on how to write plugins, though,
see the wiki: Peak Obsession
We all have seen those great plugins that can simplify your life with
just a few (or even a single) lines. They all use the extensible class
definition of Rails and such stuff, and it would be a dream to be able
to do such plugins yourself.
…
If you’ll forgive the outrageous self-promotion, Addison Wesley have
recently released a short PDF book which should help guide you though
what you can do with Rails plugin: