Sha256: 7b27f98ac3912047339c8dfdcc68d56bf8c0a87a13c0d37f221323e721485adb

Contents?: true

Size: 1.87 KB

Versions: 11

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

require 'eac_rails_utils/menus/action'
require 'eac_rails_utils/menus/node'
require 'eac_ruby_utils/core_ext'

module EacRailsUtils
  module Menus
    class Group < ::EacRailsUtils::Menus::Node
      class << self
        # @param key [Object]
        # @return [Symbol]
        def sanitize_key(key)
          key.to_sym
        end
      end

      DEFAULT_PATH_FIRST_NODE = :main_app
      SELF_LABEL_TRANSLATE_KEY_PART = '__self'

      common_constructor :key, :parent_group, :path_first_node, default: [nil],
                                                                super_args: -> { [parent_group] } do
        self.key = self.class.sanitize_key(key)
        self.path_first_node ||= DEFAULT_PATH_FIRST_NODE
      end

      # @param path [Array<Symbol>]
      # @return [EacRailsUtils::Menus::Action]
      def action(*path)
        child_action = ::EacRailsUtils::Menus::Action.new(path, self)
        actions[child_action.key] ||= child_action
      end

      # @return [Array<EacRailsUtils::Menus::Node>]
      def children
        (groups.values + actions.values).sort
      end

      # @return [EacRailsUtils::Menus::Group]
      def group(group_key, path_first_node = nil)
        child_group = self.class.new(group_key, self, path_first_node)
        groups[child_group.key] ||= child_group
      end

      # @return [String]
      def label_translate_key
        [super, SELF_LABEL_TRANSLATE_KEY_PART].join(TRANSLATE_KEY_SEPARATOR)
      end

      # @param view [ActionView::Base]
      # @return [Hash]
      def to_dropdown_menu_entries(view)
        children.inject({}) do |a, e|
          a.merge(e.label => e.to_dropdown_menu_entries(view))
        end
      end

      def within
        yield(self)
      end

      private

      def actions
        @actions ||= {}
      end

      def groups
        @groups ||= {}
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
eac_rails_utils-0.25.0 lib/eac_rails_utils/menus/group.rb
eac_rails_utils-0.24.0 lib/eac_rails_utils/menus/group.rb
eac_rails_utils-0.23.4 lib/eac_rails_utils/menus/group.rb
eac_rails_utils-0.23.3 lib/eac_rails_utils/menus/group.rb
eac_rails_utils-0.23.2 lib/eac_rails_utils/menus/group.rb
eac_rails_utils-0.23.1 lib/eac_rails_utils/menus/group.rb
eac_rails_utils-0.23.0 lib/eac_rails_utils/menus/group.rb
eac_rails_utils-0.22.3 lib/eac_rails_utils/menus/group.rb
eac_rails_utils-0.22.2 lib/eac_rails_utils/menus/group.rb
eac_rails_utils-0.22.1 lib/eac_rails_utils/menus/group.rb
eac_rails_utils-0.22.0 lib/eac_rails_utils/menus/group.rb