FightinJoe : Aaron Wheeler

Parts, Builder, and Caching

Sunday, 23 March 2008

Rails is clever. Merb is smart.

Case in point: Components / Cells / Parts. The goal: to be able to extract generic view code for ease of use across the whole application. This doesn’t work well in Rails; Mike on Tech explains why here.

The solution in Rails to this problem is clever: using the Engines plugin (which hack Rails to make it easy to specify controller, view, and model code in a plugin), extend Rails to support cells in an efficient manner.

UPDATE Looks like Cells no longer has the Engines dependency.

The solution in Merb is smart: build the framework from the beginning to support components (called Parts).

Parts

From the source :


          # A Merb::PartController is a light weight way to share logic and templates
          # amongst controllers.  
          # Merb::PartControllers work just like Merb::controller.  
          # There is a filter stack, layouts (if needed) all the render functions,
          # and url generation.  
          #
          # Cookies, params, and even the request object are shared with the web controller
          

To start using Parts in Merb, generate a new Parts controller


          $ merb-gen part_controller generic
          # => exists  app
          # => exists  app/helpers
          # => exists  app/parts
          # => exists  app/parts/views
          # => create  app/parts/views/generic_part
          # => create  app/helpers/generic_part_helper.rb
          # => create  app/parts/generic_part.rb
          # => create  app/parts/views/generic_part/index.html.erb
          

To enable parts, include merb-parts as a dependancy in config/init.rb. Make sure that the merb-parts gem is installed (it’s installed with merb-more).


          # config/init.rb
          dependency( "merb-parts")
          

Each part will correspond to an action in a part controller. To create a menu part:

Add the #menu action to app/parts/generic_part.rb


          # app/parts/generic_part.rb
          def menu
            render
          end
          

Create the menu part view


          # app/parts/views/generic_part/menu.html.erb
          <ul>
            <li><a href="http://www.google.com">Google</a></li>
            <% params[:menu_items].each do |name, link| %>
              <li><a href="<%= link %>"><%= name %></a></li>
            <% end %>
          </ul>
          

Reference the part in your regular controller views. Any additional hash values are included in the params hash available to part controller actions.


          # app/views/blogs/index.html.erb
          ...
          <%= part( GenericPart => :menu, :menu_items => { :yahoo => 'http://www.yahoo.com' } ) %>
          

Builder

Builder is the same as XmlBuilder in Rails. It is a templating engine that makes it easy to write XML.

In Rails, access to the XmlBuilder object is made available to templates ending in .rxml. In Merb, this is how make a Builder template:

Inlude merb-builder as a dependency in config/init.rb. Make sure that the merb-builder gem is installed (it’s installed with merb-more).


          # config/init.rb
          dependency( "merb-buidler" )
          
Create your template with the .builder suffix. This template will have access to the xml object used by Builder.

          # app/views/blogs/index.xml.builder
          xml.posts do
            for post in @posts
              xml.post do
                xml.id post.id
              end
            end
          end
          

Caching

Caching is similarly turned on in Merb. Merb supports page, action, fragment, and object caching. Merb also supports the following cache stores: Database (ActiveRecord, DataMapper, and Sequel support), filesystem, memory, and memcache.

The merb-cache Readme has examples for cache usage and configuration.

One gotcha is that at the time of this writing, disabling caching does not turn off page caching. To enable turning off the page cache, see this fix.

Comments

  • Mar23
    Virginia Rector Your blogs/writing here are no longer in a language I understand. Where are you?

Comments are closed