view_component 2.15.0 → 2.18.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: 9e01b3b2899158831379c51c72086d84ca838b829a9638eeaec381aa287fa8c8
4
- data.tar.gz: fab254768f2f88651ed2c89e9e09ef5fa7e50524dad061435e5abd4e7de1a4ae
3
+ metadata.gz: 1b7c014848a75fb2a72a784a34b2c264de34a510c97c446614285f1854cd995a
4
+ data.tar.gz: 7a58ac8988ff8fea69e366ce21b2bff7a9f0a54bd4411f89630a701433064327
5
5
  SHA512:
6
- metadata.gz: b1ce90df482ab031bf57b808ec1cd0015774d3061733ae4e934d18a8b569e0aa6d22ac71107016ca0ab73f76acb720c7113d21daba2a74a5d313f53ae245e435
7
- data.tar.gz: 96e33d8a65235bf8831ce07aec2f13b5bec2617be30c2a11cca2fa969516d167be4eaf2390a6296db2f1e61f1523115e2fd53aa6af1e39a6d661f85a03baf626
6
+ metadata.gz: ed456c91f637872701a85eb71ed7d9dc0e12453ece026fcba2c2c4c2fedc1bbd1769caed36735cd90ac42b68582b3379e4acede29b749d65813d38e60cd01afc
7
+ data.tar.gz: cb73cf684f8e10a49cd56b7e001688f8a05aeb842604b5d0204b0b0f4c8017abda00556665a02a163c0a1019107f1beb13a2a32ea4492c38efedc87e4af336a0
@@ -1,5 +1,43 @@
1
1
  # master
2
2
 
3
+ # 2.18.1
4
+
5
+ * Fix bug where previews didn't work when monkey patch was disabled.
6
+
7
+ *Mixer Gutierrez*
8
+
9
+ # 2.18.0
10
+
11
+ * Fix auto-loading of previews (changes no longer require a server restart)
12
+
13
+ *Matt Brictson*
14
+
15
+ * Add `default_preview_layout` configuration option to load custom app/views/layouts.
16
+
17
+ *Jared White, Juan Manuel Ramallo*
18
+
19
+ * Calculate virtual_path once for all instances of a component class to improve performance.
20
+
21
+ *Brad Parker*
22
+
23
+ # 2.17.1
24
+
25
+ * Fix bug where rendering Slot with empty block resulted in error.
26
+
27
+ *Joel Hawksley*
28
+
29
+ # 2.17.0
30
+
31
+ * Slots return stripped HTML, removing leading and trailing whitespace.
32
+
33
+ *Jason Long, Joel Hawksley*
34
+
35
+ # 2.16.0
36
+
37
+ * Add `--sidecar` option to the erb, haml and slim generators. Places the generated template in the sidecar directory.
38
+
39
+ *Michael van Rooijen*
40
+
3
41
  # 2.15.0
4
42
 
5
43
  * Add support for templates as ViewComponent::Preview examples.
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.
@@ -664,6 +674,14 @@ class TestComponentPreview < ViewComponent::Preview
664
674
  end
