Sha256: c7304c93a04669f0d479a268933ff04e717054ae8310764e71f2b5f043f1e10e

Contents?: true

Size: 1.29 KB

Versions: 17

Compression:

Stored size: 1.29 KB

Contents

module Eac
  module MenusHelper
    class GuiBuilder
      def initialize(view)
        @view = view
      end

      def build(entries, options = {})
        fail "Argument \"entries\" is not a array" unless entries.is_a?(Array)
        @view.content_tag(:ul, options) do
          b = ActiveSupport::SafeBuffer.new
          entries.map { |v| menu_entry(v) }.each { |e| b << e }
          b
        end
      end

      private

      def menu_entry(entry)
        if entry[:type] == :group
          menu_group(entry[:label], entry[:children])
        elsif entry[:type] == :item
          menu_item(entry[:label], entry[:path], entry[:options])
        else
          fail "Unknown menu entry type: \"#{entry[:type]}\""
        end
      end

      def menu_group(label, children)
        @view.content_tag(:li) do
          @view.link_to(label) << build(children)
        end
      end

      def menu_item(label, path, options)
        @view.content_tag(:li) do
          @view.link_to(label, path, menu_item_link_options(options))
        end
      end

      def menu_item_link_options(options)
        options = options.dup
        if options.key?(:link_method)
          options[:method] = options[:link_method]
          options.delete(:link_method)
        end
        options
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
eac_rails_utils-0.8.0 lib/eac/menus_helper/gui_builder.rb
eac_rails_utils-0.7.1 lib/eac/menus_helper/gui_builder.rb
eac_rails_utils-0.7.0 lib/eac/menus_helper/gui_builder.rb
eac_rails_utils-0.6.0 lib/eac/menus_helper/gui_builder.rb
eac_rails_utils-0.5.0 lib/eac/menus_helper/gui_builder.rb
eac_rails_utils-0.4.0 lib/eac/menus_helper/gui_builder.rb
eac_rails_utils-0.3.0 lib/eac/menus_helper/gui_builder.rb
eac_rails_utils-0.2.2 lib/eac/menus_helper/gui_builder.rb
eac_rails_utils-0.2.1 lib/eac/menus_helper/gui_builder.rb
eac_rails_utils-0.2.0 lib/eac/menus_helper/gui_builder.rb
eac_rails_utils-0.1.15 lib/eac/menus_helper/gui_builder.rb
eac_rails_utils-0.1.14 lib/eac/menus_helper/gui_builder.rb
eac_rails_utils-0.1.13 lib/eac/menus_helper/gui_builder.rb
eac_rails_utils-0.1.12 lib/eac/menus_helper/gui_builder.rb
eac_rails_utils-0.1.11 lib/eac/menus_helper/gui_builder.rb
eac_rails_utils-0.1.10 lib/eac/menus_helper/gui_builder.rb
eac_rails_utils-0.0.1 lib/eac/menus_helper/gui_builder.rb