Sha256: 5ae781ed582a2572276370dee1470e5ac95db9070c18abe3db1f012d5dc3e492

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

module Navigasmic
  class XmlNavigationBuilder

    @@classnames = {
      :with_group => 'with-group',
      :disabled => 'disabled',
      :highlighted => 'highlighted'
    }

    attr_accessor :template, :name, :items

    def initialize(template, name, options = {}, &proc)
      @template, @name, @items = template, name.to_s, options, []
      render(options.delete(:html), &proc)
    end

    def render(options, &proc)
      buffer = template.capture(self, &proc)
      template.concat(template.content_tag(:ol, buffer, options))
    end

    def group(label = nil, options = {}, &proc)
      raise ArgumentError, "Missing block" unless block_given?

      options[:html] ||= {}
      options[:html][:class] = template.add_class(options[:html][:class], @@classnames[:with_group])
      options[:html][:id] ||= label.to_s.gsub(/\s/, '_').underscore

      buffer = template.capture(self, &proc)
      group = template.content_tag(:ol, buffer)
      label = label_for_group(label) unless label.blank?

      template.content_tag(:li, label.to_s + group, options.delete(:html))
    end

    def item(label, options = {}, &proc)
      buffer = block_given? ? template.capture(self, &proc) : ''

      item = NavigationItem.new(label, options)

      options[:html] ||= {}
      options[:html][:id] ||= label.to_s.gsub(/\s/, '_').underscore

      options[:html][:class] = template.add_class(options[:html][:class], @@classnames[:disabled]) if item.disabled?
      options[:html][:class] = template.add_class(options[:html][:class], @@classnames[:highlighted]) if item.highlighted?(template.request.path, template.params)

      label = label_for_item(label)
      label = template.link_to(label, item.link) unless item.link.empty?

      template.content_tag(:li, label + buffer, options.delete(:html))
    end

    def label_for_group(label)
      template.content_tag(:span, label.to_s)
    end

    def label_for_item(label)
      template.content_tag(:span, label.to_s)
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
navigasmic-0.1.3 lib/builders/xml_builder.rb
navigasmic-0.1.1 lib/builders/xml_builder.rb