Sha256: 8d773b06fb6cf4878637219d69b2797fd08fbe3e68027dedfd96486c6da68ab7

Contents?: true

Size: 1.8 KB

Versions: 57

Compression:

Stored size: 1.8 KB

Contents

module ExpressAdmin
  # Provide menus for Express Admin addon engines.
  #
  # To use:
  #
  # 1) include ExpressAdmin::Menu::Loader in your engine.rb
  # 2) add a config/menu.yml defining the menu structure
  #
  # Example:
  #
  #     title: 'Blog'
  #     path: 'express_blog.admin_blog_posts_path'
  #     items:
  #       -
  #         title: 'Posts'
  #         path: 'express_blog.admin_blog_posts_path'
  #       -
  #         title: 'Categories'
  #         path: 'express_blog.admin_blog_categories_path'
  module Menu

    # Return the top level MenuItem for the addon or defined in the supplied path.
    #
    # Accepts an addon_name such as :express_admin or a path to a yaml file
    # containing a menu definition.
    def self.[](addon_name)
      @menus ||= {}
      @menus[addon_name.to_sym] ||= begin
        menu_yml_path =
          if addon_name.eql?('admin')
            File.join(Rails.root, 'config', 'menu.yml')
          else
            addon_path = Gem.loaded_specs[addon_name].full_gem_path if addon_name.to_s.match(/^\w+$/)
            File.join(addon_path, 'config', 'menu.yml')
          end
        from(menu_yml_path)
      end
    end

    def self.from(yaml_path)
      raise "unable to locate #{yaml_path}" unless File.exists?(yaml_path)
      MenuItem.new YAML.load_file(yaml_path).with_indifferent_access
    end


    class MenuItem
      attr :title, :path, :position, :items
      def initialize(hash)
        @title = hash[:title]
        @path = hash[:path]
        @position = hash[:position] || 99
        @items = (hash[:items]||[]).map {|item| MenuItem.new(item)}
      end
    end

    module Loader
      def self.included(base)
        class << base
          def addon_name
            self.to_s.split('::')[-2].underscore.to_sym
          end
        end
      end
    end

  end
end

Version data entries

57 entries across 57 versions & 1 rubygems

Version Path
express_admin-1.7.27 lib/express_admin/menu.rb
express_admin-1.7.26 lib/express_admin/menu.rb
express_admin-1.7.25 lib/express_admin/menu.rb
express_admin-1.7.24 lib/express_admin/menu.rb
express_admin-1.7.23 lib/express_admin/menu.rb
express_admin-1.7.22 lib/express_admin/menu.rb
express_admin-1.7.21 lib/express_admin/menu.rb
express_admin-1.7.20 lib/express_admin/menu.rb
express_admin-1.7.19 lib/express_admin/menu.rb
express_admin-1.7.18 lib/express_admin/menu.rb
express_admin-1.7.17 lib/express_admin/menu.rb
express_admin-1.7.16 lib/express_admin/menu.rb
express_admin-1.7.16.pre1 lib/express_admin/menu.rb
express_admin-1.7.15 lib/express_admin/menu.rb
express_admin-1.7.14 lib/express_admin/menu.rb
express_admin-1.7.13 lib/express_admin/menu.rb
express_admin-1.7.12 lib/express_admin/menu.rb
express_admin-1.7.11 lib/express_admin/menu.rb
express_admin-1.7.10 lib/express_admin/menu.rb
express_admin-1.7.9 lib/express_admin/menu.rb