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

Add config.include_application_routes to allow skipping inclusion of application routes #1906

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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 @@ -10,6 +10,10 @@ nav_order: 5

## main

* Add `config.include_application_routes` to allow skipping inclusion of application routes.

*Elia Schito*

* Allow overridden slot methods to use `super`.

*Andrew Schwartz*
Expand Down
6 changes: 5 additions & 1 deletion lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,11 @@ def render_template_for(variant = nil)
# If Rails application is loaded, add application url_helpers to the component context
# we need to check this to use this gem as a dependency
if defined?(Rails) && Rails.application && !(child < Rails.application.routes.url_helpers)
child.include Rails.application.routes.url_helpers
child.include Rails.application.routes.mounted_helpers

if ViewComponent::Base.config.include_application_routes
child.include Rails.application.routes.url_helpers
end
end

# Derive the source location of the component Ruby file from the call stack.
Expand Down
8 changes: 7 additions & 1 deletion lib/view_component/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def defaults
preview_paths: default_preview_paths,
test_controller: "ApplicationController",
default_preview_layout: nil,
capture_compatibility_patch_enabled: false
capture_compatibility_patch_enabled: false,
include_application_routes: true,
})
end

Expand Down Expand Up @@ -167,6 +168,11 @@ def defaults
# previews.
# Defaults to `false`.

# @!attribute include_application_routes
# @return [Boolean]
# Whether to include the application's routes upon inheriting from ViewComponent::Base.
# Defaults to `true`.

def default_preview_paths
(default_rails_preview_paths + default_rails_engines_preview_paths).uniq
end
Expand Down
14 changes: 14 additions & 0 deletions test/sandbox/test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,18 @@ def test_no_method_error_does_not_reference_missing_helper
MESSAGE
assert !exception_message_regex.match?(exception.message)
end

def test_including_application_routes
assert_equal true, ViewComponent::Base.config.include_application_routes
component_class = Class.new(ViewComponent::Base)
assert component_class.include?(Rails.application.routes.url_helpers)
assert component_class.include?(Rails.application.routes.mounted_helpers)

ViewComponent::Base.config.include_application_routes = false
component_class = Class.new(ViewComponent::Base)
refute component_class.include?(Rails.application.routes.url_helpers)
assert component_class.include?(Rails.application.routes.mounted_helpers)
ensure
ViewComponent::Base.config.include_application_routes = true
end
end
Loading