view_component 2.4.0 → 2.5.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: ca0004a178e80b26bc7c47af5ef2dab32cdb4f3c61d09a1b27e593cf8ec4f5ea
4
- data.tar.gz: 16427ce66ee5872627dafd670799fe19c42bfec499163dcbbe4e643a0e3b34fc
3
+ metadata.gz: 026e6dbe1f96cf8fb5d6127b756a5f78733e5918df2f9785972208866e755fd7
4
+ data.tar.gz: cbab7c479e82b0d5445e6bdcfa7c2526e409b302283c601cc21c2df8e32f6ed9
5
5
  SHA512:
6
- metadata.gz: 7ea71f48ee58741b49df4818f0717328b715a1c11ebb0ecf0771c15aabb53fc3cdb51428c0b5f85781c04f113afca98fbe152083090f9f0b518f988cd60de3e9
7
- data.tar.gz: 401537d259e436c9cb0ea7e3c4dc40af6c02f69013460583b4827795f9d615d79aa510aa9472840af05edea39d689d6cd785279a82c6e1b48ebb980654c03087
6
+ metadata.gz: cd6a8e929dc0c9ac04c1b8ac28ccf600a22b74394af75783a901c1054372ef00c1713ac1f82ea7602633f2f9ad92d499c7ea90310a3351f6e1c7e6da3cc52924
7
+ data.tar.gz: dc16afe7a53dff39a9c1713d8ecb8878692dc49b5184da1fc5d733d41b46a91e8caeb9c1c0eae7aa4d396dac0269d563556d3a5932f8f31605ef4ee5b98569fa
@@ -1,5 +1,15 @@
1
1
  # master
2
2
 
3
+ # v2.5.0
4
+
5
+ * Add counter variables when rendering collections.
6
+
7
+ *Frank S*
8
+
9
+ * Add the ability to access params from preview examples.
10
+
11
+ *Fabio Cantoni*
12
+
3
13
  # v2.4.0
4
14
 
5
15
  * Add `#render_to_string` support.
data/README.md CHANGED
@@ -322,6 +322,25 @@ end
322
322
  </li>
