Skip to content

Commit b5e002c

Browse files
committed
Remove dependency on the Rails module
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.
1 parent e63ef0e commit b5e002c

File tree

4 files changed

+6
-6
lines changed

4 files changed

+6
-6
lines changed

lib/view_component.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module ViewComponent
1616
autoload :Preview
1717
autoload :Translatable
1818

19-
if Rails.env.test?
19+
if defined?(Rails.env) && Rails.env.test?
2020
autoload :TestHelpers
2121
autoload :SystemSpecHelpers
2222
autoload :SystemTestHelpers

lib/view_component/base.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def config
4747
end
4848

4949
include ActionView::Helpers
50-
include Rails.application.routes.url_helpers if defined?(Rails) && Rails.application
50+
include Rails.application.routes.url_helpers if defined?(Rails.application.routes)
5151
include ERB::Escape
5252
include ActiveSupport::CoreExt::ERBUtil
5353

@@ -302,7 +302,7 @@ def helpers
302302
@__vc_helpers ||= __vc_original_view_context || controller.view_context
303303
end
304304

305-
if ::Rails.env.development? || ::Rails.env.test?
305+
if defined?(Rails.env) && (::Rails.env.development? || ::Rails.env.test?)
306306
# @private
307307
def method_missing(method_name, *args) # rubocop:disable Style/MissingRespondToMissing
308308
super
@@ -331,7 +331,7 @@ def view_cache_dependencies
331331
[]
332332
end
333333

334-
if Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 1
334+
if defined?(Rails::VERSION) && (Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 1)
335335
# Rails expects us to define `format` on all renderables,
336336
# but we do not know the `format` of a ViewComponent until runtime.
337337
def format

lib/view_component/collection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def each(&block)
2020
components.each(&block)
2121
end
2222

23-
if Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 1
23+
if defined?(Rails::VERSION) && (Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 1)
2424
# Rails expects us to define `format` on all renderables,
2525
# but we do not know the `format` of a ViewComponent until runtime.
2626
def format

lib/view_component/config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def default_previews_options
163163
options = ActiveSupport::OrderedOptions.new
164164
options.controller = "ViewComponentsController"
165165
options.route = "/rails/view_components"
166-
options.enabled = Rails.env.development? || Rails.env.test?
166+
options.enabled = defined?(Rails.env) && (Rails.env.development? || Rails.env.test?)
167167
options.default_layout = nil
168168
options.paths = default_preview_paths
169169
options

0 commit comments

Comments
 (0)