diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 617ccc694..434f63c82 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -10,6 +10,10 @@ nav_order: 6 ## main +* ViewComponent now works without `rails` and `railties` gems loaded, enabling compatibility with Bridgetown 2.0. + + *Tom Lord* + * Capture partial block in the component's context, allowing access to the component instance inside the block. *23tux* diff --git a/lib/view_component.rb b/lib/view_component.rb index 93677d67f..ac1102ed9 100644 --- a/lib/view_component.rb +++ b/lib/view_component.rb @@ -17,7 +17,7 @@ module ViewComponent autoload :Preview autoload :Translatable - if Rails.env.test? + if defined?(Rails.env) && Rails.env.test? autoload :TestHelpers autoload :SystemSpecHelpers autoload :SystemTestHelpers diff --git a/lib/view_component/base.rb b/lib/view_component/base.rb index 9ece5e4b5..2aa924b99 100644 --- a/lib/view_component/base.rb +++ b/lib/view_component/base.rb @@ -46,7 +46,7 @@ def config end include ActionView::Helpers - include Rails.application.routes.url_helpers if defined?(Rails) && Rails.application + include Rails.application.routes.url_helpers if defined?(Rails.application.routes) include ERB::Escape include ActiveSupport::CoreExt::ERBUtil @@ -304,7 +304,7 @@ def helpers @__vc_helpers ||= __vc_original_view_context || controller.view_context end - if ::Rails.env.development? || ::Rails.env.test? + if defined?(Rails.env) && (::Rails.env.development? || ::Rails.env.test?) # @private def method_missing(method_name, *args) # rubocop:disable Style/MissingRespondToMissing super @@ -333,7 +333,7 @@ def view_cache_dependencies [] end - if Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 1 + if defined?(Rails::VERSION) && Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 1 # Rails expects us to define `format` on all renderables, # but we do not know the `format` of a ViewComponent until runtime. def format diff --git a/lib/view_component/collection.rb b/lib/view_component/collection.rb index c53f1f1af..7d2fb32f4 100644 --- a/lib/view_component/collection.rb +++ b/lib/view_component/collection.rb @@ -20,7 +20,7 @@ def each(&block) components.each(&block) end - if Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 1 + if defined?(Rails::VERSION) && Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 1 # Rails expects us to define `format` on all renderables, # but we do not know the `format` of a ViewComponent until runtime. def format diff --git a/lib/view_component/config.rb b/lib/view_component/config.rb index d3c26f57e..204791b88 100644 --- a/lib/view_component/config.rb +++ b/lib/view_component/config.rb @@ -163,7 +163,7 @@ def default_previews_options options = ActiveSupport::OrderedOptions.new options.controller = "ViewComponentsController" options.route = "/rails/view_components" - options.enabled = Rails.env.development? || Rails.env.test? + options.enabled = defined?(Rails.env) && (Rails.env.development? || Rails.env.test?) options.default_layout = nil options.paths = default_preview_paths options