665
675
  ```
666
676
 
677
+ You can also set a custom layout to be used by default for previews as well as the preview index pages via the `default_preview_layout` configuration option:
678
+
679
+ `config/application.rb`
680
+ ```ruby
681
+ # Set the default layout to app/views/layouts/component_preview.html.erb
682
+ config.view_component.default_preview_layout = "component_preview"
683
+ ```
684
+
667
685
  Preview classes live in `test/components/previews`, which can be configured using the `preview_paths` option:
668
686
 
669
687
  `config/application.rb`
@@ -980,10 +998,10 @@ ViewComponent is built by:
980
998
  |@maxbeizer|@franco|@tbroad-ramsey|@jensljungblad|@bbugh|
981
999
  |Nashville, TN|Switzerland|Spring Hill, TN|New York, NY|Austin, TX|
982
1000
 
983
- |<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" />|
984
- |:---:|:---:|
985
- |@johannesengl|@czj|
986
- |Berlin, Germany|Paris, France|
1001
+ |<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" />|<img src="https://avatars.githubusercontent.com/bradparker?s=256" alt="bradparker" width="128" />|<img src="https://avatars.githubusercontent.com/mattbrictson?s=256" alt="mattbrictson" width="128" />|
1002
+ |:---:|:---:|:---:|:---:|:---:|
1003
+ |@johannesengl|@czj|@mrrooijen|@bradparker|@mattbrictson|
1004
+ |Berlin, Germany|Paris, France|The Netherlands|Brisbane, Australia|San Francisco|
987
1005
 
988
1006
  ## License
989
1007
 
@@ -16,22 +16,23 @@ class ViewComponentsController < Rails::ApplicationController # :nodoc:
16
16
  def index
17
17
  @previews = ViewComponent::Preview.all
18
18
  @page_title = "Component Previews"
19
+ render "view_components/index", **determine_layout
19
20
  end
20
21
 
21
22
  def previews
22
23
  if params[:path] == @preview.preview_name
23
24
  @page_title = "Component Previews for #{@preview.preview_name}"
24
- render "view_components/previews"
25
+ render "view_components/previews", **determine_layout
25
26
  else
26
27
  prepend_application_view_paths
27
28
  prepend_preview_examples_view_path
28
29
  @example_name = File.basename(params[:path])
29
30
  @render_args = @preview.render_args(@example_name, params: params.permit!)
30
- layout = @render_args[:layout]
31
+ layout = determine_layout(@render_args[:layout], prepend_views: false)[:layout]
31
32
  template = @render_args[:template]
32
33
  locals = @render_args[:locals]
33
34
  opts = {}
34
- opts[:layout] = layout if layout.present?
35
+ opts[:layout] = layout if layout.present? || layout == false
35
36
  opts[:locals] = locals if locals.present?
36
37
  render template, opts # rubocop:disable GitHub/RailsControllerRenderLiteral
37
38
  end
@@ -39,6 +40,10 @@ class ViewComponentsController < Rails::ApplicationController # :nodoc:
39
40
 
40
41
  private
41
42
 
43
+ def default_preview_layout # :doc:
44
+ ViewComponent::Base.default_preview_layout
45
+ end
46
+
42
47
  def show_previews? # :doc:
43
48
  ViewComponent::Base.show_previews
44
49
  end
@@ -61,6 +66,24 @@ class ViewComponentsController < Rails::ApplicationController # :nodoc:
61
66
  end
62
67
  end
63
68
 
69
+ # Returns either {} or {layout: value} depending on configuration
70
+ def determine_layout(layout_override = nil, prepend_views: true)
71
+ return {} unless defined?(Rails.root)
72
+
73
+ layout_declaration = {}
74
+
75
+ if !layout_override.nil?
76
+ # Allow component-level override, even if false (thus no layout rendered)
77
+ layout_declaration[:layout] = layout_override
78
+ elsif default_preview_layout.present?
79
+ layout_declaration[:layout] = default_preview_layout
80
+ end
81
+
82
+ prepend_application_view_paths if layout_declaration[:layout].present? && prepend_views
83
+
84
+ layout_declaration
85
+ end
86
+
64
87
  def prepend_application_view_paths
65
88
  prepend_view_path Rails.root.join("app/views") if defined?(Rails.root)
66
89
  end
@@ -1 +1,5 @@
1
- <%= render(@render_args[:component], @render_args[:args], &@render_args[:block])%>
1
+ <% if ViewComponent::Base.render_monkey_patch_enabled || Rails.version.to_f >= 6.1 %>
2
+ <%= render(@render_args[:component], @render_args[:args], &@render_args[:block])%>
3
+ <% else %>
4
+ <%= render_component(@render_args[:component], &@render_args[:block])%>
5
+ <% 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
@@ -116,9 +116,9 @@ module ViewComponent
116
116
  @helpers ||= controller.view_context
117
117
  end
118
118
 
119
- # Removes the first part of the path and the extension.
119
+ # Exposes .virutal_path as an instance method
120
120
  def virtual_path
121
- self.class.source_location.gsub(%r{(.*app/components)|(\.rb)}, "")
121
+ self.class.virtual_path
122
122
  end
123
123
 
124
124
  # For caching, such as #cache_if
@@ -166,7 +166,7 @@ module ViewComponent
166
166
  mattr_accessor :render_monkey_patch_enabled, instance_writer: false, default: true
167
167
 
168
168
  class << self
169
- attr_accessor :source_location
169
+ attr_accessor :source_location, :virtual_path
170
170
 
171
171
  # Render a component collection.
172
172
  def with_collection(collection, **args)
@@ -189,6 +189,9 @@ module ViewComponent
189
189
  # has been re-defined by the consuming application, likely in ApplicationComponent.
190
190
  child.source_location = caller_locations(1, 10).reject { |l| l.label == "inherited" }[0].absolute_path
191
191
 
192
+ # Removes the first part of the path and the extension.
193
+ child.virtual_path = child.source_location.gsub(%r{(.*app/components)|(\.rb)}, "")
194
+
192
195
  # Clone slot configuration into child class
193
196
  # see #test_slots_pollution
194
197
  child.slots = self.slots.clone
@@ -34,8 +34,8 @@ module ViewComponent
34
34
  initializer "view_component.set_autoload_paths" do |app|
35
35
  options = app.config.view_component
36
36
 
37
- if options.show_previews && options.preview_path
38
- ActiveSupport::Dependencies.autoload_paths << options.preview_path
37
+ if options.show_previews && !options.preview_paths.empty?
38
+ ActiveSupport::Dependencies.autoload_paths.concat(options.preview_paths)
39
39
  end
40
40
  end
41
41
 
@@ -24,6 +24,8 @@ module ViewComponent # :nodoc:
24
24
  }
25
25
  end
26
26
 
27
+ alias_method :render_component, :render
28
+
27
29
  class << self
28
30
  # Returns all component preview classes.
29
31
  def all
@@ -7,6 +7,14 @@ module ViewComponent # :nodoc:
7
7
  extend ActiveSupport::Concern
8
8
 
9
9
  included do
10
+ # Set a custom default preview layout through app configuration:
11
+ #
12
+ # config.view_component.default_preview_layout = "component_preview"
13
+ #
14
+ # This affects preview index pages as well as individual component previews
15
+ #
16
+ mattr_accessor :default_preview_layout, instance_writer: false
17
+
10
18
  # Set the location of component previews through app configuration:
11
19
  #
12
20
  # config.view_component.preview_paths << "#{Rails.root}/lib/component_previews"
@@ -98,7 +98,7 @@ module ViewComponent
98
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
@@ -3,8 +3,8 @@
3
3
  module ViewComponent
4
4
  module VERSION
5
5
  MAJOR = 2
6
- MINOR = 15
7
- PATCH = 0
6
+ MINOR = 18
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.15.0
4
+ version: 2.18.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - GitHub Open Source
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-13 00:00:00.000000000 Z
11
+ date: 2020-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -170,7 +170,7 @@ dependencies:
170
170
  - - "~>"
171
171
  - !ruby/object:Gem::Version
172
172
  version: '0.1'
173
- description:
173
+ description:
174
174
  email:
175
175
  - opensource+view_component@github.com
176
176
  executables: []
@@ -222,7 +222,7 @@ licenses:
222
222
  - MIT
223
223
  metadata:
224
224
  allowed_push_host: https://rubygems.org
225
- post_install_message:
225
+ post_install_message:
226
226
  rdoc_options: []
227
227
  require_paths:
228
228
  - lib
@@ -238,7 +238,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
238
238
  version: '0'
239
239
  requirements: []
240
240
  rubygems_version: 3.1.2
241
- signing_key:
241
+ signing_key:
242
242
  specification_version: 4
243
243
  summary: View components for Rails
244
244
  test_files: []