如何在所有Active Admin页面控制器中自动包含一个关注点?

huangapple go评论64阅读模式
英文:

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?

英文:

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?

答案1

得分: 0

只需在文件./config/initializers/active_admin.rb的末尾添加以下行:

ActiveSupport.on_load(:active_admin_controller) do
  include ActivityHelper
end
英文:

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

huangapple
  • 本文由 发表于 2023年2月27日 16:39:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定