Nested List Tag Helper

I’ve taken a useful snippet for creating lists from the blog below and
turned it into something that can create a nested list from an array
of arrays. Thought I’d share it since some of you might find it
useful and I also wanted to know if anybody can improve it (in general
and also add the ability to use the “cycle helper” by passing it into
the helper).

Inspiration:
http://termos.vemod.net/list-helper-for-rails

Here’s the code:

def html_list(type, elements, options = {})
items = elements.map do |element|
if element.is_a?(Array)
element = html_list(type, element, options)
else
content_tag(“li”, element)
end
end
content_tag(type, items, options)
end

def ul(*args)
html_list(“ul”, *args)
end

def ol(*args)
html_list(“ol”, *args)
end

This uses recursion so you can pass it an array of arrays.

So, this:
<%= ul [“first”, “first”, [“second”, [“third”, “third”, “third”],
“second”, “second”, [“third”, “third”, “third”, [“fourth”, “fourth”,
“fourth”]]]] %>

Becomes this (properly nested):

  1. first
  2. first
    1. third
    2. <ol>
        <li>second</li>
        <li>second</li>
        <li>second</li>
      </ol>
      
      <li>third</li>
      
      <li>third</li>
      
      <ol>
        <li>second</li>
        <li>second</li>
        <li>second</li>
      </ol>
      

The writeup:
http://blog.labratz.net/articles/2007/3/23/back-from-the-abyss-a-helper-to-create-nested-lists