Skip to content

Instantly share code, notes, and snippets.

@skuroki
Last active November 22, 2016 05:24
Show Gist options
  • Save skuroki/ddd7d1cd75d829e2c182 to your computer and use it in GitHub Desktop.
Save skuroki/ddd7d1cd75d829e2c182 to your computer and use it in GitHub Desktop.
ActiveAdminでメニューの並び順を1箇所で管理する ref: http://qiita.com/skuroki@github/items/0a9db8a9e58f55202040
MENU_ITEMS = [
[:item, 'Dashboard', {label: proc{ I18n.t("active_admin.dashboard") }}],
[:item, 'Item', {}],
[:item, 'Skill', {label: '特技検索'}],
[:item, 'Evolution', {label: '進化検索'}],
[:category, 'master',
[
['Element', {}],
['BoxKind', {}],
]
],
[:item, 'Comment', {}],
]
MENU_ITEM_EXCLUDES = %w()
def inverted_menu_items
MENU_ITEMS.each_with_object([]).with_index do |(source_item, inverted), index|
case source_item.first
when :item
_, page, args = source_item
inverted << [page, args.merge(priority: index)]
when :category
_, category, children = source_item
inverted << [category, {label: I18n.t("active_admin.menu.#{category}"), priority: index}]
children.each_with_index do |category_item, index2|
page, args = category_item
inverted << [page, args.merge(parent: I18n.t("active_admin.menu.#{category}"), priority: index2)]
end
end
end
end
def check_index_and_add_priority_and(name, &block)
index = inverted_menu_items.map(&:first).index(name)
if index
args = inverted_menu_items[index][1]
block.call(args)
elsif !(MENU_ITEM_EXCLUDES.include? name)
raise "#{name} is NOT placed into the menu"
end
end
ActiveAdmin.application.namespace(false).build_menu do |menu|
MENU_ITEMS.select {|i| i.first == :category}.each do |item|
name = item[1]
check_index_and_add_priority_and(name) do |args|
menu.add args
end
end
end
class ActiveAdmin::DSL
def allocate_to_menu
name = config.resource_name.name
check_index_and_add_priority_and(name) do |args|
menu args
end
end
end
module MenuAllocator
def register(*args, &block)
super *args do
allocate_to_menu
self.instance_eval &block
end
end
def register_page(*args, &block)
super *args do
allocate_to_menu
self.instance_eval &block
end
end
end
module ActiveAdmin
class Namespace
prepend MenuAllocator
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment