view_component 2.36.0 → 2.40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

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: 74a9bb65a4c364b86c92249805629eb7dc5d7dad010e6743a461cd668d6897f3
4
- data.tar.gz: ab5bf1fa266b729faebf8bed8b8884300a8fca4462d4ab8a0c6a0dce32153a26
3
+ metadata.gz: a6b59c1dbd53f3cb893d36ca15cb6838e6b127a36c895d2e062f961929f03bb9
4
+ data.tar.gz: b32e3ecaa9f9734eded75141a75baeaa4396652e475827ff9db29dcf647e36c7
5
5
  SHA512:
6
- metadata.gz: eae497c864c6005b6ec9cea9073d2659b5378e0f0ebde82397a7e8a09e834525293f6b1a25a615198a28ae9ccfc077505c0bdb35def7cbea69aeaa3472dba7a3
7
- data.tar.gz: b11a86583fb7886828690d0d9d57ec9db3bf0f99b40f5cc0de5bb8fb6ca54ffd8e6fb65e150cdd2865c8cd71d46e13e9d7adbd3bd846d5ba5996a0a3c4ebf7d0
6
+ metadata.gz: e1e2312bb9dda96f1f52d6fa513514e461b2c3f9880ffa8321fcba69aabeaf1d87fc5199a7cf190c2596184051c0db30539b4600dbe2a15b5a560f76dc04240f
7
+ data.tar.gz: fc65722d4b20457c22de5d0603b80b171777c18bbae19c579f841b4aa092e6feb8e821da8832b6c2fb283d6a2fbca8929967f379b96d68d8ad94c8b171fa2293
data/docs/CHANGELOG.md CHANGED
@@ -7,6 +7,83 @@ title: Changelog
7
7
 
8
8
  ## main
9
9
 
10
+ ## 2.40.0
11
+
12
+ * Replace antipatterns section in the documentation with best practices.
13
+
14
+ *Blake Williams*
15
+
16
+ * Add components to `rails stats` task.
17
+
18
+ *Nicolas Brousse*
19
+
20
+ * Fix bug when using Slim and writing a slot whose block evaluates to `nil`.
21
+
22
+ *Yousuf Jukaku*
23
+
24
+ * Add documentation for test helpers.
25
+
26
+ *Joel Hawksley*
27
+
28
+ ## 2.39.0
29
+
30
+ * Clarify documentation of `with_variant` as an override of Action Pack.
31
+
32
+ *Blake Williams*, *Cameron Dutro*, *Joel Hawksley*
33
+
34
+ * Update docs page to be called Javascript and CSS, rename Building ViewComponents to Guide.
35
+
36
+ *Joel Hawksley*
37
+
38
+ * Deprecate `Base#with_variant`.
39
+
40
+ *Cameron Dutro*
41
+
42
+ * Error out in the CI if docs/api.md has to be regenerated.
43
+
44
+ *Dany Marcoux*
45
+
46
+ ## 2.38.0
47
+
48
+ * Add `--stimulus` flag to the component generator. Generates a Stimulus controller alongside the component.
49
+ * Add config option `config.view_component.generate_stimulus_controller` to always generate a Stimulus controller.
50
+
51
+ *Sebastien Auriault*
52
+
53
+ ## 2.37.0
54
+
55
+ * Clarify slots example in docs to reduce naming confusion.
56
+
57
+ *Joel Hawksley*, *Blake Williams*
58
+
59
+ * Fix error in documentation for `render_many` passthrough slots.
60
+
61
+ *Ollie Nye*
62
+
63
+ * Add test case for conflict with internal `@variant` variable.
64
+
65
+ *David Backeus*
66
+
67
+ * Document decision to not change naming convention recommendation to remove `-Component` suffix.
68
+
69
+ *Joel Hawksley*
70
+
71
+ * Fix typo in documentation.
72
+
73
+ *Ryo.gift*
74
+
75
+ * Add inline template example to benchmark script.
76
+
77
+ *Andrew Tait*
78
+
79
+ * Fix benchmark scripts.
80
+
81
+ *Andrew Tait*
82
+
83
+ * Run benchmarks in CI.
84
+
85
+ *Joel Hawksley*
86
+
10
87
  ## 2.36.0
