Sha256: 809e34a19801ea4266b8ca2aac838642093b308709a4c4a26ac84cc3bd98052b

Contents?: true

Size: 1.54 KB

Versions: 4

Compression:

Stored size: 1.54 KB

Contents

module ActiveAdmin
  class MenuItem

    attr_accessor :name, :url, :priority, :parent, :display_if_block

    def initialize(name, url, priority = 10, options = {})
      @name, @url, @priority = name, url, priority
      @children = []
      @cached_url = {} # Stores the cached url in a hash to allow us to change it and still cache it

      @display_if_block = options.delete(:if)

      yield(self) if block_given? # Builder style syntax
    end

    def add(name, url, priority=10, options = {}, &block)
      item = MenuItem.new(name, url, priority, options, &block)
      item.parent = self
      @children << item
    end

    def children
      @children.sort
    end

    def parent?
      !parent.nil?
    end

    def dom_id
      name.downcase.gsub( " ", '_' ).gsub( /[^a-z0-9_]/, '' )
    end

    # Returns an array of the ancestory of this menu item
    # The first item is the immediate parent fo the item
    def ancestors
      return [] unless parent?
      [parent, parent.ancestors].flatten
    end

    # Returns the child item with the name passed in
    #    @blog_menu["Create New"] => <#MenuItem @name="Create New" >
    def [](name)
      @children.find{ |i| i.name == name }
    end

    def <=>(other)
      result = priority <=> other.priority
      result = name <=> other.name if result == 0
      result
    end

    # Returns the display if block. If the block was not explicitly defined
    # a default block always returning true will be returned.
    def display_if_block
      @display_if_block || lambda { |_| true }
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
activeadmin-0.4.4 lib/active_admin/menu_item.rb
activeadmin-0.4.3 lib/active_admin/menu_item.rb
activeadmin-0.4.2 lib/active_admin/menu_item.rb
activeadmin-0.4.1 lib/active_admin/menu_item.rb