Skip to content

Commit

Permalink
Fix setting custom preview_paths
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tvdeyen committed Dec 16, 2024
1 parent e85b23b commit 092dda3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
6 changes: 3 additions & 3 deletions lib/view_component/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 092dda3

Please sign in to comment.