andi/simple-navigation — pull request #104 by ronalchn (2012-08-28 05:14:09+00:00)
Title: Allow item_container class to take a block on initialization for ad-hoc dynamic menu DSL

The current DSL for ad-hoc menus (generated dynamically) is not good. It is difficult to construct an array of hashes (which might be nested) in a easy to use way.

Allowing the initialize function to take a block enables the following, much better DSL for constructing a menu in-place, and rendering it.

SimpleNavigation::ItemContainer.new { |tab|
tab.item :statement, "Statement", problem_path(@problem)
tab.item :submit, "Submit", submit_problem_path(@problem) if can? :submit, @problem
}.render

It is also quite natural to use. Although - the ItemContainer class would read better if it was named Menu (Though that would probably be a more important deprecation decision).

Notice how this let's me use a CanCan or other `if ...` condition to decide when to show a particular item.

Otherwise it would be annoying to do a temporary array as you currently do:

menu = []
menu << {:key => :key, :name => "", ...} if can? ...