view_component 2.14.0 → 2.17.1

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: d886f5b071663d4c5cd6bf96d94c02541d4fe86e470e79cc4ec51f1cfcc6c35b
4
- data.tar.gz: 01f24da54ab8ebd5a701be24b019e7cbbd97e38cffc74b380d9af4fc006558e5
3
+ metadata.gz: 76647927ca9d6adc1f23b0f7acc97f65e411ecf7c3d68ffb3b210ada62031b81
4
+ data.tar.gz: d76bb0c82db7d35dd6f49b4bff446a34b80a7058d238d0aa118d2dd9c47bd60a
5
5
  SHA512:
6
- metadata.gz: f45dab99819a3b0cd15a812ebe4f678173212b3eaf7d33e76a1c5523462a144a8f8a47cf7466581a629614706a3e9176de0dac7dbda4f63fc7a0a96658a5e55e
7
- data.tar.gz: ca67804e62bc095cd0a08373b1a5e415702173c770e9695b2242808546077211d571ccfda4c722031ad08c9d079d41eeaa627e0f206784648b3183debde71bd0
6
+ metadata.gz: 2a58fbf794f76e90f29d7b0fbf948041bcfd84df62629b21ee61372fe469cd5718228514ecd88b2db5f29d703a39b8c8f082bce0cea60649bac6dfea7ad76bc7
7
+ data.tar.gz: add095b1e488f653742934fb7c773951dd579e40884621ca538edc3b533d056c4fc6f6c61dc7488eb6a86cf7fb49a6f98093702693c5fc4148dfcedf9622f88e
@@ -1,5 +1,46 @@
1
1
  # master
2
2
 
3
+ # 2.17.1
4
+
5
+ * Fix bug where rendering Slot with empty block resulted in error.
6
+
7
+ *Joel Hawksley*
8
+
9
+ # 2.17.0
10
+
11
+ * Slots return stripped HTML, removing leading and trailing whitespace.
12
+
13
+ *Jason Long, Joel Hawksley*
14
+
15
+ # 2.16.0
16
+
17
+ * Add `--sidecar` option to the erb, haml and slim generators. Places the generated template in the sidecar directory.
18
+
19
+ *Michael van Rooijen*
20
+
21
+ # 2.15.0
22
+
23
+ * Add support for templates as ViewComponent::Preview examples.
24
+
25
+ *Juan Manuel Ramallo
26
+
27
+ # 2.14.1
28
+
29
+ * Allow using `render_inline` in test when the render monkey patch is disabled with `config.view_component.render_monkey_patch_enabled = false` in versions of Rails < 6.1.
30
+
31
+ *Clément Joubert*
32
+
33
+ * Fix kwargs warnings in slotable.
34
+
35
+ Fixes:
36
+
37
+ ```
38
+ view_component/lib/view_component/slotable.rb:98: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
39
+ view_component/test/app/components/slots_component.rb:18: warning: The called method `initialize' is defined here
40
+ ```
41
+
42
+ *Eileen M. Uchitelle*
43
+
3
44
  # 2.14.0
4
45
 
5
46
  * Add `config.preview_paths` to support multiple locations of component preview files. Deprecate `config.preview_path`.
data/README.md CHANGED
@@ -341,15 +341,25 @@ As an alternative, views and other assets can be placed in a sidecar directory w
341
341
  ```
342
342
  app/components
343
343
  ├── ...
344
- ├── test_component.rb
345
- ├── test_component
346
- | ├── test_component.css
347
- | ├── test_component.html.erb
348
- | └── test_component.js
344
+ ├── example_component.rb
345
+ ├── example_component
346
+ | ├── example_component.css
347
+ | ├── example_component.html.erb
348
+ | └── example_component.js
349
349
  ├── ...
350
350
 
