joseglego
joseglego

Reputation: 2109

ActiveAdmin, navigation_menu with custom order

I'm using Runy On Rails with ActiveAdmin gem. In my admin configuration I have an AdminPage (Clients) which shouldn't be in the usual menu. It should be in the navigatio menu of one related AdminPage (Company). In this way, I can see the clients of the company.

So, in my app/admin/people.rb I have:

navigation_menu :company

Inside this menu, I have other elements too, and everything work like a charm. Now, I want to apply custom order over these items. But when I use priority (as I use in the usual menu items in the application) my model lose its navigation_menu.

So, I can't have a sub-menu with custom priority.

Can I modify the priority/order on a navigation_menu?

Upvotes: 1

Views: 246

Answers (1)

felbalart
felbalart

Reputation: 36

I think this indeed is an ActiveAdmin issue.

I managed to workaround using the monkey patch described below of add_to_menu method.

It's not the most elegant, but it does the job while the issue gets fixed in AA.

Imagine you want 2 parent menu items Clients and Product, and each one of these has 2 children. All these admin pages scoped within navigation_menu :company, for example:

Clients
-> 1. ClientAddresses
-> 2. ClientProfiles

Products
-> 1. ProductCategories
-> 2. ProductCosts

So the monkey patch goes like:

# Put this in app/admin/components/menu.rb
module ActiveAdmin
  class Resource
    module Menu
      def add_to_menu(menu_collection)
        add_parent_options if forced_parents.keys.include? resource_name.plural.to_sym
        if include_in_menu?
          @menu_item = menu_collection.add navigation_menu_name, menu_item_options
        end
      end

      def add_parent_options
        priority, parent = forced_parents[resource_name.plural.to_sym]
        @menu_item_options[:priority] = priority
        @menu_item_options[:parent] = I18n.t("active_admin.menus.#{parent}")
      end

      def forced_parents
        @forced_parents ||=
          {
            client_addresses: [1, :clients],
            client_profiles: [2, :clients],
            product_categories: [1, :products],
            product_costs: [2, :products],
          }
      end
    end
  end

Good luck

Upvotes: 2

Related Questions