Repository navigation
Properly guard against incomplete Rails module definitions - #2462
Merged
Merged
Conversation
6 tasks done
mikz
force-pushed
the
derails
branch
2 times, most recently
from
October 17, 2025 09:58
9f35f2d to
b5e002c
Compare
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, 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.
Collaborator
|
Thanks @mikz for your contribution! I think it's a good idea to fix the guards as you suggest, but ViewComponent still depends on ActiveSupport and ActionView.
I'm not sure we want to guarantee this, because most of us are using ViewComponent with Rails, but if you'd like to work on a test suite that pass without Rails, we can certainly include it to the CI. |
joelhawksley
reviewed
Dec 3, 2025
Member
|
@mikz thank you for your contribution! I would welcome a CI suite to cover this change as our compatibility with Bridgetown is documented: https://viewcomponent.org/compatibility.html#bridgetown-static-site-generator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.envBad example:
defined?(Rails) && Rails.envThere 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 Railsto 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?