From 708d2610345023526375c48d49d95ee042c6c8ac Mon Sep 17 00:00:00 2001 From: Doug Flowers <19670789+dtflowers@users.noreply.github.com> Date: Mon, 22 Jan 2024 16:13:56 -0600 Subject: [PATCH] refactor monkeypatch to eager load routes for command --- lib/routes_lazy_routes/application.rb | 10 ++++++++++ lib/routes_lazy_routes/command/routes_command.rb | 14 -------------- lib/routes_lazy_routes/railtie.rb | 3 +-- 3 files changed, 11 insertions(+), 16 deletions(-) delete mode 100644 lib/routes_lazy_routes/command/routes_command.rb diff --git a/lib/routes_lazy_routes/application.rb b/lib/routes_lazy_routes/application.rb index 0152732..664c8e4 100644 --- a/lib/routes_lazy_routes/application.rb +++ b/lib/routes_lazy_routes/application.rb @@ -21,5 +21,15 @@ def load_tasks(*) load "#{__dir__}/tasks/routes_lazy_routes.rake" end end + + # A monkey-patch that eager loads routes before the routes command is executed by prepending this module to the + # require_enviornment! method that's called when boot_application! is executed performing console commands. + module RoutesCommandEagerLoader + def require_environment! + super + + RoutesLazyRoutes.eager_load! + end + end end end diff --git a/lib/routes_lazy_routes/command/routes_command.rb b/lib/routes_lazy_routes/command/routes_command.rb deleted file mode 100644 index fa67223..0000000 --- a/lib/routes_lazy_routes/command/routes_command.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -module RoutesLazyRoutes - module Command - module RoutesCommand - # A monkey-patch that eager_loads the routes right before running the `rails routes` command. - def require_environment! - super - - RoutesLazyRoutes.eager_load! - end - end - end -end diff --git a/lib/routes_lazy_routes/railtie.rb b/lib/routes_lazy_routes/railtie.rb index ca2e5bf..7596657 100644 --- a/lib/routes_lazy_routes/railtie.rb +++ b/lib/routes_lazy_routes/railtie.rb @@ -8,8 +8,7 @@ class Railtie < ::Rails::Railtie Rails::Application.prepend RoutesLazyRoutes::Application::TaskLoader if defined? Rails::Command::RoutesCommand - require_relative 'command/routes_command' - Rails::Command::RoutesCommand.prepend RoutesLazyRoutes::Command::RoutesCommand + Rails::Application.prepend RoutesLazyRoutes::Application::RoutesCommandEagerLoader end initializer :routes_lazy_routes, before: :add_routing_paths do