Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent adding existing autoload_paths #1383

Merged
merged 2 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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*

* Add FreeAgent to list of companies using ViewComponent.

*Simon Fish*
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ ViewComponent is built by over a hundred members of the community, including:
<img src="https://avatars.githubusercontent.com/tbroad-ramsey?s=64" alt="tbroad-ramsey" width="32" />
<img src="https://avatars.githubusercontent.com/tclem?s=64" alt="tclem" width="32" />
<img src="https://avatars.githubusercontent.com/tenderlove?s=64" alt="tenderlove" width="32" />
<img src="https://avatars.githubusercontent.com/thutterer?s=64" alt="thutterer" width="32" />
<img src="https://avatars.githubusercontent.com/tonkpils?s=64" alt="tonkpils" width="32" />
<img src="https://avatars.githubusercontent.com/traels?s=64" alt="traels" width="32" />
<img src="https://avatars.githubusercontent.com/vinistock?s=64" alt="vinistock" width="32" />
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 EngineTest < 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