forked from activeadmin/activeadmin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completely refactored the menu system to work with i18n
Menu's are now generated on demand using the MenuBuilder instead of being created as the resources are registered. MenuItem can now store labels as procs, which allows us to properly i18n all the menu labels at runtime. ActiveAdmin::Resource's are now in charge of building their own MenuItem objects. The controller now stores the instance of the currently selected tab in @current_tag instead of just a string label.
- Loading branch information
Showing
25 changed files
with
385 additions
and
320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,3 @@ | |||
When /^I set my locale to "([^"]*)"$/ do |lang| | |||
I18n.locale = lang | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,42 +1,54 @@ | |||
module ActiveAdmin | module ActiveAdmin | ||
|
|||
class Menu | class Menu | ||
|
|
||
attr_accessor :children | |||
|
|||
def initialize | def initialize | ||
@items = [] | @children = Menu::ItemCollection.new | ||
|
|||
yield(self) if block_given? | yield(self) if block_given? | ||
end | |||
|
|||
def add(*args, &block) | |||
@items << MenuItem.new(*args, &block) | |||
end | end | ||
|
|
||
def [](name) | # Add a new MenuItem to the menu | ||
items.find{ |i| i.name == name } | # | ||
# Example: | |||
# menu = Menu.new | |||
# dash = MenuItem.new :label => "Dashboard" | |||
# menu.add dash | |||
# | |||
# Accepts as many menu items as you wish to add: | |||
# | |||
# menu = Menu.new | |||
# dash = MenuItem.new :label => "Dashboard" | |||
# admin = MenuItem.new :label => "Admin" | |||
# menu.add dash, admin | |||
# | |||
# @param [MenuItem] menu_items Add as many menu items as you pass in | |||
def add(*menu_items) | |||
menu_items.each do |menu_item| | |||
menu_item.parent = nil | |||
@children << menu_item | |||
end | |||
end | end | ||
|
|||
def items | def [](id) | ||
@items.sort | @children.find_by_id(id) | ||
end | end | ||
|
|||
def find_by_url(url) | def items | ||
recursive_find_by_url(items, url) | @children.sort | ||
end | end | ||
|
|
||
private | class ItemCollection < Array | ||
|
|
||
def recursive_find_by_url(collection, url) | def find_by_id(id) | ||
found = nil | id = MenuItem.generate_item_id(id) | ||
collection.each do |item| | find{ |i| i.id == id } | ||
if item.url == url | |||
found = item | |||
break | |||
else | |||
found = recursive_find_by_url(item.children, url) | |||
break if found | |||
end | |||
end | end | ||
found |
|
||
end | end | ||
|
|||
end | end | ||
|
|||
end | end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,75 @@ | |||
module ActiveAdmin | |||
|
|||
class MenuBuilder | |||
|
|||
def self.build_for_namespace(namespace) | |||
new(namespace).menu | |||
end | |||
|
|||
attr_reader :menu | |||
|
|||
def initialize(namespace) | |||
@namespace = namespace | |||
end | |||
|
|||
def menu | |||
@menu ||= build_menu | |||
end | |||
|
|||
private | |||
|
|||
def namespace | |||
@namespace | |||
end | |||
|
|||
def build_menu | |||
menu = Menu.new | |||
|
|||
add_dashboard_to_menu(menu) | |||
|
|||
namespace.resources.each do |resource| | |||
register_with_menu(menu, resource) if resource.include_in_menu? | |||
end | |||
|
|||
menu | |||
end | |||
|
|||
def add_dashboard_to_menu(menu) | |||
dashboard_path = namespace.root? ? :dashboard_path : "#{@namespace.name}_dashboard_path".to_sym | |||
|
|||
item = MenuItem.new :id => "dashboard", | |||
:label => proc{ I18n.t("active_admin.dashboard") }, | |||
:url => dashboard_path, | |||
:priority => 1 | |||
menu.add item | |||
end | |||
|
|||
# Does all the work of registernig a config with the menu system | |||
def register_with_menu(menu, resource) | |||
# The menu we're going to add this resource to | |||
add_to = menu | |||
|
|||
# Adding as a child | |||
if resource.parent_menu_item_name | |||
# Create the parent if it doesn't exist | |||
unless menu[resource.parent_menu_item_name] | |||
item = MenuItem.new(:label => resource.parent_menu_item_name, :url => "#", :id => resource.parent_menu_item_name) | |||
add_to.add(item) | |||
end | |||
|
|||
add_to = menu[resource.parent_menu_item_name] | |||
end | |||
|
|||
if add_to[resource.menu_item.id] | |||
existing = add_to[resource.menu_item.id] | |||
add_to.children.delete(existing) | |||
add_to.add(resource.menu_item) | |||
resource.menu_item.add(*existing.children) | |||
else | |||
add_to.add resource.menu_item | |||
end | |||
end | |||
|
|||
end | |||
|
|||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.