351
351
  ```
352
352
 
353
+ To generate a component with a sidecar directory, use the `--sidecar` flag:
354
+
355
+ ```
356
+ bin/rails generate component Example title content --sidecar
357
+ invoke test_unit
358
+ create test/components/example_component_test.rb
359
+ create app/components/example_component.rb
360
+ create app/components/example_component/example_component.html.erb
361
+ ```
362
+
353
363
  ### Conditional Rendering
354
364
 
355
365
  Components can implement a `#render?` method to be called after initialization to determine if the component should render.
@@ -680,6 +690,57 @@ config.view_component.preview_route = "/previews"
680
690
 
681
691
  This example will make the previews available from <http://localhost:3000/previews>.
682
692
 
693
+ #### Preview templates
694
+
695
+ Given a preview `test/components/previews/cell_component_preview.rb`, template files can be defined at `test/components/previews/cell_component_preview/`:
696
+
697
+ `test/components/previews/cell_component_preview.rb`
698
+ ```ruby
699
+ class CellComponentPreview < ViewComponent::Preview
700
+ def default
701
+ end
702
+ end
703
+ ```
704
+
705
+ `test/components/previews/cell_component_preview/default.html.erb`
706
+ ```erb
707
+ <table class="table">
708
+ <tbody>
709
+ <tr>
710
+ <%= render CellComponent.new %>
711
+ </tr>
712
+ </tbody>
713
+ </div>
714
+ ```
715
+
716
+ To use a different location for preview templates, pass the `template` argument:
717
+ (the path should be relative to `config.view_component.preview_path`):
718
+
719
+ `test/components/previews/cell_component_preview.rb`
720
+ ```ruby
721
+ class CellComponentPreview < ViewComponent::Preview
722
+ def default
723
+ render_with_template(template: 'custom_cell_component_preview/my_preview_template')
724
+ end
725
+ end
726
+ ```
727
+
728
+ Values from `params` can be accessed through `locals`:
729
+
730
+ `test/components/previews/cell_component_preview.rb`
731
+ ```ruby
732
+ class CellComponentPreview < ViewComponent::Preview
733
+ def default(title: "Default title", subtitle: "A subtitle")
734
+ render_with_template(locals: {
735
+ title: title,
736
+ subtitle: subtitle
737
+ })
738
+ end
739
+ end
740
+ ```
741
+
742
+ Which enables passing in a value with <http://localhost:3000/rails/components/cell_component/default?title=Custom+title&subtitle=Another+subtitle>.
743
+
683
744
  #### Configuring TestController
684
745
 
685
746
  Component tests and previews assume the existence of an `ApplicationController` class, which be can be configured using the `test_controller` option:
@@ -929,10 +990,10 @@ ViewComponent is built by:
929
990
  |@maxbeizer|@franco|@tbroad-ramsey|@jensljungblad|@bbugh|
930
991
  |Nashville, TN|Switzerland|Spring Hill, TN|New York, NY|Austin, TX|
931
992
 
932
- |<img src="https://avatars.githubusercontent.com/johannesengl?s=256" alt="johannesengl" width="128" />|
933
- |:---:|
934
- |@johannesengl|
935
- |Berlin, Germany|
993
+ |<img src="https://avatars.githubusercontent.com/johannesengl?s=256" alt="johannesengl" width="128" />|<img src="https://avatars.githubusercontent.com/czj?s=256" alt="czj" width="128" />|<img src="https://avatars.githubusercontent.com/mrrooijen?s=256" alt="mrrooijen" width="128" />|
994
+ |:---:|:---:|:---:|
995
+ |@johannesengl|@czj|@mrrooijen|
996
+ |Berlin, Germany|Paris, France|The Netherlands|
936
997
 
937
998
  ## License
938
999
 
@@ -24,11 +24,16 @@ class ViewComponentsController < Rails::ApplicationController # :nodoc:
24
24
  render "view_components/previews"
25
25
  else
26
26
  prepend_application_view_paths
27
+ prepend_preview_examples_view_path
27
28
  @example_name = File.basename(params[:path])
28
29
  @render_args = @preview.render_args(@example_name, params: params.permit!)
29
30
  layout = @render_args[:layout]
