diff --git a/lib/active_admin/namespace.rb b/lib/active_admin/namespace.rb index 953b5b973d41..bb3f75a2a597 100644 --- a/lib/active_admin/namespace.rb +++ b/lib/active_admin/namespace.rb @@ -255,9 +255,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_generated = true + end config.controller.active_admin_config = config end @@ -265,7 +271,7 @@ 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) + parent.send(:remove_const, name) if parent.const_defined?(name, false) && resource.controller_generated # Remove circular references resource.controller.active_admin_config = nil @@ -285,7 +291,14 @@ def register_module # 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_generated = true + end config.controller.active_admin_config = config end diff --git a/lib/active_admin/page.rb b/lib/active_admin/page.rb index 362525c4c7e2..a6824566ad8d 100644 --- a/lib/active_admin/page.rb +++ b/lib/active_admin/page.rb @@ -71,6 +71,8 @@ def controller_name [namespace.module_name, camelized_resource_name + "Controller"].compact.join('::') end + attr_accessor :controller_generated + # Override from `ActiveAdmin::Resource::Controllers` def route_uncountable? false diff --git a/lib/active_admin/resource/controllers.rb b/lib/active_admin/resource/controllers.rb index 512f34d25e4b..e80a57452f5a 100644 --- a/lib/active_admin/resource/controllers.rb +++ b/lib/active_admin/resource/controllers.rb @@ -9,6 +9,8 @@ def controller_name [namespace.module_name, resource_name.plural.camelize + "Controller"].compact.join('::') end + attr_accessor :controller_generated + # Returns the controller for this config def controller @controller ||= controller_name.constantize