Sha256: 1ba81325ab4883123f91203076cade3378fa17b43cf719d2e616bcbb39880d45

Contents?: true

Size: 1.33 KB

Versions: 33

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

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

      def build(entries, options = {})
        raise '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
          raise "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

33 entries across 33 versions & 1 rubygems

Version Path
eac_rails_utils-0.22.2 app/helpers/eac_rails_utils/menus_helper/gui_builder.rb
eac_rails_utils-0.22.1 app/helpers/eac_rails_utils/menus_helper/gui_builder.rb
eac_rails_utils-0.22.0 app/helpers/eac_rails_utils/menus_helper/gui_builder.rb
eac_rails_utils-0.21.0 app/helpers/eac_rails_utils/menus_helper/gui_builder.rb
eac_rails_utils-0.20.0 app/helpers/eac_rails_utils/menus_helper/gui_builder.rb
eac_rails_utils-0.19.0 app/helpers/eac_rails_utils/menus_helper/gui_builder.rb
eac_rails_utils-0.18.0 app/helpers/eac_rails_utils/menus_helper/gui_builder.rb
eac_rails_utils-0.17.2 app/helpers/eac_rails_utils/menus_helper/gui_builder.rb
eac_rails_utils-0.17.1 app/helpers/eac_rails_utils/menus_helper/gui_builder.rb
eac_rails_utils-0.17.0 app/helpers/eac_rails_utils/menus_helper/gui_builder.rb
eac_rails_utils-0.16.0 app/helpers/eac_rails_utils/menus_helper/gui_builder.rb
eac_rails_utils-0.15.2 app/helpers/eac_rails_utils/menus_helper/gui_builder.rb
eac_rails_utils-0.15.1 app/helpers/eac_rails_utils/menus_helper/gui_builder.rb
eac_rails_utils-0.15.0 app/helpers/eac_rails_utils/menus_helper/gui_builder.rb
eac_rails_utils-0.14.1 app/helpers/eac_rails_utils/menus_helper/gui_builder.rb
eac_rails_utils-0.14.0 app/helpers/eac_rails_utils/menus_helper/gui_builder.rb
eac_rails_utils-0.13.5 app/helpers/eac_rails_utils/menus_helper/gui_builder.rb
eac_rails_utils-0.13.4 app/helpers/eac_rails_utils/menus_helper/gui_builder.rb
eac_rails_utils-0.13.3 app/helpers/eac_rails_utils/menus_helper/gui_builder.rb
eac_rails_utils-0.13.2 app/helpers/eac_rails_utils/menus_helper/gui_builder.rb