30
- opts = layout.nil? ? {} : { layout: layout }
31
- render "view_components/preview", opts
31
+ template = @render_args[:template]
32
+ locals = @render_args[:locals]
33
+ opts = {}
34
+ opts[:layout] = layout if layout.present?
35
+ opts[:locals] = locals if locals.present?
36
+ render template, opts # rubocop:disable GitHub/RailsControllerRenderLiteral
32
37
  end
33
38
  end
34
39
 
@@ -59,4 +64,8 @@ class ViewComponentsController < Rails::ApplicationController # :nodoc:
59
64
  def prepend_application_view_paths
60
65
  prepend_view_path Rails.root.join("app/views") if defined?(Rails.root)
61
66
  end
67
+
68
+ def prepend_preview_examples_view_path
69
+ prepend_view_path(ViewComponent::Base.preview_paths)
70
+ end
62
71
  end
@@ -6,13 +6,22 @@ module Erb
6
6
  module Generators
7
7
  class ComponentGenerator < Base
8
8
  source_root File.expand_path("templates", __dir__)
9
+ class_option :sidecar, type: :boolean, default: false
9
10
 
10
11
  def copy_view_file
11
- template "component.html.erb", File.join("app/components", class_path, "#{file_name}_component.html.erb")
12
+ template "component.html.erb", destination
12
13
  end
13
14
 
14
15
  private
15
16
 
17
+ def destination
18
+ if options["sidecar"]
19
+ File.join("app/components", class_path, "#{file_name}_component", "#{file_name}_component.html.erb")
20
+ else
21
+ File.join("app/components", class_path, "#{file_name}_component.html.erb")
22
+ end
23
+ end
24
+
16
25
  def file_name
17
26
  @_file_name ||= super.sub(/_component\z/i, "")
18
27
  end
@@ -6,13 +6,22 @@ module Haml
6
6
  module Generators
7
7
  class ComponentGenerator < Erb::Generators::ComponentGenerator
8
8
  source_root File.expand_path("templates", __dir__)
9
+ class_option :sidecar, type: :boolean, default: false
9
10
 
10
11
  def copy_view_file
11
- template "component.html.haml", File.join("app/components", class_path, "#{file_name}_component.html.haml")
12
+ template "component.html.haml", destination
12
13
  end
13
14
 
14
15
  private
15
16
 
17
+ def destination
18
+ if options["sidecar"]
19
+ File.join("app/components", class_path, "#{file_name}_component", "#{file_name}_component.html.haml")
20
+ else
21
+ File.join("app/components", class_path, "#{file_name}_component.html.haml")
22
+ end
23
+ end
24
+
16
25
  def file_name
17
26
  @_file_name ||= super.sub(/_component\z/i, "")
18
27
  end
@@ -6,13 +6,22 @@ module Slim
6
6
  module Generators
7
7
  class ComponentGenerator < Erb::Generators::ComponentGenerator
8
8
  source_root File.expand_path("templates", __dir__)
9
+ class_option :sidecar, type: :boolean, default: false
9
10
 
10
11
  def copy_view_file
11
- template "component.html.slim", File.join("app/components", class_path, "#{file_name}_component.html.slim")
12
+ template "component.html.slim", destination
12
13
  end
13
14
 
14
15
  private
15
16
 
17
+ def destination
18
+ if options["sidecar"]
19
+ File.join("app/components", class_path, "#{file_name}_component", "#{file_name}_component.html.slim")
20
+ else
21
+ File.join("app/components", class_path, "#{file_name}_component.html.slim")
22
+ end
23
+ end
24
+
16
25
  def file_name
17
26
  @_file_name ||= super.sub(/_component\z/i, "")
18
27
  end
@@ -7,6 +7,7 @@ module ViewComponent
7
7
 
8
8
  autoload :Base
9
9
  autoload :Preview
10
+ autoload :PreviewTemplateError
10
11
  autoload :TestHelpers
11
12
  autoload :TestCase
12
13
  autoload :TemplateError
@@ -8,7 +8,20 @@ module ViewComponent # :nodoc:
8
8
  extend ActiveSupport::DescendantsTracker
9
9
 
