Sha256: 2f89b5da2cc764e134b79a8856e439282eedf579aa38d7cf0e8da2b7836e0e6f

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

module ActiveAdmin::MenuTree
  # ActiveAdmin::MenuTree::Config class
  class Config
    attr_reader :menu_tree, :menu_options

    def initialize
      @menu_tree = []
      @menu_options = []
    end

    def menu_tree=(new_value)
      raise ActiveAdmin::MenuTree::Error, "Invalid config" unless new_value.is_a? Array

      @menu_tree = new_value.map(&:deep_symbolize_keys)
      @menu_options = flatten_menu_tree
    end

    def find_menu_option(name:)
      menu_options.find { |item| item[:name] == name }
    end

    private

    def flatten_menu_tree
      menu_tree.map.with_index(1) do |item, index|
        options = format_options(item, index: index)
        next options unless item[:children].is_a? Array

        children =
          item[:children].map.with_index(1) do |child, child_index|
            format_options(child, index: child_index, parent: item[:label])
          end

        [options] + children
      end.flatten.compact
    end

    def format_options(item, index:, parent: nil)
      options = item.except(:children)
      options[:priority] = index * 10
      options[:label] ||= item[:name]&.pluralize&.titleize || ""
      options[:parent] = parent if parent.present?
      options
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activeadmin-menu_tree-0.1.1 lib/activeadmin/menu_tree/config.rb
activeadmin-menu_tree-0.1.0 lib/activeadmin/menu_tree/config.rb