view_component 2.6.0 → 2.7.0

Sign up to get free protection for your applications and to get access to all the features.

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: 8bc0f4ab66dfca6a89f9882d0df39f5f4a84f479f26dfd0b3334b43fe4f9b4ed
4
- data.tar.gz: 39128211a1338af6c78a54085b59f48a98411dc282e6248f03fc56229a6d7cd0
3
+ metadata.gz: 5c62fe72fd9c16275ff15c18684090a2678dfba28d585da3133b430431a672e7
4
+ data.tar.gz: b05a979440d506690105a9a8c0f501a914bb0db30d3c56303dd145b5d4aad208
5
5
  SHA512:
6
- metadata.gz: 1aa9fb3d7361efa6a98465c1513e3296abc4495ad30393ed9fbf3febc919a8669184fe2e28fe5afa9666a42b71958787b2bf091b71d4e9889b020d0495769abf
7
- data.tar.gz: 2ce5ece866ddc50d8aa01cd139c230c10b26aee9d8bbc49f7421b8d191e5b84dbd57d1247b9973f936932dcde9bd925f96c2d3114ac201d98511157b7643bac0
6
+ metadata.gz: f47750eee5ae067e03b3945ae696eccf5c14bd5677a9a65854eb15ccd060ae3d5d08591424c922097c4596546b007958b2f04bf0c14360939c44f200153ff9dc
7
+ data.tar.gz: 4098da390547ba5d915d7eb887b22d9d932e860a079ab8a04f54f2fd8d437aa7830462e116f2ef0efdf078b73dc70f05e64380747acbbd736a99fc4117c462c6
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # master
2
2
 
3
+ # 2.7.0
4
+
5
+ * Add `rendered_component` method to `ViewComponent::TestHelpers` which exposes the raw output of the rendered component.
6
+
7
+ *Richard Macklin*
8
+
9
+ * Support sidecar directories for views and other assets.
10
+
11
+ *Jon Palmer*
12
+
3
13
  # 2.6.0
4
14
 
5
15
  * Add `config.view_component.preview_route` to set the endpoint for component previews. By default `/rails/view_components` is used.
data/README.md CHANGED
@@ -191,6 +191,38 @@ class InlineVariantComponent < ViewComponent::Base
191
191
  end
192
192
  ```
193
193
 
194
+ ### Sidecar Assets
195
+
196
+ ViewComponents supports two options for defining view files.
197
+
198
+ #### Sidecar view
199
+
200
+ The simplest option is to place the view next to the Ruby component:
201
+
202
+ ```
203
+ app/components
204
+ ├── ...
205
+ ├── test_component.rb
206
+ ├── test_component.html.erb
207
+ ├── ...
208
+ ```
209
+
210
+ #### Sidecar directory
211
+
212
+ As an alternative, views and other assets can be placed in a sidecar directory with the same name as the component, which can be useful for organizing views alongside other assets like Javascript and CSS.
213
+
214
+ ```
215
+ app/components
216
+ ├── ...
217
+ ├── test_component.rb
218
+ ├── test_component
219
+ | ├── test_component.css
220
+ | ├── test_component.html.erb
221
+ | └── test_component.js
222
+ ├── ...
223
+
224
+ ```
225
+
194
226
  ### Conditional Rendering
195
227
 
196
228
  Components can implement a `#render?` method to be called after initialization to determine if the component should render.
@@ -387,7 +419,7 @@ class MyComponentTest < ViewComponent::TestCase
387
419
  end
388
420
  ```
389
421
 
390
- In the absence of `capybara`, assertion against the return values of `render_inline`, which is an instance of `Nokogiri::HTML::DocumentFragment`:
422
+ In the absence of `capybara`, assert against the return value of `render_inline`, which is an instance of `Nokogiri::HTML::DocumentFragment`:
391
423
 
392
424
  ```ruby
393
425
  test "render component" do
@@ -397,6 +429,16 @@ test "render component" do
397
429
  end
398
430
  ```
399
431
 
432
+ Alternatively, assert against the raw output of the component, which is exposed as `rendered_component`:
433
+
434
+ ```ruby
435
+ test "render component" do
436
+ render_inline(TestComponent.new(title: "my title")) { "Hello, World!" }
437
+
438
+ assert_includes rendered_component, "Hello, World!"
439
+ end
440
+ ```
441
+
400
442
  #### Action Pack Variants
401
443
 
402
444
  Use the `with_variant` helper to test specific variants:
@@ -341,7 +341,22 @@ module ViewComponent
341
341
 
342
342
  def matching_views_in_source_location
343
343
  return [] unless source_location
344
- (Dir["#{source_location.chomp(File.extname(source_location))}.*{#{ActionView::Template.template_handler_extensions.join(',')}}"] - [source_location])
344
+
345
+ location_without_extension = source_location.chomp(File.extname(source_location))
346
+
347
+ extenstions = ActionView::Template.template_handler_extensions.join(",")
348
+
349
+ # view files in the same directory as te component
350
+ sidecar_files = Dir["#{location_without_extension}.*{#{extenstions}}"]
351
+
352
+ # view files in a directory named like the component
353
+ directory = File.dirname(source_location)
354
+ filename = File.basename(source_location, ".rb")
355
+ component_name = name.demodulize.underscore
356
+
357
+ sidecar_directory_files = Dir["#{directory}/#{component_name}/#{filename}.*{#{extenstions}}"]
358
+
359
+ (sidecar_files - [source_location] + sidecar_directory_files)
345
360
  end
346
361
 
347
362
  def templates
@@ -7,7 +7,7 @@ module ViewComponent
7
7
  include Capybara::Minitest::Assertions
8
8
 
9
9
  def page
10
- Capybara::Node::Simple.new(@raw)
10
+ Capybara::Node::Simple.new(@rendered_component)
11
11
  end
12
12
 
13
13
  def refute_component_rendered
@@ -17,10 +17,12 @@ module ViewComponent
17
17
  warn "WARNING in `ViewComponent::TestHelpers`: You must add `capybara` to your Gemfile to use Capybara assertions."
18
18
  end
19
19
 
20
+ attr_reader :rendered_component
21
+
20
22
  def render_inline(component, **args, &block)
21
- @raw = controller.view_context.render(component, args, &block)
23
+ @rendered_component = controller.view_context.render(component, args, &block)
22
24
 
23
- Nokogiri::HTML.fragment(@raw)
25
+ Nokogiri::HTML.fragment(@rendered_component)
24
26
  end
25
27
 
26
28
  def controller
@@ -3,7 +3,7 @@
3
3
  module ViewComponent
4
4
  module VERSION
5
5
  MAJOR = 2
6
- MINOR = 6
6
+ MINOR = 7
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.6.0
4
+ version: 2.7.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-05-14 00:00:00.000000000 Z
11
+ date: 2020-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport