简体   繁体   中英

How can I modify the “header” section in ActiveAdmin, in each request?

I'm a bit of a newbie to ActiveAdmin, and I'm trying to show big and bold warnings when some situations arise.

I have no idea how to do this after reading the documentation.

The best I managed to do was selectively register pages or not register them, based on some conditions. The objective of that was to add menu elements selectively. This worked in dev, but fails in production where code doesn't get reloaded in every request.

if warnings
  main_menu_title = fatal ? "Fatal Warnings" : "Warnings"

  ActiveAdmin.register_page main_menu_title do
    menu :priority => 99
  end
  ActiveAdmin.register_page "Warnings_job_count" do
    menu :label => "#{delayed_job_count} delayed jobs", :parent => main_menu_title, :priority => 1
  end
  ActiveAdmin.register_page "Warnings_job_errors" do
    menu :label => "#{delayed_job_error_count} delayed jobs with errors", :parent => main_menu_title, :priority => 2
  end
end

So, in short, the question is... Is it possible to declare a block somewhere, that will be executed in every request, in all pages, and that has the ability to either add elements to the menu, or somehow modify the header section?

Thank you very much!
Daniel

You can use something this this as explained here https://stackoverflow.com/a/7218598/807442

# app/admin/views/header_renderer.rb  
module ActiveAdmin
    module Views
      class HeaderRenderer
        def to_html
          title + custom_block + global_navigation + utility_navigation
        end

        def custom_block
          # Your custom block
        end
      end
    end
  end

You can also add items to the menu like this:

current_menu.add(main_menu_title , url_symbol, priority)

# For sub-items
main_menu = current_menu[main_menu_title]
main_menu.add(sub_item_title, url_symbol, priority)

However, if you add these, they will remain there in future requests. so you may need to also remove them, in each request, and then only add it necessary. To do that, add this:

module ActiveAdmin
  class Menu
    def remove(name)
      item = self[name]
      return if item.nil?
      @items.delete(item)
    end
  end
end

and you can remove by name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM