Skip to content

Commit

Permalink
Selectively unload only controllers that were dynamically defined.
Browse files Browse the repository at this point in the history
  • Loading branch information
varyonic committed Nov 6, 2023
1 parent 643bad0 commit 41d7d04
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/active_admin/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,17 +255,23 @@ 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

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
Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions lib/active_admin/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/active_admin/resource/controllers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 41d7d04

Please sign in to comment.