Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v4.0.0 #2085

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

v4.0.0 #2085

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,6 @@ jobs:
fail-fast: false
matrix:
include:
- ruby_version: "3.0"
rails_version: "6.1"
mode: "capture_patch_enabled"
- ruby_version: "3.0"
rails_version: "6.1"
mode: "capture_patch_disabled"
- ruby_version: "3.1"
rails_version: "7.0"
mode: "capture_patch_enabled"
- ruby_version: "3.1"
rails_version: "7.0"
mode: "capture_patch_disabled"
- ruby_version: "3.2"
rails_version: "7.1"
mode: "capture_patch_enabled"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.gem
*.rbc
.ruby-version
.DS_Store
/.config
/coverage/assets
/coverage/index.html
Expand Down
19 changes: 0 additions & 19 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
# frozen_string_literal: true

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

# 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"
gem "sprockets-rails", "~> 3.4.2"
end

appraise "rails-7.1" do
gem "rails", "~> 7.1"
gem "tailwindcss-rails", "~> 2.0"
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ source "https://rubygems.org"
gemspec

rails_version = (ENV["RAILS_VERSION"] || "~> 8").to_s

gem "rails", (rails_version == "main") ? {git: "https://github.com/rails/rails", ref: "main"} : rails_version

ruby_version = (ENV["RUBY_VERSION"] || "~> 3.4").to_s
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ PATH
remote: .
specs:
view_component (3.21.0)
activesupport (>= 5.2.0, < 8.1)
activesupport (>= 7.1.0, < 8.1)
concurrent-ruby (= 1.3.4)
method_source (~> 1.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_for_preview_source(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
29 changes: 29 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,35 @@

## main

## 4.0.0

* BREAKING: Require [non-EOL](https://endoflife.date/rails) Rails (`>= 7.1.0`).

*Joel Hawksley*

* BREAKING: Require [non-EOL](https://www.ruby-lang.org/en/downloads/branches/) Ruby (`>= 3.2.0`).

*Joel Hawksley*

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

*Joel Hawksley*

* BREAKING: Remove support for variant names containing `.` to be consistent with Rails.

*Stephen Nelson*

* BREAKING: Use ActionView's `lookup_context` for picking templates instead of the request format.

3.15 added support for using templates that match the request format, i.e. if `/resource.csv` is requested then

Check failure on line 33 in docs/CHANGELOG.md

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Microsoft.Foreign] Use 'that is' instead of 'i.e.'. Raw Output: {"message": "[Microsoft.Foreign] Use 'that is' instead of 'i.e.'.", "location": {"path": "docs/CHANGELOG.md", "range": {"start": {"line": 33, "column": 73}}}, "severity": "ERROR"}
ViewComponents would pick `_component.csv.erb` over `_component.html.erb`.

With this release, the request format is no longer considered and instead ViewComponent will use the Rails logic
for picking the most appropriate template type, i.e. the csv template will be used if it matches the `Accept` header

Check failure on line 37 in docs/CHANGELOG.md

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Microsoft.Foreign] Use 'that is' instead of 'i.e.'. Raw Output: {"message": "[Microsoft.Foreign] Use 'that is' instead of 'i.e.'.", "location": {"path": "docs/CHANGELOG.md", "range": {"start": {"line": 37, "column": 51}}}, "severity": "ERROR"}
or because the controller uses a `respond_to` block to pick the response format.

*Stephen Nelson*

* Remove JS and CSS docs as they proved difficult to maintain and lacked consensus.

*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/components/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
19 changes: 1 addition & 18 deletions docs/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,12 @@ nav_order: 6

## Ruby & Rails

ViewComponent supports all actively supported versions of Ruby (3.0+) and Ruby on Rails (6.1+) and is tested against a combination of these versions of Ruby on Rails.

While EOL (end-of-life) versions of Ruby and Ruby on Rails may still work with ViewComponent, they're not actively supported and no longer tested. We will still accept patches on a case-by-case basis to support older Ruby & Rails versions based on the complexity and maintenance burden. Please open an issue before submitting such a Pull Request.
ViewComponent supports all actively supported versions of [Ruby](https://endoflife.date/ruby) (>= 3.2) and [Ruby on Rails](https://endoflife.date/rails) (>= 7.1). Changes to the minimum Ruby and Rails versions supported will only be made in major releases.

## Template languages

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
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ ViewComponent is built by over a hundred members of the community, including:
<img src="https://avatars.githubusercontent.com/sammyhenningsson?s=64" alt="sammyhenningsson" width="32" />
<img src="https://avatars.githubusercontent.com/sampart?s=64" alt="sampart" width="32" />
<img src="https://avatars.githubusercontent.com/seanpdoyle?s=64" alt="seanpdoyle" width="32" />
<img src="https://avatars.githubusercontent.com/sfnelson?s=64" alt="sfnelson" width="32" />
<img src="https://avatars.githubusercontent.com/simonrand?s=64" alt="simonrand" width="32" />
<img src="https://avatars.githubusercontent.com/skryukov?s=64" alt="skryukov" width="32" />
<img src="https://avatars.githubusercontent.com/smashwilson?s=64" alt="smashwilson" width="32" />
Expand Down
4 changes: 0 additions & 4 deletions docs/known_issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,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.
13 changes: 0 additions & 13 deletions gemfiles/rails_6.1.gemfile

This file was deleted.

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

This file was deleted.

34 changes: 13 additions & 21 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require "view_component/errors"
require "view_component/inline_template"
require "view_component/preview"
require "view_component/request_details"
require "view_component/slotable"
require "view_component/slotable_default"
require "view_component/template"
Expand Down Expand Up @@ -63,6 +64,8 @@ def set_original_view_context(view_context)
self.__vc_original_view_context = view_context
end

using RequestDetails

# Entrypoint for rendering components.
#
# - `view_context`: ActionView context from calling view
Expand Down Expand Up @@ -90,13 +93,12 @@ def render_in(view_context, &block)
# For i18n
@virtual_path ||= virtual_path

# For template variants (+phone, +desktop, etc.)
@__vc_variant ||= @lookup_context.variants.first
# Describes the inferred request constraints (locales, formats, variants)
@__vc_requested_details ||= @lookup_context.vc_requested_details

# For caching, such as #cache_if
@current_template = nil unless defined?(@current_template)
old_current_template = @current_template
@current_template = self

if block && defined?(@__vc_content_set_by_with_content)
raise DuplicateContentError.new(self.class.name)
Expand All @@ -108,7 +110,7 @@ def render_in(view_context, &block)
before_render

if render?
rendered_template = render_template_for(@__vc_variant, __vc_request&.format&.to_sym).to_s
rendered_template = render_template_for(@__vc_requested_details).to_s

# Avoid allocating new string when output_preamble and output_postamble are blank
if output_preamble.blank? && output_postamble.blank?
Expand Down Expand Up @@ -156,7 +158,7 @@ def render_parent_to_string
target_render = self.class.instance_variable_get(:@__vc_ancestor_calls)[@__vc_parent_render_level]
@__vc_parent_render_level += 1

target_render.bind_call(self, @__vc_variant)
target_render.bind_call(self, @__vc_requested_details)
ensure
@__vc_parent_render_level -= 1
end
Expand Down Expand Up @@ -267,11 +269,10 @@ def view_cache_dependencies
[]
end

# For caching, such as #cache_if
#
# @private
# Rails expects us to define `format` on all renderables,
# but we do not know the `format` of a ViewComponent until runtime.
def format
@__vc_variant if defined?(@__vc_variant)
nil
end

# The current request. Use sparingly as doing so introduces coupling that
Expand Down Expand Up @@ -328,7 +329,7 @@ def content_evaluated?
end

def maybe_escape_html(text)
return text if __vc_request && !__vc_request.format.html?
return text if @current_template && !@current_template.html?
return text if text.blank?

if text.html_safe?
Expand Down Expand Up @@ -361,13 +362,6 @@ def safe_output_postamble
# 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 Expand Up @@ -524,12 +518,12 @@ def inherited(child)
# meaning it will not be called for any children and thus not compile their templates.
if !child.instance_methods(false).include?(:render_template_for) && !child.compiled?
child.class_eval <<~RUBY, __FILE__, __LINE__ + 1
def render_template_for(variant = nil, format = nil)
def render_template_for(requested_details)
# Force compilation here so the compiler always redefines render_template_for.
# This is mostly a safeguard to prevent infinite recursion.
self.class.compile(raise_errors: true, force: true)
# .compile replaces this method; call the new one
render_template_for(variant, format)
render_template_for(requested_details)
end
RUBY
end
Expand Down Expand Up @@ -686,8 +680,6 @@ def splatted_keyword_argument_present?
def initialize_parameter_names
return attribute_names.map(&:to_sym) if respond_to?(:attribute_names)

return attribute_types.keys.map(&:to_sym) if Rails::VERSION::MAJOR <= 5 && respond_to?(:attribute_types)

initialize_parameters.map(&:last)
end

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
Loading
Loading