如何自定义活动的管理布局?

时间:2021-08-27 19:19:02

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

我需要自定义活动的管理员布局,但我该怎么做呢?

3 个解决方案

#1


11  

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.

当在gem和rails应用程序中定义视图时,会提供Rails应用程序中定义的视图。这是一个逻辑优先事项。

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.

因此,如果您需要覆盖所有或部分有效的管理员视图,则必须在应用中复制这些视图并根据需要进行更改。

#2


28  

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:

以下内容将向标题添加特定于ie的样式表:

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.

显然是一个丑陋的解决方案。

#3


2  

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.

也许ActiveAdmin现在确实提供了一种更好的方法吗?我不知道。然而,在这个例子中,这是一个更清洁的补丁,在我的例子中将webpacker gems javascript_pack_tag添加到我的管理区域。

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

#1


11  

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.

当在gem和rails应用程序中定义视图时,会提供Rails应用程序中定义的视图。这是一个逻辑优先事项。

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.

因此,如果您需要覆盖所有或部分有效的管理员视图,则必须在应用中复制这些视图并根据需要进行更改。

#2


28  

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:

以下内容将向标题添加特定于ie的样式表:

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.

显然是一个丑陋的解决方案。

#3


2  

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.

也许ActiveAdmin现在确实提供了一种更好的方法吗?我不知道。然而,在这个例子中,这是一个更清洁的补丁,在我的例子中将webpacker gems javascript_pack_tag添加到我的管理区域。

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