11
88
 
12
89
  * Add `slot_type` helper method.
@@ -11,13 +11,21 @@ module ViewComponent
11
11
  private
12
12
 
13
13
  def destination
14
+ File.join(destination_directory, "#{destination_file_name}.html.#{engine_name}")
15
+ end
16
+
17
+ def destination_directory
14
18
  if options["sidecar"]
15
- File.join(component_path, class_path, "#{file_name}_component", "#{file_name}_component.html.#{engine_name}")
19
+ File.join(component_path, class_path, destination_file_name)
16
20
  else
17
- File.join(component_path, class_path, "#{file_name}_component.html.#{engine_name}")
21
+ File.join(component_path, class_path)
18
22
  end
19
23
  end
20
24
 
25
+ def destination_file_name
26
+ "#{file_name}_component"
27
+ end
28
+
21
29
  def file_name
22
30
  @_file_name ||= super.sub(/_component\z/i, "")
23
31
  end
@@ -25,5 +33,14 @@ module ViewComponent
25
33
  def component_path
26
34
  ViewComponent::Base.view_component_path
27
35
  end
36
+
37
+ def stimulus_controller
38
+ if options["stimulus"]
39
+ File.join(destination_directory, destination_file_name).
40
+ sub("#{component_path}/", "").
41
+ gsub("_", "-").
42
+ gsub("/", "--")
43
+ end
44
+ end
28
45
  end
29
46
  end
@@ -12,6 +12,8 @@ module Rails
12
12
  argument :attributes, type: :array, default: [], banner: "attribute"
13
13
  check_class_collision suffix: "Component"
14
14
  class_option :inline, type: :boolean, default: false
15
+ class_option :stimulus, type: :boolean, default: ViewComponent::Base.generate_stimulus_controller
16
+ class_option :sidecar, type: :boolean, default: false
15
17
 
16
18
  def create_component_file
17
19
  template "component.rb", File.join(component_path, class_path, "#{file_name}_component.rb")
@@ -21,6 +23,8 @@ module Rails
21
23
 
22
24
  hook_for :preview, type: :boolean
23
25
 
26
+ hook_for :stimulus, type: :boolean
27
+
24
28
  hook_for :template_engine do |instance, template_engine|
25
29
  instance.invoke template_engine, [instance.name]
26
30
  end
@@ -8,7 +8,7 @@ class <%= class_name %>Component < <%= parent_class %>
8
8
  <%- end -%>
9
9
  <%- if initialize_call_method_for_inline? -%>
10
10
  def call
11
- content_tag :h1, "Hello world!"
11
+ content_tag :h1, "Hello world!"<%= ", data: { controller: \"#{stimulus_controller}\" }" if options["stimulus"] %>
12
12
  end
13
13
  <%- end -%>
14
14
 
@@ -11,6 +11,7 @@ module Erb
11
11
  source_root File.expand_path("templates", __dir__)
12
12
  class_option :sidecar, type: :boolean, default: false
13
13
  class_option :inline, type: :boolean, default: false
14
+ class_option :stimulus, type: :boolean, default: false
14
15
 
15
16
  def engine_name
16
17
  "erb"
@@ -19,6 +20,14 @@ module Erb
19
20
  def copy_view_file
20
21
  super
21
22
  end
23
+
24
+ private
25
+
26
+ def data_attributes
27
+ if options["stimulus"]
28
+ " data-controller=\"#{stimulus_controller}\""
29
+ end
30
+ end
22
31
  end
23
32
  end
24
33
  end
