Skip to content

Commit

Permalink
Prevent adding duplicates to autoload_paths
Browse files Browse the repository at this point in the history
  • Loading branch information
thutterer committed Jun 25, 2022
1 parent 1a2e39d commit 4739b78
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ title: Changelog

## main

* Prevent adding duplicates to `autoload_paths`.

*Thomas Hutterer*

* Include polymorphic slots in `ViewComponent::Base` by default.
* Add per-component config option for stripping newlines from templates before compilation.

Expand Down
3 changes: 2 additions & 1 deletion lib/view_component/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ class Engine < Rails::Engine # :nodoc:
options = app.config.view_component

if options.show_previews && !options.preview_paths.empty?
ActiveSupport::Dependencies.autoload_paths.concat(options.preview_paths)
paths_to_add = options.preview_paths - ActiveSupport::Dependencies.autoload_paths
ActiveSupport::Dependencies.autoload_paths.concat(paths_to_add) if paths_to_add.any?
end
end

Expand Down
4 changes: 4 additions & 0 deletions test/sandbox/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
module Sandbox
class Application < Rails::Application
config.action_controller.asset_host = "http://assets.example.com"

# Prepare test_set_no_duplicate_autoload_paths
config.autoload_paths.push("#{config.root}/my/components/previews")
config.view_component.preview_paths << "#{config.root}/my/components/previews"
end
end

Expand Down
10 changes: 10 additions & 0 deletions test/sandbox/test/initializers/engine_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require "test_helper"

class ViewComponent::Base::UnitTest < Minitest::Test
def test_set_no_duplicate_autoload_paths
test_path = "#{Rails.root}/my/components/previews" # set in sandbox/config/application.rb
assert_equal 1, ActiveSupport::Dependencies.autoload_paths.count(test_path)
end
end

0 comments on commit 4739b78

Please sign in to comment.