323
323
  ```
324
324
 
325
+ `ViewComponent` defines a counter variable matching the parameter name above, followed by `_counter`. To access this variable, add it to `initialize` as an argument:
326
+
327
+ `app/components/product_component.rb`
328
+ ``` ruby
329
+ class ProductComponent < ViewComponent::Base
330
+ def initialize(product:, product_counter:)
331
+ @product = product
332
+ @counter = product_counter
333
+ end
334
+ end
335
+ ```
336
+
337
+ `app/components/product_component.html.erb`
338
+ ``` erb
339
+ <li>
340
+ <%= @counter %> <%= @product.name %>
341
+ </li>
342
+ ```
343
+
325
344
  ### Sidecar assets (experimental)
326
345
 
327
346
  We're experimenting with including Javascript and CSS alongside components, sometimes called "sidecar" assets or files.
@@ -508,7 +527,7 @@ class TestComponentPreview < ViewComponent::Preview
508
527
  end
509
528
 
510
529
  def with_content_block
511
- render(TestComponent.new(title: "This component accepts a block of content") do
530
+ render(TestComponent.new(title: "This component accepts a block of content")) do
512
531
  tag.div do
513
532
  content_tag(:span, "Hello")
514
533
  end
@@ -521,6 +540,19 @@ Which generates <http://localhost:3000/rails/view_components/test_component/with
521
540
  <http://localhost:3000/rails/view_components/test_component/with_long_title>,
522
541
  and <http://localhost:3000/rails/view_components/test_component/with_content_block>.
523
542
 
543
+ It's also possible to set dynamic values from the params by setting them as arguments:
544
+
545
+ `test/components/previews/test_component_preview.rb`
546
+ ```ruby
547
+ class TestComponentPreview < ViewComponent::Preview
548
+ def with_dynamic_title(title: "Test component default")
549
+ render(TestComponent.new(title: title))
550
+ end
551
+ end
552
+ ```
553
+
554
+ You'll then be able to pass down a value with <http://localhost:3000/rails/components/test_component/with_dynamic_title?title=Custom+title>.
555
+
524
556
  The `ViewComponent::Preview` base class includes
525
557
  [`ActionView::Helpers::TagHelper`][tag-helper], which provides the [`tag`][tag]
526
558
  and [`content_tag`][content_tag] view helper methods.
@@ -648,10 +680,10 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/github
648
680
  |@blakewilliams|@seanpdoyle|@tclem|@nashby|@jaredcwhite|
649
681
  |Boston, MA|New York, NY|San Francisco, CA|Minsk|Portland, OR|
650
682
 
651
- |<img src="https://avatars.githubusercontent.com/simonrand?s=256" alt="simonrand" width="128" />|<img src="https://avatars.githubusercontent.com/fugufish?s=256" alt="fugufish" width="128" />|
652
- |:---:|:---:|
653
- |@simonrand|@fugufish|
654
- |Dublin, Ireland|Salt Lake City, Utah|
683
+ |<img src="https://avatars.githubusercontent.com/simonrand?s=256" alt="simonrand" width="128" />|<img src="https://avatars.githubusercontent.com/fugufish?s=256" alt="fugufish" width="128" />|<img src="https://avatars.githubusercontent.com/cover?s=256" alt="cover" width="128" />|<img src="https://avatars.githubusercontent.com/franks921?s=256" alt="franks921" width="128" />|
684
+ |:---:|:---:|:---:|:---:|
685
+ |@simonrand|@fugufish|@cover|@franks921|
686
+ |Dublin, Ireland|Salt Lake City, Utah|Barcelona|South Africa|
655
687
 
656
688
  ## License
657
689
 
@@ -30,7 +30,7 @@ class Rails::ViewComponentsController < Rails::ApplicationController # :nodoc:
30
30
  else
31
31
  prepend_application_view_paths
32
32
  @example_name = File.basename(params[:path])
33
- @render_args = @preview.render_args(@example_name)
33
+ @render_args = @preview.render_args(@example_name, params: params.permit!)
34
34
  layout = @render_args[:layout]
35
35
  opts = layout.nil? ? {} : { layout: layout }
36
36
  # rubocop:disable GitHub/RailsControllerRenderPathsExist
@@ -187,6 +187,14 @@ module ViewComponent
187
187
  templates.map { |template| template[:variant] } + variants_from_inline_calls(inline_calls)
188
188
  end
189
189
 
190
+ define_singleton_method(:collection_counter_parameter_name) do
191
+ "#{collection_parameter_name}_counter".to_sym
192
+ end
193
+
194
+ define_singleton_method(:counter_argument_present?) do
195
+ instance_method(:initialize).parameters.map(&:second).include?(collection_counter_parameter_name)
196
+ end
197
+
190
198
  # If template name annotations are turned on, a line is dynamically
191
199
  # added with a comment. In this case, we want to return a different
192
200
  # starting line number so errors that are raised will point to the
@@ -1,13 +1,14 @@
1
-
2
1
  # frozen_string_literal: true
3
2
 
4
3
  module ViewComponent
5
4
  class Collection
6
5
  def render_in(view_context, &block)
7
- as = @component.collection_parameter_name
6
+ iterator = ActionView::PartialIteration.new(@collection.size)
8
7
 
9
8
  @collection.map do |item|
10
- @component.new(@options.merge(as => item)).render_in(view_context, &block)
9
+ content = @component.new(component_options(item, iterator)).render_in(view_context, &block)
10
+ iterator.iterate!
11
+ content
11
12
  end.join.html_safe
12
13
  end
13
14
 
@@ -26,5 +27,12 @@ module ViewComponent
26
27
  raise ArgumentError.new("The value of the argument isn't a valid collection. Make sure it responds to to_ary: #{object.inspect}")
27
28
  end
28
29
  end
30
+
31
+ def component_options(item, iterator)
32
+ item_options = { @component.collection_parameter_name => item }
33
+ item_options[@component.collection_counter_parameter_name] = iterator.index + 1 if @component.counter_argument_present?
34
+
35
+ @options.merge(item_options)
36
+ end
29
37
  end
30
38
  end
@@ -19,8 +19,11 @@ module ViewComponent # :nodoc:
19
19
  end
20
20
 
21
21
  # Returns the arguments for rendering of the component in its layout
22
- def render_args(example)
23
- new.public_send(example).merge(layout: @layout)
22
+ def render_args(example, params: {})
23
+ example_params_names = instance_method(example).parameters.map(&:last)
24
+ provided_params = params.slice(*example_params_names).to_h.symbolize_keys
25
+ result = provided_params.empty? ? new.public_send(example) : new.public_send(example, **provided_params)
26
+ result.merge(layout: @layout)
24
27
  end
25
28
 
26
29
  # Returns the component object class associated to the preview.
@@ -3,7 +3,7 @@
3
3
  module ViewComponent
4
4
  module VERSION
5
5
  MAJOR = 2
6
- MINOR = 4
6
+ MINOR = 5
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.4.0
4
+ version: 2.5.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-04-16 00:00:00.000000000 Z
11
+ date: 2020-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport