Skip to content

Commit a3b9af5

Browse files
authored
Merge pull request #2462 from mikz/derails
Properly guard against incomplete Rails module definitions
2 parents e9d9920 + fd6d582 commit a3b9af5

File tree

5 files changed

+10
-6
lines changed

5 files changed

+10
-6
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ nav_order: 6
1010

1111
## main
1212

13+
* ViewComponent now works without `rails` and `railties` gems loaded, enabling compatibility with Bridgetown 2.0.
14+
15+
*Tom Lord*
16+
1317
* Capture partial block in the component's context, allowing access to the component instance inside the block.
1418

1519
*23tux*

lib/view_component.rb

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

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

lib/view_component/base.rb

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

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

@@ -304,7 +304,7 @@ def helpers
304304
@__vc_helpers ||= __vc_original_view_context || controller.view_context
305305
end
306306

307-
if ::Rails.env.development? || ::Rails.env.test?
307+
if defined?(Rails.env) && (::Rails.env.development? || ::Rails.env.test?)
308308
# @private
309309
def method_missing(method_name, *args) # rubocop:disable Style/MissingRespondToMissing
310310
super
@@ -333,7 +333,7 @@ def view_cache_dependencies
333333
[]
334334
end
335335

336-
if Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 1
336+
if defined?(Rails::VERSION) && Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 1
337337
# Rails expects us to define `format` on all renderables,
338338
# but we do not know the `format` of a ViewComponent until runtime.
339339
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)