Skip to content

Commit

Permalink
Merge branch 'main' into v4
Browse files Browse the repository at this point in the history
  • Loading branch information
joelhawksley authored Sep 11, 2024
2 parents da8bcc0 + 451543a commit 1e5867f
Show file tree
Hide file tree
Showing 20 changed files with 243 additions and 112 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ jobs:
RUBY_VERSION: ${{ matrix.ruby_version }}
CAPTURE_PATCH_ENABLED: ${{ matrix.mode == 'capture_patch_enabled' && 'true' || 'false' }}
- name: Upload coverage results
uses: actions/upload-artifact@v3.1.3
uses: actions/upload-artifact@v4.4.0
if: always()
with:
name: simplecov-resultset-rails${{matrix.rails_version}}-ruby${{matrix.ruby_version}}-${{matrix.mode}}
path: coverage
include-hidden-files: true
primer_view_components_compatibility:
name: Test compatibility with Primer ViewComponents (main)
runs-on: ubuntu-latest
Expand Down Expand Up @@ -121,7 +122,7 @@ jobs:
ruby-version: 3.3
bundler-cache: true
- name: Download coverage results
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4.1.8
- name: Collate simplecov
run: |
bundle exec rake coverage:report
18 changes: 8 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ GEM
mini_mime (1.1.5)
mini_portile2 (2.8.7)
minitest (5.25.1)
net-imap (0.4.14)
net-imap (0.4.16)
date
net-protocol
net-pop (0.1.2)
Expand All @@ -174,7 +174,7 @@ GEM
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
parallel (1.26.3)
parser (3.3.4.2)
parser (3.3.5.0)
ast (~> 2.4.1)
racc
pry (0.14.2)
Expand Down Expand Up @@ -224,9 +224,8 @@ GEM
regexp_parser (2.9.2)
reline (0.5.9)
io-console (~> 0.5)
rexml (3.3.6)
strscan
rspec-core (3.13.0)
rexml (3.3.7)
rspec-core (3.13.1)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.2)
diff-lcs (>= 1.2.0, < 2.0)
Expand Down Expand Up @@ -254,7 +253,7 @@ GEM
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.32.1)
rubocop-ast (1.32.2)
parser (>= 3.3.1.0)
rubocop-md (1.2.2)
rubocop (>= 1.0)
Expand Down Expand Up @@ -301,11 +300,10 @@ GEM
lint_roller (~> 1.1)
rubocop-performance (~> 1.21.0)
stringio (3.1.1)
strscan (3.1.0)
temple (0.10.3)
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
thor (1.3.1)
thor (1.3.2)
tilt (2.4.0)
timeout (0.4.1)
turbo-rails (1.5.0)
Expand All @@ -323,10 +321,10 @@ GEM
websocket-extensions (0.1.5)
xpath (3.2.0)
nokogiri (~> 1.8)
yard (0.9.36)
yard (0.9.37)
yard-activesupport-concern (0.0.1)
yard (>= 0.8)
zeitwerk (2.6.17)
zeitwerk (2.6.18)

PLATFORMS
ruby
Expand Down
14 changes: 14 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ nav_order: 5

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

* Add support for request formats.

*Joel Hawksley*

* Add `rendered_json` test helper.

*Joel Hawksley*

* Add `with_format` test helper.

*Joel Hawksley*

* Warn if using Ruby < 3.1 or Rails < 7.0, which will not be supported by ViewComponent v4.

*Joel Hawksley*

