Sha256: f6f0ec7e9257332c103a4c5260201776e6591965b437620bfcd926c62e1ee5dd

Contents?: true

Size: 1.68 KB

Versions: 30

Compression:

Stored size: 1.68 KB

Contents

class Avo::Menu::Builder
  class << self
    def parse_menu(&block)
      Docile.dsl_eval(Avo::Menu::Builder.new, &block).build
    end
  end

  def initialize(name: nil, items: [])
    @menu = Avo::Menu::Menu.new

    @menu.name = name
    @menu.items = items
  end

  # Adds a link
  def link(name, **args)
    @menu.items << Avo::Menu::Link.new(name: name, **args)
  end

  # Validates and adds a resource
  def resource(name, **args)
    name = name.to_s.singularize
    res = Avo::App.guess_resource(name)

    if res.present?
      @menu.items << Avo::Menu::Resource.new(resource: name, **args)
    end
  end
  alias_method :resources, :resource

  # Adds a dashboard
  def dashboard(dashboard, **args)
    @menu.items << Avo::Menu::Dashboard.new(dashboard: dashboard, **args)
  end

  # Adds a section
  def section(name = nil, **args, &block)
    @menu.items << Avo::Menu::Section.new(name: name, **args, items: self.class.parse_menu(&block).items)
  end

  # Adds a group
  def group(name = nil, **args, &block)
    @menu.items << Avo::Menu::Group.new(name: name, **args, items: self.class.parse_menu(&block).items)
  end

  # Add all the resources
  def all_resources(**args)
    Avo::App.resources_for_navigation.each do |res|
      resource res.model_class, **args
    end
  end

  # Add all the dashboards
  def all_dashboards(**args)
    Avo::App.dashboards_for_navigation.each do |dash|
      dashboard dash.id, **args
    end
  end

  # Add all the tools
  def all_tools(**args)
    Avo::App.tools_for_navigation.each do |tool|
      link tool.humanize, path: "#{root_path}/#{tool}"
    end
  end

  # Fetch the menu
  def build
    @menu
  end

  protected

  def root_path
    Avo::App.root_path
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
avo-2.9.2.pre1 lib/avo/menu/builder.rb
avo-2.9.1.pre3 lib/avo/menu/builder.rb
avo-2.9.1.pre2 lib/avo/menu/builder.rb
avo-2.9.1.pre1 lib/avo/menu/builder.rb
avo-2.9.0 lib/avo/menu/builder.rb
avo-2.8.0 lib/avo/menu/builder.rb
avo-2.7.1.pre.1 lib/avo/menu/builder.rb
avo-2.7.0 lib/avo/menu/builder.rb
avo-2.6.1.pre.2 lib/avo/menu/builder.rb
avo-2.6.1.pre.1 lib/avo/menu/builder.rb
avo-2.6.0 lib/avo/menu/builder.rb
avo-2.5.2.pre.7 lib/avo/menu/builder.rb
avo-2.5.2.pre.6 lib/avo/menu/builder.rb
avo-2.5.2.pre.5 lib/avo/menu/builder.rb
avo-2.5.2.pre.4 lib/avo/menu/builder.rb
avo-2.5.2.pre.3 lib/avo/menu/builder.rb
avo-2.5.2.pre.2 lib/avo/menu/builder.rb
avo-2.5.2.pre.1 lib/avo/menu/builder.rb
avo-2.5.1 lib/avo/menu/builder.rb
avo-2.5.0 lib/avo/menu/builder.rb