Skip to content

Conversation

mikz
Copy link

@mikz mikz commented Oct 17, 2025

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:

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?

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant