view_component 2.17.1 → 2.18.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: 76647927ca9d6adc1f23b0f7acc97f65e411ecf7c3d68ffb3b210ada62031b81
4
- data.tar.gz: d76bb0c82db7d35dd6f49b4bff446a34b80a7058d238d0aa118d2dd9c47bd60a
3
+ metadata.gz: 6fcbca08ed9ca2f6cf62a7ad154dc297ba4606a62faf0aebeeaa544c2c76a663
4
+ data.tar.gz: 677f442dbcf86d4ca7cfbf4d3ad71a8cb92756ddfeb85afb3b973bd1238a56b2
5
5
  SHA512:
6
- metadata.gz: 2a58fbf794f76e90f29d7b0fbf948041bcfd84df62629b21ee61372fe469cd5718228514ecd88b2db5f29d703a39b8c8f082bce0cea60649bac6dfea7ad76bc7
7
- data.tar.gz: add095b1e488f653742934fb7c773951dd579e40884621ca538edc3b533d056c4fc6f6c61dc7488eb6a86cf7fb49a6f98093702693c5fc4148dfcedf9622f88e
6
+ metadata.gz: bd47863117cc291707ccee9b3bed69daeea0dd432cead045f6d8211ad201bbec34a23132836815751f9a5df64ab92fd3724dc3bf61e03282741d3d468e7c1faa
7
+ data.tar.gz: 150ebb993d9cdca790ffa437540cb454bea5021350a5a5f96b7596cb522b552a8507c5456e3353b6e2c7ee2df6edfb5eb5b92815f81c38996cb47c5e7351c324
@@ -1,5 +1,19 @@
1
1
  # master
2
2
 
3
+ # 2.18.0
4
+
5
+ * Fix auto-loading of previews (changes no longer require a server restart)
6
+
7
+ *Matt Brictson*
8
+
9
+ * Add `default_preview_layout` configuration option to load custom app/views/layouts.
10
+
11
+ *Jared White, Juan Manuel Ramallo*
12
+
13
+ * Calculate virtual_path once for all instances of a component class to improve performance.
14
+
15
+ *Brad Parker*
16
+
3
17
  # 2.17.1
4
18
 
5
19
  * Fix bug where rendering Slot with empty block resulted in error.
data/README.md CHANGED
@@ -674,6 +674,14 @@ class TestComponentPreview < ViewComponent::Preview
674
674
  end
675
675
  ```
676
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.default_preview_layout = "component_preview"
683
+ ```
684
+
677
685
  Preview classes live in `test/components/previews`, which can be configured using the `preview_paths` option:
678
686
 
679
687
  `config/application.rb`
@@ -990,10 +998,10 @@ ViewComponent is built by:
990
998
  |@maxbeizer|@franco|@tbroad-ramsey|@jensljungblad|@bbugh|
991
999
  |Nashville, TN|Switzerland|Spring Hill, TN|New York, NY|Austin, TX|
992
1000
 
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|
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|
997
1005
 
998
1006
  ## License
999
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
@@ -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
 
@@ -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"
@@ -3,8 +3,8 @@
3
3
  module ViewComponent
4
4
  module VERSION
5
5
  MAJOR = 2
6
- MINOR = 17
7
- PATCH = 1
6
+ MINOR = 18
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.17.1
4
+ version: 2.18.0
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-22 00:00:00.000000000 Z
11
+ date: 2020-07-27 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: []