How can I customize the active admin layout?

2019-03-09 18:55发布

I need to customize the active admin layout, but how can I do it?

3条回答
对你真心纯属浪费
2楼-- · 2019-03-09 19:34

The active admin layout is not actually defined as a layout file, but is generated programatically. Placing a custom layout in the layout directory will therefore not actually override the default layout.

You can, however, monkey-patch or duck-punch the active admin layout methods inside your application.

The following will add an ie-specific stylesheet to the header:

module ActiveAdmin
  module Views
    module Pages
      class Base < Arbre::HTML::Document

        alias_method :original_build_active_admin_head, :build_active_admin_head unless method_defined?(:original_build_active_admin_head)

        def build_active_admin_head
          within @head do
            meta :"http-equiv" => "Content-type", :content => "text/html; charset=utf-8"
            insert_tag Arbre::HTML::Title, [title, active_admin_application.site_title].join(" | ")
            active_admin_application.stylesheets.each do |path|
              link :href => stylesheet_path(path), :media => "screen", :rel => "stylesheet", :type => "text/css"
            end
            active_admin_application.javascripts.each do |path|
              script :src => javascript_path(path), :type => "text/javascript"
            end
            text_node csrf_meta_tag
            text_node "<!--[if lt IE 7]>
            <link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"admin_ie7.css\ />
            <![endif] -->".html_safe
          end
        end

      end
    end
  end
end

Clearly an ugly solution.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-03-09 19:36

When a view is defined in a gem AND in the rails app, the one defined in the Rails app is served. It's a logic priority.

So if you need to override all or some active admin views, you'll have to copy these in your app and change them as you desire.

查看更多
叼着烟拽天下
4楼-- · 2019-03-09 19:53

Maybe ActiveAdmin does provide a nicer way to do this by now? I don't know. However here would be an example for a bit cleaner patch for that situation, in my example to add the webpacker gems javascript_pack_tag to my admin area.

module MyApp
  module ActiveAdmin
    module Views
      module Pages
        module BaseExtension
          def build_active_admin_head
            super
            within @head do
              text_node(javascript_pack_tag('application'))
            end
          end
        end
      end
    end
  end
end

class ActiveAdmin::Views::Pages::Base < Arbre::HTML::Document
  prepend MyApp::ActiveAdmin::Views::Pages::BaseExtension
end
查看更多
登录 后发表回答