@@ -1 +1 @@
1
- <div>Add <%= class_name %> template here</div>
1
+ <div<%= data_attributes %>>Add <%= class_name %> template here</div>
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stimulus
4
+ module Generators
5
+ class ComponentGenerator < ::Rails::Generators::NamedBase
6
+ include ViewComponent::AbstractGenerator
7
+
8
+ source_root File.expand_path("templates", __dir__)
9
+ class_option :sidecar, type: :boolean, default: false
10
+
11
+ def create_stimulus_controller
12
+ template "component_controller.js", destination
13
+ end
14
+
15
+ private
16
+
17
+ def destination
18
+ if options["sidecar"]
19
+ File.join(component_path, class_path, "#{file_name}_component", "#{file_name}_component_controller.js")
20
+ else
21
+ File.join(component_path, class_path, "#{file_name}_component_controller.js")
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,7 @@
1
+ import { Controller } from "stimulus";
2
+
3
+ export default class extends Controller {
4
+ connect() {
5
+ console.log("Hello, Stimulus!", this.element);
6
+ }
7
+ }
@@ -218,9 +218,14 @@ module ViewComponent
218
218
 
219
219
  # Use the provided variant instead of the one determined by the current request.
220
220
  #
221
+ # @deprecated Will be removed in v3.0.0.
221
222
  # @param variant [Symbol] The variant to be used by the component.
222
223
  # @return [self]
223
224
  def with_variant(variant)
225
+ ActiveSupport::Deprecation.warn(
226
+ "`with_variant` is deprecated and will be removed in ViewComponent v3.0.0."
227
+ )
228
+
224
229
  @__vc_variant = variant
225
230
 
226
231
  self
@@ -278,6 +283,14 @@ module ViewComponent
278
283
  #
279
284
  mattr_accessor :show_previews_source, instance_writer: false, default: false
280
285
 
286
+ # Always generate a Stimulus controller alongside the component:
287
+ #
288
+ # config.view_component.generate_stimulus_controller = true
289
+ #
290
+ # Defaults to `false`.
291
+ #
292
+ mattr_accessor :generate_stimulus_controller, instance_writer: false, default: false
293
+
281
294
  # Path for component files
282
295
  #
283
296
  # config.view_component.view_component_path = "app/my_components"
@@ -8,6 +8,10 @@ module ViewComponent
8
8
  config.view_component = ActiveSupport::OrderedOptions.new
9
9
  config.view_component.preview_paths ||= []
10
10
 
11
+ rake_tasks do
12
+ load "view_component/rails/tasks/view_component.rake"
13
+ end
14
+
11
15
  initializer "view_component.set_configs" do |app|
12
16
  options = app.config.view_component
13
17
 
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ task stats: "view_component:statsetup"
4
+
5
+ namespace :view_component do
6
+ task statsetup: :environment do
7
+ require "rails/code_statistics"
8
+
9
+ ::STATS_DIRECTORIES << ["ViewComponents", ViewComponent::Base.view_component_path]
10
+ end
11
+ end
@@ -64,7 +64,7 @@ module ViewComponent
64
64
  @__vc_content_set_by_with_content
65
65
  end
66
66
 
67
- @content
67
+ @content = @content.to_s
68
68
  end
69
69
 
70
70
  # Allow access to public component methods via the wrapper
@@ -27,8 +27,19 @@ module ViewComponent
27
27
  # :nocov:
28
28
  end
29
29
 
30
+ # @private
30
31
  attr_reader :rendered_component
31
32
 
33
+ # Render a component inline. Internally sets `page` to be a `Capybara::Node::Simple`,
34
+ # allowing for Capybara assertions to be used:
35
+ #
36
+ # ```ruby
37
+ # render_inline(MyComponent.new)
38
+ # assert_text("Hello, World!")
39
+ # ```
40
+ #
41
+ # @param component [ViewComponent::Base] The instance of the component to be rendered.
42
+ # @return [Nokogiri::HTML]
32
43
  def render_inline(component, **args, &block)
33
44
  @rendered_component =
34
45
  if Rails.version.to_f >= 6.1
