Writing rails plugins - tips and tricks - plugins with view helpers
One of the beauty of Ruby on Rails are the Plugins …
Ruby On Rails itself is already a great framework, but the combination of Rails plugins with the Ruby Open Classes is a killer feature!
This little combination enables you to create you own “tags” to be used in your views and layouts.
And it is really easy to create this kind of helper in Rails.
The non plugin way is to create a method in one of the Helper classes (the ones in app/helpers directory), for example, if all the forms in your application are inside a table, with one column for the label and one column for the real field, you can create a helper to simplify the code in your views like this:
- rails plugins101
- cd plugins101
- ./script/generate scaffold example name:string url:string
The standard very simple form for the “Example” model we created for this example, in the application standards would be:
1 2 3 4 5 6 7 | <% form_for(@example) do |f| %> <table> <tr><td><label for="example_name">Name</label></td><td><%= f.text_field :name %></td></tr> <tr><td><label for="example_name">Url</label></td><td><%= f.text_field :url %></td></tr> <tr><td colspan="2"><%= f.submit "Update" %></td></tr> </table> <% end %> |
but if we edit the file app/application_helper.rb and add the following method there:
1 2 3 | def textfield label, object, property, options = {} %Q{<tr><td><label for="#{object.to_s}_#{property.to_s}">#{label}</label></td><td>#{text_field object, property, options}</td></tr>} end |
we could simplify a lot the code only with the help of this “helper”, the new form code would be like this:
1 2 3 4 5 6 7 | <% form_for(@example) do |f| %> <table> <%= textfield "Name", :example, :name %> <%= textfield "Url", :example, :url %> <tr><td colspan="2"><%= f.submit "Update" %></td></tr> </table> <% end %> |
for this little form, this is too much work for some less characters in the view, but if you think about your entire application it would help you a lot!
Now think bigger, this ugly table/form standard I’ve created is the standard for the entire company, all applications in the company follow this standard.
So all developers, always write the forms using the first option (the ugly one, with lots of HTML).
and one day, a new designer tells that you need to add one new class in the “TR” tag in all forms for all applications, and you start crying ![]()
Or you choose to use the second option (with the helper method) and just need to change one line of code for each application in the company!
Now you are a hero, right?
But if you are smarter than this? what about creating a plugin, that all applications can use, and define this tag inside the plugin?
That way you change only one line of code, test only once, and all applications will be fixed at the same time!
And as always with Rails, this is a lot easy to do!
Just follow this steps:
- ./script/generate plugin life_saver
- Edit the file vendor/plugins/life_saver/lib/life_saver.rb and put the following content in it:
1 2 3 4 5 6
# LifeSaver module LifeSaver def textfield label, object, property, options = {} %Q{<tr><td><label for=#{object.to_s}_#{property.to_s}>#{label}</label></td><td>#{text_field object, property, options}</td></tr>} end end
- Edit the file vendor/plugins/life_saver/init.rb
1 2
# Include hook code here ActionView::Base.send :include, LifeSaver
- Remove the method from the application_helper.rb
Now you have just created your first Ruby On Rails plugin!
And yes, the code is the same as the one used in the application_helper.rb, the only trick here is in the init.rb file of the plugin, that line of code is including all methods of the module “LifeSaver” in the base class for all Rails views, the ActionView::Base class …
Now, if you did this, instead of just adding a helper method to each application, tell your boss how much time you have saved for the company with this little story here, and ask for a rise
I hope this little step by step help some one!
The next one will be about writing tests for your plugins, and the third one will talk about plugins with generators, the forth you’ll have to come back here to discover ![]()
If you enjoyed this post, make sure you subscribe to my RSS feed!





[…] Writing rails plugins - tips and tricks - plugins with view helpers Jan […]
[…] from a helper method to a plugin. This is the first post from a series about writing rails plugins.http://www.urubatan.info/2008/01/writing-rails-plugins-tips-and-tricks-plugins-with-view-helpers/Cooking for 80 The News JournalVolunteering to prepare dinner at at Ronald McDonald House means more […]