view_component 2.55.0 → 2.57.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: c6460c94f3bb4f2b19cd5311d957d2187e437276c0c19b657834cccba39fb36f
4
- data.tar.gz: c0dfc55481d2bcfcdc2f54ea4f4bacc94780e2dce39b77eaf5828490c1d0fc52
3
+ metadata.gz: 1c0dea17d60642f97d9edad061c538054015eac0f774bc58d37ba30c177a76ac
4
+ data.tar.gz: bb12f7080b7c90ab0e475550bf44577ef25d2b1e0d0966cc5f07c272d9ea5fec
5
5
  SHA512:
6
- metadata.gz: 6ffed42bf70668cf3dbf811f5f24e4a9e2e6cca7fdaea3674f8323c08a60b0f7c49f94c9a1d0bb1c962fb04c0c6dbdb219891116b70385bb8d3fe5f650d86f91
7
- data.tar.gz: 416501060d316e680dbf7f65543aa9a57b693e13bf8a9ebd8c244794155b33702863b22723d140bb85df560e4b18d746f3d534a9a98f4698ced8435cecce3375
6
+ metadata.gz: 3eb53d5be8bdb0ab083f0c57938bda3df2094c6368619efb59254bd8ce6a749ffcd4d1dab653b80be48a17f361fa15611c9086c2a526891d81e92527ffd67c9d
7
+ data.tar.gz: 418af4138439260637f53b025de631a11f222a699030afe36d3b51b7a31c5f451e7d3eb2ec5c6d13fa39b9b44e12e27fae972b631c92133bfbf054079695b80d
@@ -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,65 @@ title: Changelog
9
9
 
10
10
  ## main
11
11
 
12
+ ## 2.57.1
13
+
14
+ * Fix issue causing `NoMethodError`s when calling helper methods from components rendered as part of a collection.
15
+ * Fix syntax error in the ERB example in the polymorphic slots docs.
16
+
17
+ *Cameron Dutro*
18
+
19
+ ## 2.57.0
20
+
21
+ * Add missing `require` for `Translatable` module in `Base`.
22
+
23
+ *Hans Lemuet*
24
+
25
+ * Allow anything that responds to `#render_in` to be rendered in the parent component's view context.
26
+
27
+ *Cameron Dutro*
28
+
29
+ * Fix script/release so it honors semver.
30
+
31
+ *Cameron Dutro*
32
+
33
+ ## 2.56.2
34
+
35
+ * Restore removed `rendered_component`, marking it for deprecation in v3.0.0.
36
+
37
+ *Tyson Gach*, *Richard Macklin*, *Joel Hawksley*
38
+
39
+ ## 2.56.1
40
+
41
+ * Rename private accessor `rendered_component` to `rendered_content`.
42
+
43
+ *Yoshiyuki Hirano*, *Simon Dawson*
44
+
45
+ ## 2.56.0
46
+
47
+ * Introduce experimental `render_preview` test helper. Note: `@rendered_component` in `TestHelpers` has been renamed to `@rendered_content`.
48
+
49
+ *Joel Hawksley*
50
+
51
+ * Move framework tests into sandbox application.
52
+
53
+ *Joel Hawksley*
54
+
55
+ * Add G2 to list of companies that use ViewComponent.
56
+
57
+ *Jack Shuff*
58
+
59
+ * Add Within3 to list of companies that use ViewComponent.
60
+
61
+ *Drew Bragg*
62
+
63
+ * Add Mission Met to list of companies that use ViewComponent.
64
+
65
+ *Nick Smith*
66
+
67
+ * Fix `#with_request_url` test helper not parsing nested query parameters into nested hashes.
68
+
69
+ *Richard Marbach*
70
+
12
71
  ## 2.55.0
13
72
 
14
73
  * Add `render_parent` convenience method to avoid confusion between `<%= super %>` and `<% super %>` in template code.
@@ -9,6 +9,7 @@ require "view_component/polymorphic_slots"
9
9
  require "view_component/previewable"
10
10
  require "view_component/slotable"
11
11
  require "view_component/slotable_v2"
12
+ require "view_component/translatable"
12
13
  require "view_component/with_content_helper"
13
14
 
14
15
  module ViewComponent
@@ -32,6 +33,19 @@ module ViewComponent
32
33
 
33
34
  attr_accessor :__vc_original_view_context
34
35
 
36
+ # Components render in their own view context. Helpers and other functionality
37
+ # require a reference to the original Rails view context, an instance of
38
+ # `ActionView::Base`. Use this method to set a reference to the original
39
+ # view context. Objects that implement this method will render in the component's
40
+ # view context, while objects that don't will render in the original view context
41
+ # so helpers, etc work as expected.
42
+ #
43
+ # @param view_context [ActionView::Base] The original view context.
44
+ # @return [void]
45
+ def set_original_view_context(view_context)
46
+ self.__vc_original_view_context = view_context
47
+ end
48
+
35
49
  # EXPERIMENTAL: This API is experimental and may be removed at any time.
36
50
  # Hook for allowing components to do work as part of the compilation process.
37
51
  #
