view_component 2.37.0 → 2.38.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: 5f97acb8cd2ac4c3c706207aa030cfb62af6d9066b9d05ed32b0946851132367
4
- data.tar.gz: aeceea89a252646b358b5a67883fc926bffd56d321e0e3237fbc1471970c6745
3
+ metadata.gz: 7f5e17579fef222144f1b4566ee9061514f05879ac9a60fe405d5979624b1ae5
4
+ data.tar.gz: 40d115097a28b8340bef59b09f527357e3e2f8837178d268de0f43330be46cd4
5
5
  SHA512:
6
- metadata.gz: 8bc5f98ac1fc8ca620d2add5b59d5c0505489229cd0d8a7dcc9858774399c4e82e3dae419b5016b23a1228bec1e9e2e5a3e9d6980376439e5fb0cf02153ac8f9
7
- data.tar.gz: 4d7b0037a063825a777a94ca03e32e89abcadc04bb0c5ec2265f5eb8a1b62f7abb1e807ace75726365ab8baebdd774dd5fc757da2b0cd6ce51a5670b03309ff2
6
+ metadata.gz: 407e8525abd5a6ef76f5a0178c837f855494565030cd46a5c076bcaa8b88f3888a3d6771e6d461c36a207481e547f6cf286eccc658b4667d13073f3b1ed8796d
7
+ data.tar.gz: 2f850ee69467621b7a3989f7045160efa5d342e520df04ba6f91044a5e48f6b8df58a573b1555aea8da0c3f4bf6a4a407ffecca92c46def9c906104473415d4f
data/docs/CHANGELOG.md CHANGED
@@ -7,6 +7,13 @@ title: Changelog
7
7
 
8
8
  ## main
9
9
 
10
+ ## 2.38.0
11
+
12
+ * Add `--stimulus` flag to the component generator. Generates a Stimulus controller alongside the component.
13
+ * Add config option `config.view_component.generate_stimulus_controller` to always generate a Stimulus controller.
14
+
15
+ *Sebastien Auriault*
16
+
10
17
  ## 2.37.0
11
18
 
12
19
  * Clarify slots example in docs to reduce naming confusion.
@@ -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
+ }
@@ -278,6 +278,14 @@ module ViewComponent
278
278
  #
279
279
  mattr_accessor :show_previews_source, instance_writer: false, default: false
280
280
 
281
+ # Always generate a Stimulus controller alongside the component:
282
+ #
283
+ # config.view_component.generate_stimulus_controller = true
284
+ #
285
+ # Defaults to `false`.
286
+ #
287
+ mattr_accessor :generate_stimulus_controller, instance_writer: false, default: false
288
+
281
289
  # Path for component files
282
290
  #
283
291
  # config.view_component.view_component_path = "app/my_components"
@@ -3,7 +3,7 @@
3
3
  module ViewComponent
4
4
  module VERSION
5
5
  MAJOR = 2
6
- MINOR = 37
6
+ MINOR = 38
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.37.0
4
+ version: 2.38.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-08-09 00:00:00.000000000 Z
11
+ date: 2021-08-12 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
@@ -338,7 +340,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
338
340
  - !ruby/object:Gem::Version
339
341
  version: '0'
340
342
  requirements: []
341
- rubygems_version: 3.1.4
343
+ rubygems_version: 3.2.22
342
344
  signing_key:
343
345
  specification_version: 4
344
346
  summary: View components for Rails