Recipe 14.7. Adding View Helpers to Rails as Plug-ins


Problem

You have view helper methods that you frequently reuse in your Rails applications. For example, you have a couple of W3C validation links that you repeatedly add to the layouts of your Rails applications during development to ensure the your XHTML and CSS is valid. You need a way to bundle and distribute these helpers for easy reuse.

Solution

Create a plug-in so that you can mix your view helpers into any application that installs that plug-in. To encapsulate these methods in a plug-in, start by creating a subdirectory of the plug-ins directory named after your plug-in: for example, vendor/plugins/ w3c_validation. Within this directory, create a subdirectory named lib containing a module named W3cValidationHelper. Within this module, define the validation methods available within your views: in this case, validate_xhtml10 and validate_css.

vendor/plugins/w3c_validation/lib/w3c_validation_helper.rb:

module W3cValidationHelper   def validate_xhtml10     html = <<-"HTML"       <p>         <a href="http://validator.w3.org/check?uri=referer"><img             src="/books/4/185/1/html/2/http://www.w3.org/Icons/valid-xhtml10"             alt="Valid XHTML 1.0 Strict" height="31" width="88"              style="border: 0;"/></a>       </p>     HTML     return html   end   def validate_css     referer = request.env['HTTP_HOST'] + request.env['REQUEST_URI']     html = <<-"HTML"       <p>         <a             href="http://jigsaw.w3.org/css-validator/validator?uri=#{referer}">             <img style="border:0;width:88px;height:31px"               src="/books/4/185/1/html/2/http://jigsaw.w3.org/css-validator/images/vcss"                alt="Valid CSS!" /></a>       </p>     HTML     return html   end end

In addition to a lib directory under w3c_validation, create init.rb, to be invoked when your application is started. Have Rails mix-in the W3cValidationHelper module by including it into ActionView::Base. Now, add the following line to init.rb:

vendor/plugins/w3c_validation/init.rb:

ActionView::Base.send :include, XhtmlValidationHelper

After you have restarted any Rails applications that have this plug-in installed, you can use the methods defined in the W3cValidationHelper module in your views. For example, adding a call to each helper method in application.rhtml, below any other content in the file, displays links to the XHTML and CSS validation services provided by W3C. If you want these to appear on your pages only during development, wrap the helper calls in a conditional that tests that your application is running with its "development" environment.

app/views/layouts/application.rhtml:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head>   <title>Rails Test</title> </head> <body>   <%= yield %>   <% if ENV['RAILS_ENV'] == 'development' %>     <%= validate_xhtml10 %>     <%= validate_css %>   <% end %> </body> </html>

Discussion

Whether you're an individual developer or are part of a team, it makes good sense to build up a library of helpers methods bundled as plug-ins. Once you start sharing helper methods across Rails projects, you should continually think of ways that you might make specific methods more general, so that they may be added to a shared helper plug-in.

The utility of plug-ins doesn't stop at view helpers. You can use plug-ins to extend any Rails class with the features that you need. Just don't get carried away and put all your helpers into one monolithic plug-in. It's a good idea to create plug-ins that consist of related helpers: for example, a plug-in that contains only view helpers, or even a specific category of view helpers. Each application you write should be able to include only those helpers that it needs.

See Also

  • W3C Validator, http://validator.w3.org




Rails Cookbook
Rails Cookbook (Cookbooks (OReilly))
ISBN: 0596527314
EAN: 2147483647
Year: 2007
Pages: 250
Authors: Rob Orsini

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net