@@ -40,10 +51,12 @@ module ViewComponent
40
51
  Nokogiri::HTML.fragment(@rendered_component)
41
52
  end
42
53
 
54
+ # @private
43
55
  def controller
44
56
  @controller ||= build_controller(Base.test_controller.constantize)
45
57
  end
46
58
 
59
+ # @private
47
60
  def request
48
61
  @request ||=
49
62
  begin
@@ -53,6 +66,15 @@ module ViewComponent
53
66
  end
54
67
  end
55
68
 
69
+ # Set the Action Pack request variant for the given block:
70
+ #
71
+ # ```ruby
72
+ # with_variant(:phone) do
73
+ # render_inline(MyComponent.new)
74
+ # end
75
+ # ```
76
+ #
77
+ # @param variant [Symbol] The variant to be set for the provided block.
56
78
  def with_variant(variant)
57
79
  old_variants = controller.view_context.lookup_context.variants
58
80
 
@@ -62,6 +84,16 @@ module ViewComponent
62
84
  controller.view_context.lookup_context.variants = old_variants
63
85
  end
64
86
 
87
+ # Set the controller to be used while executing the given block,
88
+ # allowing access to controller-specific methods:
89
+ #
90
+ # ```ruby
91
+ # with_controller_class(UsersController) do
92
+ # render_inline(MyComponent.new)
93
+ # end
94
+ # ```
95
+ #
96
+ # @param klass [ActionController::Base] The controller to be used.
65
97
  def with_controller_class(klass)
66
98
  old_controller = defined?(@controller) && @controller
67
99
 
@@ -71,6 +103,15 @@ module ViewComponent
71
103
  @controller = old_controller
72
104
  end
73
105
 
106
+ # Set the URL for the current request (such as when using request-dependent path helpers):
107
+ #
108
+ # ```ruby
109
+ # with_request_url("/users/42") do
110
+ # render_inline(MyComponent.new)
111
+ # end
112
+ # ```
113
+ #
114
+ # @param path [String] The path to set for the current request.
74
115
  def with_request_url(path)
75
116
  old_request_path_parameters = request.path_parameters
76
117
  old_controller = defined?(@controller) && @controller
@@ -82,6 +123,7 @@ module ViewComponent
82
123
  @controller = old_controller
83
124
  end
84
125
 
126
+ # @private
85
127
  def build_controller(klass)
86
128
  klass.new.tap { |c| c.request = request }.extend(Rails.application.routes.url_helpers)
87
129
  end
@@ -3,7 +3,7 @@
3
3
  module ViewComponent
4
4
  module VERSION
5
5
  MAJOR = 2
6
- MINOR = 36
6
+ MINOR = 40
7
7
  PATCH = 0
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.36.0
4
+ version: 2.40.0
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: 2021-07-28 00:00:00.000000000 Z
11
+ date: 2021-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -287,6 +287,8 @@ files:
287
287
  - lib/rails/generators/rspec/templates/component_spec.rb.tt
288
288
  - lib/rails/generators/slim/component_generator.rb
289
289
  - lib/rails/generators/slim/templates/component.html.slim.tt
290
+ - lib/rails/generators/stimulus/component_generator.rb
291
+ - lib/rails/generators/stimulus/templates/component_controller.js.tt
290
292
  - lib/rails/generators/test_unit/component_generator.rb
291
293
  - lib/rails/generators/test_unit/templates/component_test.rb.tt
292
294
  - lib/view_component.rb
@@ -301,6 +303,7 @@ files:
301
303
  - lib/view_component/preview.rb
302
304
  - lib/view_component/preview_template_error.rb
303
305
  - lib/view_component/previewable.rb
306
+ - lib/view_component/rails/tasks/view_component.rake
304
307
  - lib/view_component/render_component_helper.rb
305
308
  - lib/view_component/render_component_to_string_helper.rb
306
309
  - lib/view_component/render_monkey_patch.rb