lib/simple_navigation/item.rb in simple-navigation-2.0.1 vs lib/simple_navigation/item.rb in simple-navigation-2.1.0
- old
+ new
@@ -4,20 +4,23 @@
class Item
attr_reader :key, :name, :url, :sub_navigation, :method
attr_writer :html_options
# see ItemContainer#item
- def initialize(container, key, name, url, options, sub_nav_block) #:nodoc:
+ #
+ # The subnavigation (if any) is either provided by a block or passed in directly as <tt>items</tt>
+ def initialize(container, key, name, url, options, items=nil, &sub_nav_block)
@container = container
@key = key
@method = options.delete(:method)
@name = name
@url = url
@html_options = options
- if sub_nav_block
+ if sub_nav_block || items
@sub_navigation = ItemContainer.new(@container.level + 1)
- sub_nav_block.call @sub_navigation
+ sub_nav_block.call @sub_navigation if sub_nav_block
+ @sub_navigation.items = items if items
end
end
# Returns true if this navigation item should be rendered as 'selected'.
# An item is selected if
@@ -31,11 +34,11 @@
end
# Returns the html-options hash for the item, i.e. the options specified for this item in the config-file.
# It also adds the 'selected' class to the list of classes if necessary.
def html_options
- default_options = self.autogenerate_item_ids? ? {:id => key.to_s} : {}
+ default_options = self.autogenerate_item_ids? ? {:id => autogenerated_item_id} : {}
options = default_options.merge(@html_options)
options[:class] = [@html_options[:class], self.selected_class].flatten.compact.join(' ')
options.delete(:class) if options[:class].blank?
options
end
@@ -77,9 +80,13 @@
# ActionController::Routing::Routes.recognize_path(url, {:method => (method || :get)})
# end
def autogenerate_item_ids?
SimpleNavigation.config.autogenerate_item_ids
+ end
+
+ def autogenerated_item_id
+ SimpleNavigation.config.id_generator.call(key)
end
def auto_highlight?
SimpleNavigation.config.auto_highlight && @container.auto_highlight
end
\ No newline at end of file