Skip to content

Commit

Permalink
If a controller is defined dynamically mark it as disposable, otherwi…
Browse files Browse the repository at this point in the history
…se don't remove it during namespace unload!
  • Loading branch information
varyonic committed Nov 16, 2023
1 parent 8ee52b3 commit a58f2cf
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions lib/active_admin/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,27 @@ 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

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

Expand Down

0 comments on commit a58f2cf

Please sign in to comment.