10
10
  def render(component, **args, &block)
11
- { component: component, args: args, block: block }
11
+ {
12
+ args: args,
13
+ block: block,
14
+ component: component,
15
+ locals: {},
16
+ template: "view_components/preview",
17
+ }
18
+ end
19
+
20
+ def render_with_template(template: nil, locals: {})
21
+ {
22
+ template: template,
23
+ locals: locals
24
+ }
12
25
  end
13
26
 
14
27
  class << self
@@ -23,6 +36,8 @@ module ViewComponent # :nodoc:
23
36
  example_params_names = instance_method(example).parameters.map(&:last)
24
37
  provided_params = params.slice(*example_params_names).to_h.symbolize_keys
25
38
  result = provided_params.empty? ? new.public_send(example) : new.public_send(example, **provided_params)
39
+ result ||= {}
40
+ result[:template] = preview_example_template_path(example) if result[:template].nil?
26
41
  @layout = nil unless defined?(@layout)
27
42
  result.merge(layout: @layout)
28
43
  end
@@ -62,6 +77,20 @@ module ViewComponent # :nodoc:
62
77
  @layout = layout_name
63
78
  end
64
79
 
80
+ # Returns the relative path (from preview_path) to the preview example template if the template exists
81
+ def preview_example_template_path(example)
82
+ preview_path = Array(preview_paths).detect do |preview_path|
83
+ Dir["#{preview_path}/#{preview_name}_preview/#{example}.html.*"].first
84
+ end
85
+
86
+ if preview_path.nil?
87
+ raise PreviewTemplateError, "preview template for example #{example} does not exist"
88
+ end
89
+
90
+ path = Dir["#{preview_path}/#{preview_name}_preview/#{example}.html.*"].first
91
+ Pathname.new(path).relative_path_from(Pathname.new(preview_path)).to_s
92
+ end
93
+
65
94
  private
66
95
 
67
96
  def load_previews
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ViewComponent
4
+ class PreviewTemplateError < StandardError
5
+ end
6
+ end
@@ -95,10 +95,10 @@ module ViewComponent
95
95
  end
96
96
 
97
97
  # Instantiate Slot class, accommodating Slots that don't accept arguments
98
- slot_instance = args.present? ? slot_class.new(args) : slot_class.new
98
+ slot_instance = args.present? ? slot_class.new(**args) : slot_class.new
99
99
 
100
100
  # Capture block and assign to slot_instance#content
101
- slot_instance.content = view_context.capture(&block) if block_given?
101
+ slot_instance.content = view_context.capture(&block).to_s.strip.html_safe if block_given?
102
102
 
103
103
  if slot[:collection]
104
104
  # Initialize instance variable as an empty array
@@ -20,7 +20,12 @@ module ViewComponent
20
20
  attr_reader :rendered_component
21
21
 
22
22
  def render_inline(component, **args, &block)
23
- @rendered_component = controller.view_context.render(component, args, &block)
23
+ @rendered_component =
24
+ if Rails.version.to_f >= 6.1
25
+ controller.view_context.render(component, args, &block)
26
+ else
27
+ controller.view_context.render_component(component, &block)
28
+ end
24
29
 
25
30
  Nokogiri::HTML.fragment(@rendered_component)
26
31
  end
@@ -3,8 +3,8 @@
3
3
  module ViewComponent
4
4
  module VERSION
5
5
  MAJOR = 2
6
- MINOR = 14
7
- PATCH = 0
6
+ MINOR = 17
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.14.0
4
+ version: 2.17.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: 2020-07-07 00:00:00.000000000 Z
11
+ date: 2020-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -203,6 +203,7 @@ files:
203
203
  - lib/view_component/compile_cache.rb
204
204
  - lib/view_component/engine.rb
205
205
  - lib/view_component/preview.rb
206
+ - lib/view_component/preview_template_error.rb
206
207
  - lib/view_component/previewable.rb
207
208
  - lib/view_component/render_component_helper.rb
208
209
  - lib/view_component/render_component_to_string_helper.rb