view_component 2.54.1 → 2.56.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of view_component might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f9cab3c05c054f173b248b903912dfb432eeec09b2d3d42f6fd602a7565acd2d
4
- data.tar.gz: 78b70e8ea1306fb635978dbe2a86cece2618247bb0079798e5a886f2210dfed1
3
+ metadata.gz: 338f95cb25ce1684e1be8486d44f84130f7a2a1b96f01d3a99b9909cb64ff650
4
+ data.tar.gz: ac955c1551852c4ef4f5c884bacca4513f10b08b59e6a6b9aa635cfc20a7481c
5
5
  SHA512:
6
- metadata.gz: 653d1c627f3fb52677e47516472fec508c562359e3beac3defa43d172d3e793359207df638f352f1c1cc6a876a0f3c3fdc4c7ff4eefdc1907c20a161e9359d83
7
- data.tar.gz: 25bc7a21cde84c3311d9482cdb2ccccf821ce2c104492506a999e81f8cd9d9a5b3b3c19f3b417dc3f250787adf18fbe9f9d9ff5589e897fc199e26e50e4b0efa
6
+ metadata.gz: efc8cebe7198a787d2534bf1d5ddadbea1d90940495df41cd1c13fe8f2f8ec2f4f065c6a7ee874e4e0c84cb823a02bdbcc3b912c29f00580b04b7475c1384755
7
+ data.tar.gz: 0c4f543ec0047ce0cbd7fbbeafd38ddc973051e6e010e19b36a3040a89ebdd96a3dc108c48b7aeb2ce95416908bbefad0387fe965f7400940ea07284f177c5eb
@@ -8,7 +8,6 @@ module ViewComponent
8
8
  prepend_view_path File.expand_path("../../../views", __dir__)
9
9
 
10
10
  around_action :set_locale, only: :previews
11
- before_action :find_preview, only: :previews
12
11
  before_action :require_local!, unless: :show_previews?
13
12
 
14
13
  if respond_to?(:content_security_policy)
@@ -23,6 +22,8 @@ module ViewComponent
23
22
  end
24
23
 
25
24
  def previews
25
+ find_preview
26
+
26
27
  if params[:path] == @preview.preview_name
27
28
  @page_title = "Component Previews for #{@preview.preview_name}"
28
29
  render "view_components/previews", **determine_layout
data/docs/CHANGELOG.md CHANGED
@@ -9,6 +9,56 @@ title: Changelog
9
9
 
10
10
  ## main
11
11
 