* Add Kicksite to list of companies using ViewComponent.
Expand Down
16 changes: 7 additions & 9 deletions docs/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GEM
remote: https://rubygems.org/
specs:
activesupport (7.2.0)
activesupport (7.2.1)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.3.1)
Expand Down Expand Up @@ -35,10 +35,10 @@ GEM
ffi (>= 1.15.0)
eventmachine (1.2.7)
execjs (2.9.1)
faraday (2.10.1)
faraday-net_http (>= 2.0, < 3.2)
faraday (2.11.0)
faraday-net_http (>= 2.0, < 3.4)
logger
faraday-net_http (3.1.1)
faraday-net_http (3.3.0)
net-http
ffi (1.17.0)
ffi (1.17.0-x86_64-darwin)
Expand Down Expand Up @@ -220,7 +220,7 @@ GEM
listen (3.9.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
logger (1.6.0)
logger (1.6.1)
mercenary (0.3.6)
mini_portile2 (2.8.7)
minima (2.5.1)
Expand All @@ -245,8 +245,7 @@ GEM
rb-fsevent (0.11.2)
rb-inotify (0.11.1)
ffi (~> 1.0)
rexml (3.3.6)
strscan
rexml (3.3.7)
rouge (3.30.0)
rubyzip (2.3.2)
safe_yaml (1.0.5)
Expand All @@ -260,15 +259,14 @@ GEM
faraday (>= 0.17.3, < 3)
securerandom (0.3.1)
simpleidn (0.2.3)
strscan (3.1.0)
terminal-table (1.8.0)
unicode-display_width (~> 1.1, >= 1.1.1)
typhoeus (1.4.1)
ethon (>= 0.9.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (1.8.0)
uri (0.13.0)
uri (0.13.1)
webrick (1.8.1)

PLATFORMS
Expand Down
14 changes: 14 additions & 0 deletions docs/guide/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ def test_render_component_for_tablet
end
```

## Request formats

Use the `with_format` helper to test specific request formats:

```ruby
def test_render_component_as_json
with_format :json do
render_inline(MultipleFormatsComponent.new)

assert_equal(rendered_json["hello"], "world")
end
end
```

## Configuring the controller used in tests

Since 2.27.0
Expand Down
4 changes: 4 additions & 0 deletions lib/view_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ module ViewComponent
autoload :TestCase
autoload :SystemTestCase
autoload :Translatable

if defined?(Rails) && Rails.version < "7.0"
Kernel.warn("ViewComponent v4 will drop support for Rails < 7.0 in 2025.")
end
end

require "view_component/engine" if defined?(Rails::Engine)
46 changes: 10 additions & 36 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ def render_in(view_context, &block)

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

if output_preamble.blank? && output_postamble.blank?
rendered_template
Expand Down Expand Up @@ -330,16 +337,6 @@ def maybe_escape_html(text)
end
end

def safe_render_template_for(variant)
if compiler.renders_template_for_variant?(variant)
render_template_for(variant)
else
maybe_escape_html(render_template_for(variant)) do
Kernel.warn("WARNING: The #{self.class} component rendered HTML-unsafe output. The output will be automatically escaped, but you may want to investigate.")
end
end
end

def safe_output_preamble
maybe_escape_html(output_preamble) do
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.")
Expand Down Expand Up @@ -500,13 +497,6 @@ def with_collection(collection, **args)
Collection.new(self, collection, **args)
end

# Provide identifier for ActionView template annotations
#
# @private
def short_identifier
@short_identifier ||= defined?(Rails.root) ? source_location.sub("#{Rails.root}/", "") : source_location
end

# @private
def inherited(child)
# Compile so child will inherit compiled `call_*` template methods that
Expand All @@ -519,12 +509,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)
def render_template_for(variant = nil, format = nil)
# 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)
render_template_for(variant, format)
end
RUBY
end
Expand Down Expand Up @@ -586,22 +576,6 @@ def compiler
@__vc_compiler ||= Compiler.new(self)
end

# we'll eventually want to update this to support other types
# @private
def type
"text/html"
end

# @private
def format
:html
end

# @private
def identifier
source_location
end

# Set the parameter name used when rendering elements of a collection ([documentation](/guide/collections)):
#
# ```ruby
Expand Down
7 changes: 6 additions & 1 deletion lib/view_component/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class Collection
include Enumerable
attr_reader :component

delegate :format, to: :component
delegate :size, to: :@collection

attr_accessor :__vc_original_view_context
Expand Down Expand Up @@ -41,6 +40,12 @@ def each(&block)
components.each(&block)
end

# Rails expects us to define `format` on all renderables,
# but we do not know the `format` of a ViewComponent until runtime.
def format
nil
end

private

def initialize(component, object, **options)
Expand Down
Loading

0 comments on commit 1e5867f

Please sign in to comment.