From 092dda3362788f73a850e3cce78e2fb478a58a88 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Mon, 16 Dec 2024 15:14:02 +0100 Subject: [PATCH] Fix setting custom preview_paths Setting `preview_paths` to anything else than `test/components/previews` is broken, because we mutate `ActiveSupport::Dependencies.autoload_paths`. From the Rails guides: > Please do not mutate ActiveSupport::Dependencies.autoload_paths; > the public interface to change autoload paths is config.autoload_paths. Fixes #365 --- docs/CHANGELOG.md | 4 ++++ lib/view_component/engine.rb | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 9760606aa..810c6781a 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -10,6 +10,10 @@ nav_order: 5 ## main +* Fix setting custom `preview_paths`. + + *Thomas von Deyen* + * Add FreeATS to list of companies using ViewComponent. *Ilia Liamshin* diff --git a/lib/view_component/engine.rb b/lib/view_component/engine.rb index b920eb5d8..0a8fb5887 100644 --- a/lib/view_component/engine.rb +++ b/lib/view_component/engine.rb @@ -76,12 +76,12 @@ class Engine < Rails::Engine # :nodoc: end # :nocov: - initializer "view_component.set_autoload_paths" do |app| + initializer "view_component.set_autoload_paths", before: :set_autoload_paths do |app| options = app.config.view_component if options.show_previews && !options.preview_paths.empty? - paths_to_add = options.preview_paths - ActiveSupport::Dependencies.autoload_paths - ActiveSupport::Dependencies.autoload_paths.concat(paths_to_add) if paths_to_add.any? + paths_to_add = options.preview_paths - app.config.autoload_paths + app.config.autoload_paths.concat(paths_to_add) if paths_to_add.any? end end