Skip to content

Commit 1e5867f

Browse files
authored
Merge branch 'main' into v4
2 parents da8bcc0 + 451543a commit 1e5867f

20 files changed

+243
-112
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,12 @@ jobs:
7373
RUBY_VERSION: ${{ matrix.ruby_version }}
7474
CAPTURE_PATCH_ENABLED: ${{ matrix.mode == 'capture_patch_enabled' && 'true' || 'false' }}
7575
- name: Upload coverage results
76-
uses: actions/upload-artifact@v3.1.3
76+
uses: actions/upload-artifact@v4.4.0
7777
if: always()
7878
with:
7979
name: simplecov-resultset-rails${{matrix.rails_version}}-ruby${{matrix.ruby_version}}-${{matrix.mode}}
8080
path: coverage
81+
include-hidden-files: true
8182
primer_view_components_compatibility:
8283
name: Test compatibility with Primer ViewComponents (main)
8384
runs-on: ubuntu-latest
@@ -121,7 +122,7 @@ jobs:
121122
ruby-version: 3.3
122123
bundler-cache: true
123124
- name: Download coverage results
124-
uses: actions/download-artifact@v3
125+
uses: actions/download-artifact@v4.1.8
125126
- name: Collate simplecov
126127
run: |
127128
bundle exec rake coverage:report

Gemfile.lock

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ GEM
160160
mini_mime (1.1.5)
161161
mini_portile2 (2.8.7)
162162
minitest (5.25.1)
163-
net-imap (0.4.14)
163+
net-imap (0.4.16)
164164
date
165165
net-protocol
166166
net-pop (0.1.2)
@@ -174,7 +174,7 @@ GEM
174174
mini_portile2 (~> 2.8.2)
175175
racc (~> 1.4)
176176
parallel (1.26.3)
177-
parser (3.3.4.2)
177+
parser (3.3.5.0)
178178
ast (~> 2.4.1)
179179
racc
180180
pry (0.14.2)
@@ -224,9 +224,8 @@ GEM
224224
regexp_parser (2.9.2)
225225
reline (0.5.9)
226226
io-console (~> 0.5)
227-
rexml (3.3.6)
228-
strscan
229-
rspec-core (3.13.0)
227+
rexml (3.3.7)
228+
rspec-core (3.13.1)
230229
rspec-support (~> 3.13.0)
231230
rspec-expectations (3.13.2)
232231
diff-lcs (>= 1.2.0, < 2.0)
@@ -254,7 +253,7 @@ GEM
254253
rubocop-ast (>= 1.31.1, < 2.0)
255254
ruby-progressbar (~> 1.7)
256255
unicode-display_width (>= 2.4.0, < 3.0)
257-
rubocop-ast (1.32.1)
256+
rubocop-ast (1.32.2)
258257
parser (>= 3.3.1.0)
259258
rubocop-md (1.2.2)
260259
rubocop (>= 1.0)
@@ -301,11 +300,10 @@ GEM
301300
lint_roller (~> 1.1)
302301
rubocop-performance (~> 1.21.0)
303302
stringio (3.1.1)
304-
strscan (3.1.0)
305303
temple (0.10.3)
306304
terminal-table (3.0.2)
307305
unicode-display_width (>= 1.1.1, < 3)
308-
thor (1.3.1)
306+
thor (1.3.2)
309307
tilt (2.4.0)
310308
timeout (0.4.1)
311309
turbo-rails (1.5.0)
@@ -323,10 +321,10 @@ GEM
323321
websocket-extensions (0.1.5)
324322
xpath (3.2.0)
325323
nokogiri (~> 1.8)
326-
yard (0.9.36)
324+
yard (0.9.37)
327325
yard-activesupport-concern (0.0.1)
328326
yard (>= 0.8)
329-
zeitwerk (2.6.17)
327+
zeitwerk (2.6.18)
330328

331329
PLATFORMS
332330
ruby

docs/CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ nav_order: 5
1818

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

21+
* Add support for request formats.
22+
23+
*Joel Hawksley*
24+
25+
* Add `rendered_json` test helper.
26+
27+
*Joel Hawksley*
28+
29+
* Add `with_format` test helper.
30+
31+
*Joel Hawksley*
32+
33+
* Warn if using Ruby < 3.1 or Rails < 7.0, which will not be supported by ViewComponent v4.
34+
2135
*Joel Hawksley*
2236

2337
* Add Kicksite to list of companies using ViewComponent.

docs/Gemfile.lock

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4-
activesupport (7.2.0)
4+
activesupport (7.2.1)
55
base64
66
bigdecimal
77
concurrent-ruby (~> 1.0, >= 1.3.1)
@@ -35,10 +35,10 @@ GEM
3535
ffi (>= 1.15.0)
3636
eventmachine (1.2.7)
3737
execjs (2.9.1)
38-
faraday (2.10.1)
39-
faraday-net_http (>= 2.0, < 3.2)
38+
faraday (2.11.0)
39+
faraday-net_http (>= 2.0, < 3.4)
4040
logger
41-
faraday-net_http (3.1.1)
41+
faraday-net_http (3.3.0)
4242
net-http
4343
ffi (1.17.0)
4444
ffi (1.17.0-x86_64-darwin)
@@ -220,7 +220,7 @@ GEM
220220
listen (3.9.0)
221221
rb-fsevent (~> 0.10, >= 0.10.3)
222222
rb-inotify (~> 0.9, >= 0.9.10)
223-
logger (1.6.0)
223+
logger (1.6.1)
224224
mercenary (0.3.6)
225225
mini_portile2 (2.8.7)
226226
minima (2.5.1)
@@ -245,8 +245,7 @@ GEM
245245
rb-fsevent (0.11.2)
246246
rb-inotify (0.11.1)
247247
ffi (~> 1.0)
248-
rexml (3.3.6)
249-
strscan
248+
rexml (3.3.7)
250249
rouge (3.30.0)
251250
rubyzip (2.3.2)
252251
safe_yaml (1.0.5)
@@ -260,15 +259,14 @@ GEM
260259
faraday (>= 0.17.3, < 3)
261260
securerandom (0.3.1)
262261
simpleidn (0.2.3)
263-
strscan (3.1.0)
264262
terminal-table (1.8.0)
265263
unicode-display_width (~> 1.1, >= 1.1.1)
266264
typhoeus (1.4.1)
267265
ethon (>= 0.9.0)
268266
tzinfo (2.0.6)
269267
concurrent-ruby (~> 1.0)
270268
unicode-display_width (1.8.0)
271-
uri (0.13.0)
269+
uri (0.13.1)
272270
webrick (1.8.1)
273271

