Sha256: ca6c4ed81295585613be68a2b4761ae2a4d1be13702e124931640eb170a9d7ea

Contents?: true

Size: 1.25 KB

Versions: 48

Compression:

Stored size: 1.25 KB

Contents

require 'push_type/menu_builder/dsl/menu_item'

module PushType
  module MenuBuilder

    class MenuRenderer

      def initialize(view)
        @view = view
      end

      def render_menu(menu_or_key)
        @menu = case menu_or_key
          when Menu   then menu_or_key
          when Symbol then PushType::MenuBuilder.select(menu_or_key)
          else raise('menu_or_key must be a menu of symbol')
        end

        @view.content_tag @menu.element, @menu.html_options do
          @menu.items.inject('') do |str, item|
            str << render_item(item)
          end.html_safe
        end
      end

      def render_item(item)
        text = get_item_value(:text, item)
        link = get_item_value(:link, item)

        css_class = [
          item.item_options[:class],
          ( get_item_value(:active, item) ? item.active_class || @menu.active_class : nil )
        ].compact.join(' ')
        options = item.item_options.merge class: ( css_class if css_class.present? )

        @view.content_tag item.element, @view.link_to(text, link, item.link_options), options
      end

      def get_item_value(key, item)
        val = item.instance_variable_get(:"@#{ key }")
        val.respond_to?(:call) ? @view.instance_exec(&val) : val
      end

    end

  end
end

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
push_type_core-0.5.0.alpha.4 lib/push_type/menu_builder/menu_renderer.rb
push_type_core-0.5.0.alpha.3 lib/push_type/menu_builder/menu_renderer.rb
push_type_core-0.5.0.alpha.2 lib/push_type/menu_builder/menu_renderer.rb
push_type_core-0.5.0.alpha.1 lib/push_type/menu_builder/menu_renderer.rb
push_type_core-0.4.0 lib/push_type/menu_builder/menu_renderer.rb
push_type_core-0.4.0.beta.3 lib/push_type/menu_builder/menu_renderer.rb
push_type_core-0.3.3 lib/push_type/menu_builder/menu_renderer.rb
push_type_core-0.3.1 lib/push_type/menu_builder/menu_renderer.rb