@@ -113,24 +127,25 @@ module ViewComponent
113
127
  @current_template = old_current_template
114
128
  end
115
129
 
130
+ # @private
116
131
  def perform_render
117
132
  render_template_for(@__vc_variant).to_s + _output_postamble
118
133
  end
119
134
 
120
135
  # 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
136
+ # double render if they emit the result:
124
137
  #
125
- # <% super %> # does not double-render
138
+ # <%= super %> # double-renders
139
+ # <% super %> # does not double-render
126
140
  #
127
- # Calls `super`, returning nil to avoid rendering the result twice.
141
+ # Calls `super`, returning `nil` to avoid rendering the result twice.
128
142
  def render_parent
129
143
  mtd = @__vc_variant ? "call_#{@__vc_variant}" : "call"
130
144
  method(mtd).super_method.call
131
145
  nil
132
146
  end
133
147
 
148
+ # @private
134
149
  # :nocov:
135
150
  def render_template_for(variant = nil)
136
151
  # Force compilation here so the compiler always redefines render_template_for.
@@ -182,8 +197,8 @@ module ViewComponent
182
197
  #
183
198
  # @private
184
199
  def render(options = {}, args = {}, &block)
185
- if options.is_a? ViewComponent::Base
186
- options.__vc_original_view_context = __vc_original_view_context
200
+ if options.respond_to?(:set_original_view_context)
201
+ options.set_original_view_context(self.__vc_original_view_context)
187
202
  super
188
203
  else
189
204
  __vc_original_view_context.render(options, args, &block)
@@ -10,8 +10,15 @@ module ViewComponent
10
10
  delegate :format, to: :component
11
11
  delegate :size, to: :@collection
12
12
 
13
+ attr_accessor :__vc_original_view_context
14
+
15
+ def set_original_view_context(view_context)
16
+ self.__vc_original_view_context = view_context
17
+ end
18
+
13
19
  def render_in(view_context, &block)
14
20
  components.map do |component|
21
+ component.set_original_view_context(self.__vc_original_view_context)
15
22
  component.render_in(view_context, &block)
16
23
  end.join.html_safe # rubocop:disable Rails/OutputSafety
17
24
  end
@@ -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
@@ -28,7 +30,16 @@ module ViewComponent
28
30
  end
29
31
 
30
32
  # @private
31
- attr_reader :rendered_component
33
+ attr_reader :rendered_content
34
+
35
+ def rendered_component
36
+ ViewComponent::Deprecation.warn(
37
+ "`rendered_component` is deprecated and will be removed in v3.0.0. " \
38
+ "Use `page` instead."
39
+ )
40
+
41
+ rendered_content
42
+ end
32
43
 
33
44
  # Render a component inline. Internally sets `page` to be a `Capybara::Node::Simple`,
34
45
  # allowing for Capybara assertions to be used:
@@ -41,14 +52,14 @@ module ViewComponent
41
52
  # @param component [ViewComponent::Base, ViewComponent::Collection] The instance of the component to be rendered.
42
53
  # @return [Nokogiri::HTML]
43
54
  def render_inline(component, **args, &block)
44
- @rendered_component =
55
+ @rendered_content =
45
56
  if Rails.version.to_f >= 6.1
46
57
  controller.view_context.render(component, args, &block)
47
58
  else
48
59
  controller.view_context.render_component(component, &block)
49
60
  end
50
61
 
51
- Nokogiri::HTML.fragment(@rendered_component)
62
+ Nokogiri::HTML.fragment(@rendered_content)
52
63
  end
53
64
 
54
65
  # Execute the given block in the view context. Internally sets `page` to be a
@@ -62,8 +73,8 @@ module ViewComponent
62
73
  # assert_text("Hello, World!")
63
74
  # ```
64
75
  def render_in_view_context(&block)
65
- @rendered_component = controller.view_context.instance_exec(&block)
66
- Nokogiri::HTML.fragment(@rendered_component)
76
+ @rendered_content = controller.view_context.instance_exec(&block)
77
+ Nokogiri::HTML.fragment(@rendered_content)
67
78
  end
68
79
 
69
80
  # @private
@@ -136,7 +147,7 @@ module ViewComponent
136
147
 
137
148
  request.path_info = path
138
149
  request.path_parameters = Rails.application.routes.recognize_path(path)
139
- request.set_header("action_dispatch.request.query_parameters", Rack::Utils.parse_query(path.split("?")[1]))
150
+ request.set_header("action_dispatch.request.query_parameters", Rack::Utils.parse_nested_query(path.split("?")[1]))
140
151
  request.set_header(Rack::QUERY_STRING, path.split("?")[1])
141
152
  yield
142
153
  ensure
@@ -3,8 +3,8 @@
3
3
  module ViewComponent
4
4
  module VERSION
5
5
  MAJOR = 2
6
- MINOR = 55
7
- PATCH = 0
6
+ MINOR = 57
7
+ PATCH = 1
8
8
 
9
9
  STRING = [MAJOR, MINOR, PATCH].join(".")
10
10
  end
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.55.0
4
+ version: 2.57.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-13 00:00:00.000000000 Z
11
+ date: 2022-06-15 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