274272
PLATFORMS

docs/guide/testing.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ def test_render_component_for_tablet
135135
end
136136
```
137137

138+
## Request formats
139+
140+
Use the `with_format` helper to test specific request formats:
141+
142+
```ruby
143+
def test_render_component_as_json
144+
with_format :json do
145+
render_inline(MultipleFormatsComponent.new)
146+
147+
assert_equal(rendered_json["hello"], "world")
148+
end
149+
end
150+
```
151+
138152
## Configuring the controller used in tests
139153

140154
Since 2.27.0

lib/view_component.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ module ViewComponent
2121
autoload :TestCase
2222
autoload :SystemTestCase
2323
autoload :Translatable
24+
25+
if defined?(Rails) && Rails.version < "7.0"
26+
Kernel.warn("ViewComponent v4 will drop support for Rails < 7.0 in 2025.")
27+
end
2428
end
2529

2630
require "view_component/engine" if defined?(Rails::Engine)

lib/view_component/base.rb

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,14 @@ def render_in(view_context, &block)
107107

108108
if render?
109109
# Avoid allocating new string when output_preamble and output_postamble are blank
110-
rendered_template = safe_render_template_for(@__vc_variant).to_s
110+
rendered_template =
111+
if compiler.renders_template_for?(@__vc_variant, request&.format&.to_sym)
112+
render_template_for(@__vc_variant, request&.format&.to_sym)
113+
else
114+
maybe_escape_html(render_template_for(@__vc_variant, request&.format&.to_sym)) do
115+
Kernel.warn("WARNING: The #{self.class} component rendered HTML-unsafe output. The output will be automatically escaped, but you may want to investigate.")
116+
end
117+
end.to_s
111118

112119
if output_preamble.blank? && output_postamble.blank?
113120
rendered_template
@@ -330,16 +337,6 @@ def maybe_escape_html(text)
330337
end
331338
end
332339

333-
def safe_render_template_for(variant)
334-
if compiler.renders_template_for_variant?(variant)
335-
render_template_for(variant)
336-
else
337-
maybe_escape_html(render_template_for(variant)) do
338-
Kernel.warn("WARNING: The #{self.class} component rendered HTML-unsafe output. The output will be automatically escaped, but you may want to investigate.")
339-
end
340-
end
341-
end
342-
343340
def safe_output_preamble
344341
maybe_escape_html(output_preamble) do
345342
Kernel.warn("WARNING: The #{self.class} component was provided an HTML-unsafe preamble. The preamble will be automatically escaped, but you may want to investigate.")
@@ -500,13 +497,6 @@ def with_collection(collection, **args)
500497
Collection.new(self, collection, **args)
501498
end
502499

503-
# Provide identifier for ActionView template annotations
504-
#
505-
# @private
506-
def short_identifier
507-
@short_identifier ||= defined?(Rails.root) ? source_location.sub("#{Rails.root}/", "") : source_location
508-
end
509-
510500
# @private
511501
def inherited(child)
512502
# Compile so child will inherit compiled `call_*` template methods that
@@ -519,12 +509,12 @@ def inherited(child)
519509
# meaning it will not be called for any children and thus not compile their templates.
520510
if !child.instance_methods(false).include?(:render_template_for) && !child.compiled?
521511
child.class_eval <<~RUBY, __FILE__, __LINE__ + 1
522-
def render_template_for(variant = nil)
512+
def render_template_for(variant = nil, format = nil)
523513
# Force compilation here so the compiler always redefines render_template_for.
524514
# This is mostly a safeguard to prevent infinite recursion.
525515
self.class.compile(raise_errors: true, force: true)
526516
# .compile replaces this method; call the new one
527-
render_template_for(variant)
517+
render_template_for(variant, format)
528518
end
529519
RUBY
530520
end
@@ -586,22 +576,6 @@ def compiler
586576
@__vc_compiler ||= Compiler.new(self)
587577
end
588578

589-
# we'll eventually want to update this to support other types
590-
# @private
591-
def type
592-
"text/html"
593-
end
594-
595-
# @private
596-
def format
597-
:html
598-
end
599-
600-
# @private
601-
def identifier
602-
source_location
603-
end
604-
605579
# Set the parameter name used when rendering elements of a collection ([documentation](/guide/collections)):
606580
#
607581
# ```ruby

lib/view_component/collection.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class Collection
77
include Enumerable
88
attr_reader :component
99

10-
delegate :format, to: :component
1110
delegate :size, to: :@collection
1211

1312
attr_accessor :__vc_original_view_context
@@ -41,6 +40,12 @@ def each(&block)
4140
components.each(&block)
4241
end
4342

43+
# Rails expects us to define `format` on all renderables,
44+
# but we do not know the `format` of a ViewComponent until runtime.
45+
def format
46+
nil
47+
end
48+
4449
private
4550

4651
def initialize(component, object, **options)

0 commit comments

Comments
 (0)