Sha256: 57b21b6395fbddd83ed9af758b734dc012da6409e3625716450915fb9609af86

Contents?: true

Size: 1.87 KB

Versions: 12

Compression:

Stored size: 1.87 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
    
    # 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

12 entries across 12 versions & 4 rubygems

Version Path
andrewroth_activeadmin-0.3.4.3 lib/active_admin/menu_item.rb
andrewroth_activeadmin-0.3.4.2 lib/active_admin/menu_item.rb
andrewroth_activeadmin-0.3.4.1 lib/active_admin/menu_item.rb
andrewroth_activeadmin-0.3.4 lib/active_admin/menu_item.rb
activeadmin-0.3.4 lib/active_admin/menu_item.rb
activeadmin-0.3.3 lib/active_admin/menu_item.rb
activeadmin-0.3.2 lib/active_admin/menu_item.rb
activeadmin-0.3.1 lib/active_admin/menu_item.rb
activeadmin-0.3.0 lib/active_admin/menu_item.rb
nsm-activeadmin-0.2.2 lib/active_admin/menu_item.rb
saulolso-activeadmin-0.2.2.1 lib/active_admin/menu_item.rb
saulolso-activeadmin-0.2.2 lib/active_admin/menu_item.rb