adhu
adhu

Reputation: 63

How to automatically include a concern in all Active Admin Page controllers?

I have the following concern:

./app/controllers/concerns/activity_helper.rb

module ActivityHelper
  extend ActiveSupport::Concern

  included do
    before_action :my_method, except: %i[update destroy]
  end

  def my_method
    ...
  end
end

Currently i am including this concern in several Active Admin page controllers manually like so:

./app/admin/dashboard.rb

controller do
  include ActivityHelper
end

I already tried the following but it does not work without giving any error:

  1. I created a new module in ./app/controllers/concerns/active_admin_controller_concern.rb:
module ActiveAdminControllerConcern
   extend ActiveSupport::Concern

   included do
       controller do
          include ActivityHelper
       end
   end
end
  1. I added the following method and config in ./config/initializers/active_admin.rb:
def add_active_admin_controller_concern
  ActiveAdmin::ResourceDSL.send(:include, ActiveAdminControllerConcern)
end
ActiveAdmin.setup do |config|
  config.before_action :add_active_admin_controller_concern
end

How to automatically include only the module ActivityHelper in all Active Admin page controllers?

Upvotes: 1

Views: 486

Answers (1)

adhu
adhu

Reputation: 63

You don't need the file ./app/controllers/concerns/active_admin_controller_concern.rb

Just add the following line at the end of the file ./config/initializers/active_admin.rb

ActiveSupport.on_load(:active_admin_controller) do
  include ActivityHelper
end

Upvotes: 0

Related Questions