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 8be8086
Showing 1 changed file with 3 additions and 3 deletions.
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 8be8086

Please sign in to comment.