view_component 2.4.0 → 2.5.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 +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +37 -5
- data/app/controllers/rails/view_components_controller.rb +1 -1
- data/lib/view_component/base.rb +8 -0
- data/lib/view_component/collection.rb +11 -3
- data/lib/view_component/preview.rb +5 -2
- data/lib/view_component/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 026e6dbe1f96cf8fb5d6127b756a5f78733e5918df2f9785972208866e755fd7
|
4
|
+
data.tar.gz: cbab7c479e82b0d5445e6bdcfa7c2526e409b302283c601cc21c2df8e32f6ed9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd6a8e929dc0c9ac04c1b8ac28ccf600a22b74394af75783a901c1054372ef00c1713ac1f82ea7602633f2f9ad92d499c7ea90310a3351f6e1c7e6da3cc52924
|
7
|
+
data.tar.gz: dc16afe7a53dff39a9c1713d8ecb8878692dc49b5184da1fc5d733d41b46a91e8caeb9c1c0eae7aa4d396dac0269d563556d3a5932f8f31605ef4ee5b98569fa
|
data/CHANGELOG.md
CHANGED
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
|
data/lib/view_component/base.rb
CHANGED
@@ -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
|
-
|
6
|
+
iterator = ActionView::PartialIteration.new(@collection.size)
|
8
7
|
|
9
8
|
@collection.map do |item|
|
10
|
-
@component.new(
|
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
|
-
|
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.
|
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
|
+
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-
|
11
|
+
date: 2020-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|