From cee733dca1847b22ed64d5d6684e5332fdd2df35 Mon Sep 17 00:00:00 2001 From: Igor Fedoronchuk Date: Thu, 21 May 2026 10:24:24 +0200 Subject: [PATCH] fix: prepend view extensions at gem-require time, not via to_prepare `config.to_prepare` fires after the applications initializers, which is too late for consuming engines that require AA resource files from an initializer (a pattern AA permits via `config.load_paths`). Those resources see footer_data: silently no-op and the tfoot row never renders (or NameError when references to TableForExtension appear at registration time). Adopt the pattern used by sibling AA plugins (active_admin_sidebar, etc.): require "activeadmin" at the top of the gem's main file so `ActiveAdmin::Views` is guaranteed to exist, then eagerly prepend the extensions onto `TableFor` and `IndexAsTable`. The Engine class is kept (empty) so Rails still treats the gem as an engine on boot. --- lib/activeadmin_table_footer.rb | 12 ++++++++++++ lib/activeadmin_table_footer/engine.rb | 12 +++++------- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/activeadmin_table_footer.rb b/lib/activeadmin_table_footer.rb index 6dc4385..d05f63b 100644 --- a/lib/activeadmin_table_footer.rb +++ b/lib/activeadmin_table_footer.rb @@ -1,5 +1,12 @@ # frozen_string_literal: true +# Load ActiveAdmin first so `ActiveAdmin::Views::TableFor` and +# `IndexAsTable` exist by the time we prepend onto them. AA doesn't +# publish a load hook for itself (only `:active_admin_controller`), and +# `config.to_prepare` would fire too late for engines that require AA +# resources from an initializer. Sibling plugins (active_admin_sidebar, +# etc.) use the same pattern. +require "activeadmin" require "activeadmin_table_footer/version" require "activeadmin_table_footer/styles" @@ -22,3 +29,8 @@ def configure end require "activeadmin_table_footer/engine" if defined?(Rails) +require "activeadmin_table_footer/table_for_extension" +require "activeadmin_table_footer/index_as_table_extension" + +ActiveAdmin::Views::TableFor.prepend(ActiveadminTableFooter::TableForExtension) +ActiveAdmin::Views::IndexAsTable.prepend(ActiveadminTableFooter::IndexAsTableExtension) diff --git a/lib/activeadmin_table_footer/engine.rb b/lib/activeadmin_table_footer/engine.rb index 521fea8..9e743f1 100644 --- a/lib/activeadmin_table_footer/engine.rb +++ b/lib/activeadmin_table_footer/engine.rb @@ -3,13 +3,11 @@ require "rails/engine" module ActiveadminTableFooter + # Empty engine — kept so Rails recognizes this gem as an engine on + # boot. Patches are applied eagerly from + # `lib/activeadmin_table_footer.rb` so they are in place before AA + # resources are loaded (even when consuming engines `require` admin + # files from an initializer). class Engine < ::Rails::Engine - config.to_prepare do - require "activeadmin_table_footer/table_for_extension" - require "activeadmin_table_footer/index_as_table_extension" - - ActiveAdmin::Views::TableFor.prepend(ActiveadminTableFooter::TableForExtension) - ActiveAdmin::Views::IndexAsTable.prepend(ActiveadminTableFooter::IndexAsTableExtension) - end end end