6

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?

ggtmtmgg
  • 147
  • 1
  • 8

4 Answers4

13

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.

alexb
  • 880
  • 11
  • 16
  • Fails with "ArgumentError at /admin/trips/1 Can't be in the context of nil." – Bramski Mar 16 '18 at 19:15
  • Just change @header to header and this works. The error message is rather obtuse. – Bramski Mar 16 '18 at 19:25
  • 2
    Read the above comment as "change `@head` to `head`". I encountered the ArgumentError crash when switching from v1.3.x to v1.4.3, and this change fixed it. – Hari Gopal Dec 10 '18 at 11:35
4

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.

Piers C
  • 2,880
  • 1
  • 23
  • 29
3

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
  • now webpacker is deprecated (or "retired" as they call it), so instead of coupling your app to it, better use one of the hacks from the other replies – jaynetics Mar 07 '22 at 20:11
0

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
armchairdj
  • 1,030
  • 11
  • 13