Sha256: 7a3b1436522f3fcc69e857348d2d3d0eb8474cf005b57ec82d6d4f9a622fb4a8

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 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

      ActiveAdmin::MenuTree.log_debug("menu_tree=: #{new_value.inspect}")
      @menu_tree = new_value.map(&:deep_symbolize_keys)
      @menu_options = flatten_options(@menu_tree)
    end

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

    private

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

        next_parent = parent ? [parent, item[:label]].flatten.compact : item[:label]
        children = flatten_options(item[:children], parent: next_parent)
        [options] + children
      end.flatten.compact
    end

    def format_options(item, index:, parent: nil)
      # TODO: validate option
      options = item.except(:children)
      if item.key?(:name)
        ActiveAdmin::MenuTree.warn_deprecated("Use `id` key, instead of `name`.")
        options[:id] ||= item[:name]
      end
      options[:priority] = index * 10
      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.3.0 lib/activeadmin/menu_tree/config.rb
activeadmin-menu_tree-0.2.3 lib/activeadmin/menu_tree/config.rb