
Allows ActiveAdmin menus to be managed in tree format.
This is a wrapper library for managing ActiveAdmin's menu structure in a simple yaml format, and automatically setting the priority and parent.
Motivation
I felt that the normal way of specifying menus in ActiveAdmin is inconvenient when changing it in an application.
Specifically, the two points are as follows:
- Order control by
priority.
- To change the order of the menu, we need to change the priority of each resource one by one.
- Managing hierarchical relationships with
parent.
- To create a hierarchy, we need to create a parent menu with initializer, and then specify the parent menu from each resource.
The configuration is scattered and burdensome to change.
This gem solves these problems.
Installation
Add this line to your application's Gemfile:
gem 'activeadmin-menu_tree'
And then execute:
$ bundle install
Usage
Only 2 steps
In your Rails project with ActiveAdmin.
Write the configuration in a yaml file.
activeadmin:
menu_tree:
- id: Dashboard
- label: Admin
children:
- id: AdminUser
label: Admin Users
- id: Comment
label: Admin Comments
Load it in initializer.
ActiveAdmin::MenuTree.setup do |config|
config.menu_tree = YAML.load_file(Rails.root.join("config/activeadmin-menu_tree.yml"))["activeadmin"]["menu_tree"]
end
You can use menu_tree instead of menu in ActiveAdmin Resource.
ActiveAdmin.register AdminUser do
menu_tree
end
ActiveAdmin.register_page "Dashboard" do
menu_tree label: proc { I18n.t("active_admin.dashboard") }
end
Other ways to load configuration
It is also possible to simply use hash instead of yaml.
If you want to use dynamic specification using proc in menu_tree (instead of in ActiveAdmin Resource), you may want to use this one.
ActiveAdmin::MenuTree.setup do |config|
config.menu_tree = [
{ id: "Dashboard", label: proc { I18n.t("active_admin.dashboard") } },
{
label: "Foo",
if: proc { "Something dynamic" },
children: [
{ id: "Bar" },
{ id: "Baz" }
]
}
]
end
If your project uses the config gem, you can use it by converting it to hash with to_hash as follows:
ActiveAdmin::MenuTree.setup do |config|
config.menu_tree = Settings.activeadmin.menu_tree.map(&:to_hash)
end
activeadmin:
menu_tree:
Or you can use other configuration gems like global gem by converting them to hash as well.
Full configuration example
activeadmin:
menu_tree:
- id: Dashboard
- id: Product
- label: User Info
children:
- id: User
- id: Profile
- label: Admin
children:
- id: AdminUser
label: Admin Users
- id: Comment
label: Admin Comments
- label: Others
children:
- id: Foo
- id: Bar
- label: Example Site
url: 'https://example.com'
html_options:
target: blank
- label: Lorem
children:
- label: ipsum
children:
- label: dolor
children:
- label: sit
children:
- label: amet
url: 'https://wikipedia.org/wiki/Lorem_ipsum'
html_options:
target: blank
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/shuuuuun/activeadmin-menu_tree.
License
The gem is available as open source under the terms of the MIT License.