From a58f2cffaf662db69ea26e26991fcb83eb80b8c7 Mon Sep 17 00:00:00 2001 From: Piers Chambers Date: Thu, 16 Nov 2023 10:57:24 -0500 Subject: [PATCH] If a controller is defined dynamically mark it as disposable, otherwise don't remove it during namespace unload! --- lib/active_admin/namespace.rb | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/active_admin/namespace.rb b/lib/active_admin/namespace.rb index 86ca65b6bec..c44de2194c1 100644 --- a/lib/active_admin/namespace.rb +++ b/lib/active_admin/namespace.rb @@ -246,9 +246,15 @@ def build_page(name, options) resources.add Page.new(self, name, options) end - # TODO: replace `eval` with `Class.new` def register_page_controller(config) - eval "class ::#{config.controller_name} < ActiveAdmin::PageController; end" + if Object.const_defined?("::#{config.controller_name}") + unless config.controller.ancestors.include?(ActiveAdmin::PageController) + raise "#{config.controller_name} not a PageController" + end + else + eval "class ::#{config.controller_name} < ActiveAdmin::PageController; end" + config.controller.const_set('DISPOSABLE', true) + end config.controller.active_admin_config = config end @@ -256,7 +262,11 @@ def unload_resources! resources.each do |resource| parent = (module_name || 'Object').constantize name = resource.controller_name.split('::').last - parent.send(:remove_const, name) if parent.const_defined?(name, false) + if parent.const_defined?(name, false) + if resource.controller.const_defined?(:DISPOSABLE) + parent.send(:remove_const, name) + end + end # Remove circular references resource.controller.active_admin_config = nil @@ -274,9 +284,15 @@ def register_module end end - # TODO: replace `eval` with `Class.new` def register_resource_controller(config) - eval "class ::#{config.controller_name} < ActiveAdmin::ResourceController; end" + if Object.const_defined?("::#{config.controller_name}") + unless config.controller.ancestors.include?(ActiveAdmin::ResourceController) + raise "#{config.controller_name} not a ResourceController" + end + else + eval "class ::#{config.controller_name} < ActiveAdmin::ResourceController; end" + config.controller.const_set(:DISPOSABLE, true) + end config.controller.active_admin_config = config end