Sha256: a1a7e4e0109ce1284348c50f15fa113ecdc039126ff073fc092ef0ae4d3d5e30

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module Fume
  module Nav
    module HelperExtensions
      def fume_nav(*args, &block)
        attributes = {
          active_class: "active",
          hide_if_empty: true
        }.merge(args.extract_options!)

        attributes[:current] = args.shift
        attributes[:helper] = self
        tag = NavTag.new(attributes)
        content = capture(tag, &block)

        return content if !tag.hide?
      end
      
      class NavTag
        attr_accessor :helper, :current, :active_class, :hide_if_empty

        def initialize(attributes = {})
          attributes.each_pair do |name, value|
            send("#{name}=", value)
          end
          @empty = true
        end

        def hide?
          hide_if_empty && @empty
        end
        
        def li_tag(value, options = {}, &block)
          self.content_tag(value, :li, options, &block)
        end

        def content_tag(value, tag_name, options = {}, &block)
          if value == current
            if options[:class]
              options[:class] += " #{active_class}"
            else
              options[:class] = "#{active_class}"
            end
          end

          @empty = false
          helper.content_tag(tag_name, options, &block)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fume-nav-0.0.2 lib/fume/nav/helper_ext.rb