Properly guard against incomplete Rails module definitions #2462
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Some parts of the code guard against undefined Rails module, but some parts do that in unsafe manner.
Good example:
defined?(Rails.env) && Rails.env
Bad example:
defined?(Rails) && Rails.env
There are gems (eg. rails-html-sanitizer https://github.com/rails/rails-html-sanitizer/blob/2a8fe8971b02b1b14abb6634e04af4e32b0057cd/lib/rails-html-sanitizer.rb#L10-L12), that define
module Rails
to extend it, but then it is incomplete and causes failures.This change unifies accessing Rails module with proper
defines?
guarding against incomplete definition of the Rails module.This pattern is already used on other places in the codebase:
view_component/app/controllers/concerns/view_component/preview_actions.rb
Line 81 in e63ef0e
view_component/app/controllers/concerns/view_component/preview_actions.rb
Line 98 in e63ef0e
view_component/lib/view_component/engine.rb
Line 22 in e63ef0e
view_component/lib/view_component/preview.rb
Line 7 in e63ef0e
view_component/lib/view_component/template.rb
Line 150 in e63ef0e
What are you trying to accomplish?
Use view_component gem without Rails.
What approach did you choose and why?
Guard clause
defined?
checking the nested access into the Rails module. Since it is already used within the codebase on other places.Anything you want to highlight for special attention from reviewers?
I don't know what would be a good way to test this. A part of test suite that runs without Rails? Is that something you'd like to guarantee as a project?