Skip to content

Commit

Permalink
[v4] Remove code paths for unsupported versions of Rails (#2103)
Browse files Browse the repository at this point in the history
* remove appraisals for unsupported rails versions

* remove accommodations for Rails < 6.1 in PreviewHelper

* remove support for Rails < 6.1 in Preview layout

* remove old gemfiles

* remove known issue docs for version of rails no longer supported

* remove old rails support in test suite

* simplify render_inline to only support modern Rails

* always include collection_renderer

* remove ruby and rails deprecation warnings in engine.rb

* remove render_monkey_patch

* remove render monkey patch

* update rendering test for rails and ruby deprecations

* remove duplicate components
  • Loading branch information
joelhawksley authored Sep 26, 2024
1 parent bc4a193 commit a16e4fe
Show file tree
Hide file tree
Showing 36 changed files with 43 additions and 513 deletions.
17 changes: 0 additions & 17 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
# frozen_string_literal: true

appraise "rails-6.1" do
gem "rails", "~> 6.1"
gem "tailwindcss-rails", "~> 2.0"

# Required for Ruby 3.1.0
gem "net-smtp", require: false
gem "net-imap", require: false
gem "net-pop", require: false
gem "turbo-rails", "~> 1"
end

appraise "rails-7.0" do
gem "rails", "~> 7.0"
gem "tailwindcss-rails", "~> 2.0"
gem "turbo-rails", "~> 1"
end

appraise "rails-7.1" do
gem "rails", "~> 7.1"
gem "tailwindcss-rails", "~> 2.0"
Expand Down
40 changes: 4 additions & 36 deletions app/helpers/preview_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# frozen_string_literal: true

module PreviewHelper
# :nocov:
include ActionView::Helpers::AssetUrlHelper if Rails.version.to_f < 6.1
# :nocov:

AVAILABLE_PRISM_LANGUAGES = %w[ruby erb haml]
FALLBACK_LANGUAGE = "ruby"

Expand All @@ -25,38 +21,10 @@ def prism_js_source_url
def find_template_data(lookup_context:, template_identifier:)
template = lookup_context.find_template(template_identifier)

if Rails.version.to_f >= 6.1 || template.source.present?
{
source: template.source,
prism_language_name: prism_language_name_by_template(template: template)
}
# :nocov:
else
# Fetch template source via finding it through preview paths
# to accomodate source view when exclusively using templates
# for previews for Rails < 6.1.
all_template_paths = ViewComponent::Base.config.preview_paths.map do |preview_path|
Dir.glob("#{preview_path}/**/*")
end.flatten

# Search for templates the contain `html`.
matching_templates = all_template_paths.find_all do |path|
path =~ /#{template_identifier}*.(html)/
end

raise ViewComponent::NoMatchingTemplatesForPreviewError.new(template_identifier) if matching_templates.empty?
raise ViewComponent::MultipleMatchingTemplatesForPreviewError.new(template_identifier) if matching_templates.size > 1

template_file_path = matching_templates.first
template_source = File.read(template_file_path)
prism_language_name = prism_language_name_by_template_path(template_file_path: template_file_path)

{
source: template_source,
prism_language_name: prism_language_name
}
end
# :nocov:
{
source: template.source,
prism_language_name: prism_language_name_by_template(template: template)
}
end

private
Expand Down
6 changes: 1 addition & 5 deletions app/views/view_components/preview.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<% if @render_args[:component] %>
<% if ViewComponent::Base.config.render_monkey_patch_enabled || Rails.version.to_f >= 6.1 %>
<%= render(@render_args[:component], @render_args[:args], &@render_args[:block]) %>
<% else %>
<%= render_component(@render_args[:component], &@render_args[:block]) %>
<% end %>
<%= render(@render_args[:component], @render_args[:args], &@render_args[:block]) %>
<% else %>
<%= render template: @render_args[:template], locals: @render_args[:locals] || {} %>
<% end %>
Expand Down
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ nav_order: 5

*Joel Hawksley*

* BREAKING: Remove `render_component` and `render` monkey patch configured with `render_monkey_patch_enabled`.

*Joel Hawksley*

* Add basic internal testing for memory allocations.

*Joel Hawksley*
Expand Down
6 changes: 0 additions & 6 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,6 @@ Defaults to `['test/component/previews']` relative to your Rails root.
The entry route for component previews.
Defaults to `"/rails/view_components"`.

### `.render_monkey_patch_enabled`

If this is disabled, use `#render_component` or
`#render_component_to_string` instead.
Defaults to `true`.

### `.show_previews`

Whether component previews are enabled.
Expand Down
15 changes: 0 additions & 15 deletions docs/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,6 @@ ViewComponent supports all actively supported versions of [Ruby](https://endofli

ViewComponent is tested against ERB, Haml, and Slim, but it should support most Rails template handlers.

## Disabling the render monkey patch (Rails < 6.1)

Since 2.13.0
{: .label }

To [avoid conflicts](https://github.com/viewcomponent/view_component/issues/288) between ViewComponent and other gems that also monkey patch the `render` method, it's possible to configure ViewComponent to not include the render monkey patch:

`config.view_component.render_monkey_patch_enabled = false # defaults to true`

With the monkey patch disabled, use `render_component` (or `render_component_to_string`) instead:

```erb
<%= render_component Component.new(message: "bar") %>
```

## Bridgetown (Static Site Generator)

[Bridgetown](https://www.bridgetownrb.com/) supports ViewComponent via an experimental shim provided by the [bridgetown-view-component gem](https://github.com/bridgetownrb/bridgetown-view-component). More information available [here](https://www.bridgetownrb.com/docs/components/ruby#need-compatibility-with-rails-try-viewcomponent-experimental).
Expand Down
4 changes: 0 additions & 4 deletions docs/known_issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,3 @@ Calls to form helpers such as `form_with` in ViewComponents [don't use the defau
<%= f.text_field :name %>
<% end %>
```

## Inconsistent controller rendering behavior between Rails versions

In versions of Rails < 6.1, rendering a ViewComponent from a controller doesn't include the layout.
12 changes: 0 additions & 12 deletions gemfiles/rails_6.1.gemfile

This file was deleted.

9 changes: 0 additions & 9 deletions gemfiles/rails_7.0.gemfile

This file was deleted.

7 changes: 0 additions & 7 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,6 @@ def compiler
# configured on a per-test basis using `with_controller_class`.
#

# Set if render monkey patches should be included or not in Rails <6.1:
#
# ```ruby
# config.view_component.render_monkey_patch_enabled = false
# ```
#

# Path for component files
#
# ```ruby
Expand Down
2 changes: 1 addition & 1 deletion lib/view_component/collection.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "action_view/renderer/collection_renderer" if Rails.version.to_f >= 6.1
require "action_view/renderer/collection_renderer"

module ViewComponent
class Collection
Expand Down
7 changes: 0 additions & 7 deletions lib/view_component/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def defaults
show_previews_source: false,
instrumentation_enabled: false,
use_deprecated_instrumentation_name: true,
render_monkey_patch_enabled: true,
view_component_path: "app/components",
component_parent_class: nil,
show_previews: Rails.env.development? || Rails.env.test?,
Expand Down Expand Up @@ -120,12 +119,6 @@ def defaults
# Will default to `false` in next major version.
# Defaults to `true`.

# @!attribute render_monkey_patch_enabled
# @return [Boolean] Whether the #render method should be monkey patched.
# If this is disabled, use `#render_component` or
# `#render_component_to_string` instead.
# Defaults to `true`.

# @!attribute view_component_path
# @return [String]
# The path in which components, their templates, and their sidecars should
Expand Down
51 changes: 0 additions & 51 deletions lib/view_component/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class Engine < Rails::Engine # :nodoc:
options[config_option] ||= ViewComponent::Base.public_send(config_option)
end
options.instrumentation_enabled = false if options.instrumentation_enabled.nil?
options.render_monkey_patch_enabled = true if options.render_monkey_patch_enabled.nil?
options.show_previews = (Rails.env.development? || Rails.env.test?) if options.show_previews.nil?

if options.show_previews
Expand Down Expand Up @@ -85,46 +84,6 @@ class Engine < Rails::Engine # :nodoc:
end
end

initializer "view_component.monkey_patch_render" do |app|
next if Rails.version.to_f >= 6.1 || !app.config.view_component.render_monkey_patch_enabled

# :nocov:
ViewComponent::Deprecation.deprecation_warning("Monkey patching `render`", "ViewComponent 4.0 will remove the `render` monkey patch")

ActiveSupport.on_load(:action_view) do
require "view_component/render_monkey_patch"
ActionView::Base.prepend ViewComponent::RenderMonkeyPatch
end

ActiveSupport.on_load(:action_controller) do
require "view_component/rendering_monkey_patch"
require "view_component/render_to_string_monkey_patch"
ActionController::Base.prepend ViewComponent::RenderingMonkeyPatch
ActionController::Base.prepend ViewComponent::RenderToStringMonkeyPatch
end
# :nocov:
end

initializer "view_component.include_render_component" do |_app|
next if Rails.version.to_f >= 6.1

# :nocov:
ViewComponent::Deprecation.deprecation_warning("using `render_component`", "ViewComponent 4.0 will remove `render_component`")

ActiveSupport.on_load(:action_view) do
require "view_component/render_component_helper"
ActionView::Base.include ViewComponent::RenderComponentHelper
end

ActiveSupport.on_load(:action_controller) do
require "view_component/rendering_component_helper"
require "view_component/render_component_to_string_helper"
ActionController::Base.include ViewComponent::RenderingComponentHelper
ActionController::Base.include ViewComponent::RenderComponentToStringHelper
end
# :nocov:
end

initializer "static assets" do |app|
if serve_static_preview_assets?(app.config)
app.middleware.use(::ActionDispatch::Static, "#{root}/app/assets/vendor")
Expand Down Expand Up @@ -168,16 +127,6 @@ def serve_static_preview_assets?(app_config)
end
end

# :nocov:
if RUBY_VERSION < "3.0.0"
ViewComponent::Deprecation.deprecation_warning("Support for Ruby versions < 3.0.0", "ViewComponent 4.0 will remove support for Ruby versions < 3.0.0 ")
end

if Rails.version.to_f < 6.1
ViewComponent::Deprecation.deprecation_warning("Support for Rails versions < 6.1", "ViewComponent 4.0 will remove support for Rails versions < 6.1 ")
end
# :nocov:

app.executor.to_run :before do
CompileCache.invalidate! unless ActionView::Base.cache_template_loading
end
Expand Down
2 changes: 0 additions & 2 deletions lib/view_component/preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ def render_with_template(template: nil, locals: {})
}
end

alias_method :render_component, :render

class << self
# Returns all component preview classes.
def all
Expand Down
10 changes: 0 additions & 10 deletions lib/view_component/render_component_helper.rb

This file was deleted.

9 changes: 0 additions & 9 deletions lib/view_component/render_component_to_string_helper.rb

This file was deleted.

13 changes: 0 additions & 13 deletions lib/view_component/render_monkey_patch.rb

This file was deleted.

13 changes: 0 additions & 13 deletions lib/view_component/render_to_string_monkey_patch.rb

This file was deleted.

9 changes: 0 additions & 9 deletions lib/view_component/rendering_component_helper.rb

This file was deleted.

13 changes: 0 additions & 13 deletions lib/view_component/rendering_monkey_patch.rb

This file was deleted.

11 changes: 1 addition & 10 deletions lib/view_component/test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,7 @@ def assert_component_rendered
# @return [Nokogiri::HTML]
def render_inline(component, **args, &block)
@page = nil
@rendered_content =
if Rails.version.to_f >= 6.1
vc_test_controller.view_context.render(component, args, &block)

# :nocov:
else
vc_test_controller.view_context.render_component(component, &block)
end

# :nocov:
@rendered_content = vc_test_controller.view_context.render(component, args, &block)

Nokogiri::HTML.fragment(@rendered_content)
end
Expand Down
6 changes: 1 addition & 5 deletions test/sandbox/app/components/container_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

class ContainerComponent < ViewComponent::Base
def call
if Rails.application.config.view_component.render_monkey_patch_enabled || Rails.version.to_f >= 6.1
render HelpersProxyComponent.new
else
render_component HelpersProxyComponent.new
end
render HelpersProxyComponent.new
end
end

This file was deleted.

This file was deleted.

Loading

0 comments on commit a16e4fe

Please sign in to comment.