Sha256: 6bb16adc823c6edc02394423f0aee6ec7632b91d225c6a2253c924c9f96396ed

Contents?: true

Size: 1.65 KB

Versions: 5

Compression:

Stored size: 1.65 KB

Contents

module ActiveAdmin
  class MenuItem
    
    # Use this to get to the routes
    include Rails.application.routes.url_helpers
    
    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
    
    def url
      case @url
      when Symbol
        generated = send(@url) # Call the named route
      else
        generated = @url
      end
      @cached_url[@url] ||= generated
    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

  end  
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
activeadmin-0.2.2 lib/active_admin/menu_item.rb
activeadmin-0.2.1 lib/active_admin/menu_item.rb
activeadmin-0.2.0 lib/active_admin/menu_item.rb
activeadmin-0.1.1 lib/active_admin/menu_item.rb
activeadmin-0.1.0 lib/active_admin/menu_item.rb