【发布时间】:2022-10-24 07:35:22
【问题描述】:
我们正在基于一些环境变量动态加载代码,效果非常好。
像这样的东西:
# User class
class User
include DynamicConcern
end
module DynamicConcern
extend ActiveSupport::Concern
included do
if "Custom::#{ENV["CUSTOMER_NAME"].camelize}::#{self.name}Concern".safe_constantize
include "Custom::#{ENV["CUSTOMER_NAME"].camelize}::#{self.name}Concern".constantize
end
end
end
# custom code
module Custom::Custom123::UserConcern
extend ActiveSupport::Concern
included do
...
end
end
多年来我们一直在使用它,它在模型中运行得非常好。几天前,我们尝试对控制器使用相同的方法,但意识到这种方法并没有 t 可以很好地处理继承,其中父类继承了关注点以及继承的类:
class ApplicationController < ActionController::Base
# this gets loaded and includes the right dynamic module
include DynamicConcern
end
class ShopController < ApplicationController
# this is NOT getting loaded again and skipped,
# since it has been loaded already in the parent controller
include DynamicConcern
end
有没有办法告诉rails它应该第二次包含/评估关注点,因为第二次它会有另一个包含另一个模块的类名?
我不是在寻找其他解决方案,因为我们的很多代码都是基于这种方法的,我认为可以在不重写所有内容的情况下解决这个问题。
谢谢!
【问题讨论】:
标签: ruby-on-rails ruby inheritance separation-of-concerns activesupport-concern