简体   繁体   中英

Can ActiveAdmin load javascript bundled by webpack? - Rails 5.1

I've upgraded from rails 4.2.6 to rails 5.1, and then started to use webpack.
All set up for using webpack have done, but I can't figure out how to load javascript files on the ActiveAdmin page.
ActiveAdmin loads app/assets/javascripts/active_admin.js.coffee by default.
Is there a way to load javascript files which is bundled by webpack on the ActiveAdmin page?

I'm a bit late, but I believe it's better to wrap the method rather than to completely override the class. Also, monkey patching Header will result in tags being rendered in div with id="header" . In order to render them in <head /> I did the following:

ActiveAdmin::Views::Pages::Base.class_eval do
  alias_method :original_build_active_admin_head, :build_active_admin_head

  def build_active_admin_head(*args, &block)
    original_build_active_admin_head(*args, &block)
    within @head do render '/custom_headers' end
  end
end

Put this file to config/initializers (so that it won't be reloaded every time in development mode resulting in infinite loop) folder and create a app/views/_custom_headers.html.erb file with whatever you need.

Webpack is not yet officially supported. The approach we are using for now is to monkey patch ActiveAdmin::Views::Header to include tags to load the generated output of Webpack, eg.

class ActiveAdmin::Views::Header < Component
  def build(namespace, menu)
    ...
    render "application/custom_header_tags"
  end
end

In our case our custom_header_tags.erb uses React On Rails but substitute whatever integration you prefer.

For new apps starting with Rails 6.0, Webpacker has become the default asset generator. You can opt-in to using Webpacker for ActiveAdmin assets as well by updating your configuration to turn on the use_webpacker option, either at installation time or manually.

at active_admin installation:

rails g active_admin:install --use_webpacker

manually add to config/initializers/active_admin.rb

ActiveAdmin.setup do |config|
  config.use_webpacker = true
end

And run the generator to get default Active Admin assets:

rails g active_admin:webpacker

The method signature for #build_active_admin_head continues to evolve. It no longer takes arguments as of ActiveAdmin v1.3.1.

Also, in addition to the method-aliasing and component-overriding methods described in other answers, there is one additional method for monkey-patching ActiveAdmin: module#prepend .

Here's how I override the ActiveAdmin body layout (to throw a big "staging" banner across the page in my staging environment) and head layout (to add calls to webpacker's javascript_pack_tag ):

module AdminPageLayoutOverride
  def build_page(*args)
    within @body do
      render "layouts/global/environment_banner"
    end

    super
  end

  def build_active_admin_head
    super

    within @head do
      render "admin/custom_script_tags"
    end
  end
end

ActiveAdmin::Views::Pages::Base.send :prepend, AdminPageLayoutOverride

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