Permalink
Browse files

UtlityNavigation removed, uses menu system

* MenuItem#label now accepts a proc evaluated in view context
* MenuItem#dom_id of '#' is now dispalyed instead of hidden
* DSL#display_menu now DSL#navigation_menu
* Docs updated on how to override utility nav
  • Loading branch information...
1 parent 59e6d4f commit 452956eccf77ea368b05902a3d0d8020152a7aaa @macfanatic macfanatic committed Mar 5, 2013
@@ -42,7 +42,7 @@
height: 20px
}
- ul#tabs {
+ ul.tabs {
display: inline-block;
height: 100%;
margin: 0;
@@ -128,10 +128,13 @@
display: inline-block;
margin: 0;
padding: 0;
- span, a { margin-left: 10px; }
a { text-decoration: none; }
a:hover { color: #fff; }
+
+ li {
+ display:inline;
+ }
}
}
@@ -40,7 +40,7 @@ h3 {
#header {
float: left;
- #tabs, #utility_nav {
+ #tabs, .tabs, #utility_nav {
display: none;
}
@@ -114,3 +114,16 @@ You can also disable comments for a specific resource:
ActiveAdmin.register Post do
config.comments = false
end
+
+## Utility Navigation
+
+The "utility navigation" shown at the top right when logged in by default shows the current user email address and a link to "Log Out". However, the utility navigation is just like any other menu in the system, so you can provide your own menu to be rendered in place if you like.
+
+ ActiveAdmin.setup do |config|
+ config.namespace :admin do |admin|
+ admin.build_menu :utility_navigation do |menu|
+ menu.add label: "ActiveAdmin.info", url: "http://www.activeadmin.info", html_options: { target: :blank }
+ admin.add_logout_button_to_menu menu # can also pass priority & html_options for link_to to use
+ end
+ end
+ end
@@ -111,7 +111,7 @@ can customize the menu for the namespace within the Active Admin initializer.
menu.add :label => "The Application", :url => "/", :priority => 0
menu.add :label => "Sites" do |sites|
- sites.add :label => "Google", :url => "http://google.com"
+ sites.add :label => "Google", :url => "http://google.com", html_options: { target: :blank }
sites.add :label => "Facebook", :url => "http://facebook.com"
sites.add :label => "Github", :url => "http://github.com"
end
@@ -233,12 +233,12 @@ seperate menu which you can use if you so wish. To use:
ActiveAdmin.register Ticket do
belongs_to :project
- display_menu :project
+ navigation_menu :project
end
ActiveAdmin.register Milestone do
belongs_to :project
- display_menu :project
+ navigation_menu :project
end
Now, when you navigate to the tickets section, the global navigation will
@@ -250,7 +250,7 @@ different menus, say perhaps on user permissions or level. For example:
ActiveAdmin.register Ticket do
belongs_to: :project
- display_menu do
+ navigation_menu do
authorized?(:manage, SomeResource) ? :project : :restricted_menu
end
end
@@ -55,7 +55,7 @@ Feature: Belongs To
ActiveAdmin.register User
ActiveAdmin.register Post do
belongs_to :user
- display_menu :user
+ navigation_menu :user
end
"""
When I go to the last author's posts
@@ -11,7 +11,7 @@ module Menu
protected
def current_menu
- active_admin_config.display_menu
+ active_admin_config.navigation_menu
end
# Set's @current_tab to be name of the tab to mark as current
@@ -121,7 +121,7 @@ def menu(options = {})
config.menu_item_options = options
end
- # Set the name of the menu to display. This is mainly used in conjuction with the
+ # Set the name of the navigation menu to display. This is mainly used in conjuction with the
# `#belongs_to` functionality.
#
# @param [Symbol] menu_name The name of the menu to display as the global navigation
@@ -130,8 +130,8 @@ def menu(options = {})
# Pass a block returning the name of a menu you want rendered for the request, being
# executed in the context of the controller
#
- def display_menu(menu_name=nil, &block)
- config.display_menu_name = menu_name || block
+ def navigation_menu(menu_name=nil, &block)
+ config.navigation_menu_name = menu_name || block
end
# Rewrite breadcrumb links.
@@ -76,7 +76,12 @@ def children
end
def normalize_id(string)
- string.to_s.downcase.gsub(" ", "_")
+ case string
+ when Proc
+ string
+ else
+ string.to_s.downcase.gsub(" ", "_")
+ end
end
end
@@ -26,6 +26,10 @@ def clear!
@built = false
end
+ def exists?(menu_name)
+ @menus.keys.include? menu_name
+ end
+
def fetch(menu_name)
build_menus!
@@ -3,7 +3,7 @@ module ActiveAdmin
class MenuItem
include Menu::MenuNode
- attr_accessor :id, :label, :url, :priority, :display_if_block
+ attr_accessor :id, :label, :url, :priority, :display_if_block, :html_options
attr_reader :parent
# Build a new menu item
@@ -31,35 +31,35 @@ class MenuItem
# A block for the view to call to decide if this menu item should be displayed.
# The block should return true of false
#
+ # @option options [Hash] :html_options
+ # A hash of options to pass to `link_to` when rendering the item
+ #
# @param [ActiveAdmin::MenuItem] parent The parent menu item of this item
#
def initialize(options = {}, parent = nil)
- @label = options[:label]
- @id = normalize_id(options[:id] || label)
- @url = options[:url] || "#"
- @priority = options[:priority] || 10
- @parent = parent
+ @label = options[:label]
+ @id = normalize_id(options[:id] || label)
+ @url = options[:url] || "#"
+ @priority = options[:priority] || 10
+ @html_options = options[:html_options] || {}
+ @parent = parent
@display_if_block = options[:if]
yield(self) if block_given? # Builder style syntax
end
- def label
- case @label
- when Proc
- @label.call
- else
- @label.to_s
- end
- end
-
def parent?
!parent.nil?
end
def dom_id
- id.gsub( " ", '_' ).gsub( /[^a-z0-9_]/, '' )
+ case id
+ when Proc
+ id
+ else
+ id.to_s.gsub( " ", '_' ).gsub( /[^a-z0-9_]/, '' )
+ end
end
# Returns an array of the ancestory of this menu item
@@ -71,7 +71,7 @@ def ancestors
def <=>(other)
result = priority <=> other.priority
- result = label <=> other.label if result == 0
+ result = label.to_s <=> other.label.to_s if result == 0
result
end
@@ -136,6 +136,27 @@ def build_menu(name = DEFAULT_MENU, &block)
end
end
+ # Add the default logout button to the menu, using the ActiveAdmin configuration settings
+ #
+ # @param [ActiveAdmin::MenuItem] menu The menu to add the logout link to
+ # @param [Fixnum] priority Override the default priority of 100 to position the logout button where you want
+ # @param [Hash] html_options An options hash to pass along to link_to
+ #
+ # @returns [void]
+ def add_logout_button_to_menu(menu, priority=100, html_options={})
+ if logout_link_path
+ logout_method = logout_link_method || :get
+ menu_options = {
+ label: I18n.t('active_admin.logout'),
+ url: proc{ render_or_call_method_or_proc_on self, active_admin_namespace.logout_link_path },
+ priority: priority,
+ html_options: { method: logout_method }.merge(html_options),
+ if: proc{ current_active_admin_user? }
+ }
+ menu.add menu_options
+ end
+ end
+
protected
def build_menu_collection
@@ -145,12 +166,24 @@ def build_menu_collection
# Support for deprecated dashboards...
Dashboards.add_to_menu(self, menus.menu(DEFAULT_MENU))
+ # Build the default utility navigation
+ build_default_utility_nav
+
resources.each do |resource|
resource.add_to_menu(@menus)
end
end
end
+ # Builds the default utility navigation in top right header with current user & logout button
+ def build_default_utility_nav
+ return if @menus.exists? :utility_navigation
+ @menus.menu :utility_navigation do |menu|
+ menu.add label: proc{ display_name current_active_admin_user }, url: '#', id: 'current_user', if: proc{ current_active_admin_user? }
+ add_logout_button_to_menu menu
+ end
+ end
+
# Either returns an existing Resource instance or builds a new
# one for the resource and options
def find_or_build_resource(resource_class, options)
@@ -7,7 +7,7 @@ module Menu
# call #menu(false)
def menu_item_options=(options)
if options == false
- @display_menu = false
+ @navigation_menu = false
@menu_item_options = {}
else
self.menu_item_menu_name = options[:menu_name]
@@ -30,22 +30,22 @@ def default_menu_options
}
end
- def display_menu_name=(menu_name)
- @display_menu_name = menu_name
+ def navigation_menu_name=(menu_name)
+ @navigation_menu_name = menu_name
end
- def display_menu_name
- @display_menu_name ||= DEFAULT_MENU
- case @display_menu_name
+ def navigation_menu_name
+ @navigation_menu_name ||= DEFAULT_MENU
+ case @navigation_menu_name
when Proc
- controller.instance_eval(&@display_menu_name).to_sym
+ controller.instance_eval(&@navigation_menu_name).to_sym
else
- @display_menu_name
+ @navigation_menu_name
end
end
- def display_menu
- namespace.fetch_menu(display_menu_name)
+ def navigation_menu
+ namespace.fetch_menu(navigation_menu_name)
end
def menu_item_menu_name=(menu_name)
@@ -68,7 +68,7 @@ def menu_item
# Should this resource be added to the menu system?
def include_in_menu?
- @display_menu != false
+ @navigation_menu != false
end
end
@@ -6,7 +6,6 @@ class ViewFactory < AbstractViewFactory
# Register Helper Renderers
register :global_navigation => ActiveAdmin::Views::TabbedNavigation,
:site_title => ActiveAdmin::Views::SiteTitle,
- :utility_navigation => ActiveAdmin::Views::UtilityNav,
:action_items => ActiveAdmin::Views::ActionItems,
:title_bar => ActiveAdmin::Views::TitleBar,
:header => ActiveAdmin::Views::Header,
@@ -7,6 +7,7 @@ def build(namespace, menu)
@namespace = namespace
@menu = menu
+ @utility_menu = @namespace.fetch_menu(:utility_navigation)
build_site_title
build_global_navigation
@@ -19,11 +20,11 @@ def build_site_title
end
def build_global_navigation
- insert_tag view_factory.global_navigation, @menu, :class => 'header-item'
+ insert_tag view_factory.global_navigation, @menu, :class => 'header-item tabs'
end
def build_utility_navigation
- insert_tag view_factory.utility_navigation, @namespace
+ insert_tag view_factory.global_navigation, @utility_menu, :id => "utility_nav", :class => 'header-item tabs'
end
end
Oops, something went wrong.

0 comments on commit 452956e

Please sign in to comment.