view_component 2.14.1 → 2.15.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '084152c558b71f87f41d620f9cdce42e689e0b4159cdfa68f3cb041a7bbea3ba'
4
- data.tar.gz: 45324159a07873ce92562650fe7c2ad6e8a279c3db216b3460a6075f71033180
3
+ metadata.gz: 9e01b3b2899158831379c51c72086d84ca838b829a9638eeaec381aa287fa8c8
4
+ data.tar.gz: fab254768f2f88651ed2c89e9e09ef5fa7e50524dad061435e5abd4e7de1a4ae
5
5
  SHA512:
6
- metadata.gz: 58f994aac7efdf81146972a351a12726131658e4ccbd8bf0661eb135d7935ff396c64752d9f9edd6dda50b90a4829aeb506f24edee712279aa1cf4bf4f15886d
7
- data.tar.gz: 430b55482aaafaea6a27e73593861f897620cb635c2a2f14f14b5a772846a8f5a7b42bd973f637cd587816762891bcc432ced92dbac04fe1d391bf7734873a6a
6
+ metadata.gz: b1ce90df482ab031bf57b808ec1cd0015774d3061733ae4e934d18a8b569e0aa6d22ac71107016ca0ab73f76acb720c7113d21daba2a74a5d313f53ae245e435
7
+ data.tar.gz: 96e33d8a65235bf8831ce07aec2f13b5bec2617be30c2a11cca2fa969516d167be4eaf2390a6296db2f1e61f1523115e2fd53aa6af1e39a6d661f85a03baf626
@@ -1,5 +1,11 @@
1
1
  # master
2
2
 
3
+ # 2.15.0
4
+
5
+ * Add support for templates as ViewComponent::Preview examples.
6
+
7
+ *Juan Manuel Ramallo
8
+
3
9
  # 2.14.1
4
10
 
5
11
  * 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.
data/README.md CHANGED
@@ -680,6 +680,57 @@ config.view_component.preview_route = "/previews"
680
680
 
681
681
  This example will make the previews available from <http://localhost:3000/previews>.
682
682
 
683
+ #### Preview templates
684
+
685
+ Given a preview `test/components/previews/cell_component_preview.rb`, template files can be defined at `test/components/previews/cell_component_preview/`:
686
+
687
+ `test/components/previews/cell_component_preview.rb`
688
+ ```ruby
689
+ class CellComponentPreview < ViewComponent::Preview
690
+ def default
691
+ end
692
+ end
693
+ ```
694
+
695
+ `test/components/previews/cell_component_preview/default.html.erb`
696
+ ```erb
697
+ <table class="table">
698
+ <tbody>
699
+ <tr>
700
+ <%= render CellComponent.new %>
701
+ </tr>
702
+ </tbody>
703
+ </div>
704
+ ```
705
+
706
+ To use a different location for preview templates, pass the `template` argument:
707
+ (the path should be relative to `config.view_component.preview_path`):
708
+
709
+ `test/components/previews/cell_component_preview.rb`
710
+ ```ruby
711
+ class CellComponentPreview < ViewComponent::Preview
712
+ def default
713
+ render_with_template(template: 'custom_cell_component_preview/my_preview_template')
714
+ end
715
+ end
716
+ ```
717
+
718
+ Values from `params` can be accessed through `locals`:
719
+
720
+ `test/components/previews/cell_component_preview.rb`
721
+ ```ruby
722
+ class CellComponentPreview < ViewComponent::Preview
723
+ def default(title: "Default title", subtitle: "A subtitle")
724
+ render_with_template(locals: {
725
+ title: title,
726
+ subtitle: subtitle
727
+ })
728
+ end
729
+ end
730
+ ```
731
+
732
+ Which enables passing in a value with <http://localhost:3000/rails/components/cell_component/default?title=Custom+title&subtitle=Another+subtitle>.
733
+
683
734
  #### Configuring TestController
684
735
 
685
736
  Component tests and previews assume the existence of an `ApplicationController` class, which be can be configured using the `test_controller` option:
@@ -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
@@ -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
@@ -3,8 +3,8 @@
3
3
  module ViewComponent
4
4
  module VERSION
5
5
  MAJOR = 2
6
- MINOR = 14
7
- PATCH = 1
6
+ MINOR = 15
7
+ PATCH = 0
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.1
4
+ version: 2.15.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: 2020-07-10 00:00:00.000000000 Z
11
+ date: 2020-07-13 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