12
+ ## 2.56.1
13
+
14
+ * Rename private accessor `rendered_component` to `rendered_content`.
15
+
16
+ *Yoshiyuki Hirano*, *Simon Dawson*
17
+
18
+ ## 2.56.0
19
+
20
+ * Introduce experimental `render_preview` test helper. Note: `@rendered_component` in `TestHelpers` has been renamed to `@rendered_content`.
21
+
22
+ *Joel Hawksley*
23
+
24
+ * Move framework tests into sandbox application.
25
+
26
+ *Joel Hawksley*
27
+
28
+ * Add G2 to list of companies that use ViewComponent.
29
+
30
+ *Jack Shuff*
31
+
32
+ * Add Within3 to list of companies that use ViewComponent.
33
+
34
+ *Drew Bragg*
35
+
36
+ * Add Mission Met to list of companies that use ViewComponent.
37
+
38
+ *Nick Smith*
39
+
40
+ * Fix `#with_request_url` test helper not parsing nested query parameters into nested hashes.
41
+
42
+ *Richard Marbach*
43
+
44
+ ## 2.55.0
45
+
46
+ * Add `render_parent` convenience method to avoid confusion between `<%= super %>` and `<% super %>` in template code.
47
+
48
+ *Cameron Dutro*
49
+
50
+ * Add note about discouraging inheritance.
51
+
52
+ *Joel Hawksley*
53
+
54
+ * Clean up grammar in documentation.
55
+
56
+ *Joel Hawksley*
57
+
58
+ * The ViewComponent team at GitHub is hiring! We're looking for a Rails engineer with accessibility experience: [https://boards.greenhouse.io/github/jobs/4020166](https://boards.greenhouse.io/github/jobs/4020166). Reach out to joelhawksley@github.com with any questions!
59
+
60
+ * The ViewComponent team is hosting a happy hour at RailsConf. Join us for snacks, drinks, and stickers: [https://www.eventbrite.com/e/viewcomponent-happy-hour-tickets-304168585427](https://www.eventbrite.com/e/viewcomponent-happy-hour-tickets-304168585427)
61
+
12
62
  ## 2.54.1
13
63
 
14
64
  * Update docs dependencies.
@@ -26,7 +76,7 @@ title: Changelog
26
76
 
27
77
  *Blake Williams*
28
78
 
29
- * Add QuickNode to list of companies that heavily rely on ViewComponent.
79
+ * Add QuickNode to list of companies that use ViewComponent.
30
80
 
31
81
  *Luc Castera*
32
82
 
@@ -60,7 +110,7 @@ title: Changelog
60
110
 
61
111
  *Jason Swett*
62
112
 
63
- * Add Bearer to list of companies that heavily rely on ViewComponent.
113
+ * Add Bearer to list of companies that use ViewComponent.
64
114
 
65
115
  *Yaroslav Shmarov*
66
116
 
@@ -68,6 +118,10 @@ title: Changelog
68
118
 
69
119
  *Joel Hawksley*
70
120
 
121
+ * Enable rendering arbitrary block contents in the view context in tests.
122
+
123
+ *Cameron Dutro*
124
+
71
125
  ## 2.52.0
72
126
 
73
127
  * Add ADR for separate slot getter/setter API.
@@ -117,6 +117,20 @@ module ViewComponent
117
117
  render_template_for(@__vc_variant).to_s + _output_postamble
118
118
  end
119
119
 
120
+ # Subclass components that call `super` inside their template code will cause a
121
+ # double render if they accidentally emit the result:
122
+ #
123
+ # <%= super %> # double-renders
124
+ #
125
+ # <% super %> # does not double-render
126
+ #
127
+ # Calls `super`, returning nil to avoid rendering the result twice.
128
+ def render_parent
129
+ mtd = @__vc_variant ? "call_#{@__vc_variant}" : "call"
130
+ method(mtd).super_method.call
131
+ nil
132
+ end
133
+
120
134
  # :nocov:
121
135
  def render_template_for(variant = nil)
122
136
  # Force compilation here so the compiler always redefines render_template_for.
@@ -30,7 +30,8 @@ module ViewComponent # :nodoc:
30
30
  class << self
31
31
  # Returns all component preview classes.
32
32
  def all
33
- load_previews if descendants.empty?
33
+ load_previews
34
+
34
35
  descendants
35
36
  end
36
37
 
@@ -98,14 +99,14 @@ module ViewComponent # :nodoc:
98
99
  source[1...(source.size - 1)].join("\n")
99
100
  end
100
101
 
101
- private
102
-
103
102
  def load_previews
104
103
  Array(preview_paths).each do |preview_path|
105
104
  Dir["#{preview_path}/**/*_preview.rb"].sort.each { |file| require_dependency file }
106
105
  end
107
106
  end
108
107
 
108
+ private
109
+
109
110
  def preview_paths
110
111
  Base.preview_paths
111
112
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ViewComponent
4
+ module RenderPreviewHelper
5
+ # Render a preview inline. Internally sets `page` to be a `Capybara::Node::Simple`,
6
+ # allowing for Capybara assertions to be used:
7
+ #
8
+ # ```ruby
9
+ # render_preview(:default)
10
+ # assert_text("Hello, World!")
11
+ # ```
12
+ #
13
+ # Note: `#rendered_preview` expects a preview to be defined with the same class
14
+ # name as the calling test, but with `Test` replaced with `Preview`:
15
+ #
16
+ # MyComponentTest -> MyComponentPreview etc.
17
+ #
18
+ # @param preview [String] The name of the preview to be rendered.
19
+ # @return [Nokogiri::HTML]
20
+ def render_preview(name)
21
+ begin
22
+ preview_klass = self.class.name.gsub("Test", "Preview")
23
+ preview_klass = preview_klass.constantize
24
+ rescue NameError
25
+ raise NameError.new(
26
+ "`render_preview` expected to find #{preview_klass}, but it does not exist."
27
+ )
28
+ end
29
+
30
+ previews_controller = build_controller(ViewComponent::Base.preview_controller.constantize)
31
+ previews_controller.request.params[:path] = "#{preview_klass.preview_name}/#{name}"
32
+ previews_controller.response = ActionDispatch::Response.new
33
+ result = previews_controller.previews
34
+
35
+ @rendered_content = result
36
+
37
+ Nokogiri::HTML.fragment(@rendered_content)
38
+ end
39
+ end
40
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "view_component/render_preview_helper"
4
+
3
5
  module ViewComponent
4
6
  module TestHelpers
5
7
  begin
@@ -7,7 +9,7 @@ module ViewComponent
7
9
  include Capybara::Minitest::Assertions
8
10
 
9
11
  def page
10
- Capybara::Node::Simple.new(@rendered_component)
12
+ Capybara::Node::Simple.new(@rendered_content)
11
13
  end
12
14
 
13
15
  def refute_component_rendered
@@ -19,8 +21,8 @@ module ViewComponent
19
21
  # :nocov:
20
22
  if ENV["DEBUG"]
21
23
  warn(
22
- "WARNING in `ViewComponent::TestHelpers`: You must add `capybara` " \
23
- "to your Gemfile to use Capybara assertions."
24
+ "WARNING in `ViewComponent::TestHelpers`: Add `capybara` " \
25
+ "to Gemfile to use Capybara assertions."
24
26
  )
25
27
  end
26
28
 
@@ -28,7 +30,7 @@ module ViewComponent
28
30
  end
29
31
 
30
32
  # @private
31
- attr_reader :rendered_component
33
+ attr_reader :rendered_content
32
34
 
33
35
  # Render a component inline. Internally sets `page` to be a `Capybara::Node::Simple`,
34
36
  # allowing for Capybara assertions to be used:
@@ -41,14 +43,29 @@ module ViewComponent
41
43
  # @param component [ViewComponent::Base, ViewComponent::Collection] The instance of the component to be rendered.
42
44
  # @return [Nokogiri::HTML]
43
45
  def render_inline(component, **args, &block)
44
- @rendered_component =
46
+ @rendered_content =
45
47
  if Rails.version.to_f >= 6.1
46
48
  controller.view_context.render(component, args, &block)
47
49
  else
48
50
  controller.view_context.render_component(component, &block)
49
51
  end
50
52
 
51
- Nokogiri::HTML.fragment(@rendered_component)
53
+ Nokogiri::HTML.fragment(@rendered_content)
54
+ end
55
+
56
+ # Execute the given block in the view context. Internally sets `page` to be a
57
+ # `Capybara::Node::Simple`, allowing for Capybara assertions to be used:
58
+ #
59
+ # ```ruby
60
+ # render_in_view_context do
61
+ # render(MyComponent.new)
62
+ # end
63
+ #
64
+ # assert_text("Hello, World!")
65
+ # ```
66
+ def render_in_view_context(&block)
67
+ @rendered_content = controller.view_context.instance_exec(&block)
68
+ Nokogiri::HTML.fragment(@rendered_content)
52
69
  end
53
70
 
54
71
  # @private
@@ -121,7 +138,7 @@ module ViewComponent
121
138
 
122
139
  request.path_info = path
123
140
  request.path_parameters = Rails.application.routes.recognize_path(path)
124
- request.set_header("action_dispatch.request.query_parameters", Rack::Utils.parse_query(path.split("?")[1]))
141
+ request.set_header("action_dispatch.request.query_parameters", Rack::Utils.parse_nested_query(path.split("?")[1]))
125
142
  request.set_header(Rack::QUERY_STRING, path.split("?")[1])
126
143
  yield
127
144
  ensure
@@ -3,7 +3,7 @@
3
3
  module ViewComponent
4
4
  module VERSION
5
5
  MAJOR = 2
6
- MINOR = 54
6
+ MINOR = 56
7
7
  PATCH = 1
8
8
 
9
9
  STRING = [MAJOR, MINOR, PATCH].join(".")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: view_component
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.54.1
4
+ version: 2.56.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - GitHub Open Source
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-11 00:00:00.000000000 Z
11
+ date: 2022-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -345,6 +345,7 @@ files:
345
345
  - lib/view_component/render_component_helper.rb
346
346
  - lib/view_component/render_component_to_string_helper.rb
347
347
  - lib/view_component/render_monkey_patch.rb
348
+ - lib/view_component/render_preview_helper.rb
348
349
  - lib/view_component/render_to_string_monkey_patch.rb
349
350
  - lib/view_component/rendering_component_helper.rb
350
351
  - lib/view_component/rendering_monkey_patch.rb