view_component 2.0.0 → 2.1.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 +6 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +76 -76
- data/README.md +56 -9
- data/lib/view_component/base.rb +15 -0
- data/lib/view_component/collection.rb +30 -0
- data/lib/view_component/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f6cbdc740d6c9175742269ff9b13a759e28b188d2abcdda03d85bafff44ac7a
|
4
|
+
data.tar.gz: a00eff317e112a844e65fb196e6a2e7832e0ff7b5ede73ba47196131d92f9d65
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68ac701e9c6814d9f6384e393b0ac9ac18a2646b85bef1e972a092bd252f7e573c4c5c6aea73056d75a6597d71076fb43e40ba6edd4eb1a73c88618881fc9705
|
7
|
+
data.tar.gz: '099f69cfb27e52b9e337328c204fc512e1c151b562a22e94783a58ed99952856994575a9b5556f4a0ebf5c7b54012bfab55330956fc67478f19057784090894e'
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,67 +1,67 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
view_component (2.
|
4
|
+
view_component (2.1.0)
|
5
5
|
capybara (~> 3)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
actioncable (6.0.
|
11
|
-
actionpack (= 6.0.
|
10
|
+
actioncable (6.0.2.2)
|
11
|
+
actionpack (= 6.0.2.2)
|
12
12
|
nio4r (~> 2.0)
|
13
13
|
websocket-driver (>= 0.6.1)
|
14
|
-
actionmailbox (6.0.
|
15
|
-
actionpack (= 6.0.
|
16
|
-
activejob (= 6.0.
|
17
|
-
activerecord (= 6.0.
|
18
|
-
activestorage (= 6.0.
|
19
|
-
activesupport (= 6.0.
|
14
|
+
actionmailbox (6.0.2.2)
|
15
|
+
actionpack (= 6.0.2.2)
|
16
|
+
activejob (= 6.0.2.2)
|
17
|
+
activerecord (= 6.0.2.2)
|
18
|
+
activestorage (= 6.0.2.2)
|
19
|
+
activesupport (= 6.0.2.2)
|
20
20
|
mail (>= 2.7.1)
|
21
|
-
actionmailer (6.0.
|
22
|
-
actionpack (= 6.0.
|
23
|
-
actionview (= 6.0.
|
24
|
-
activejob (= 6.0.
|
21
|
+
actionmailer (6.0.2.2)
|
22
|
+
actionpack (= 6.0.2.2)
|
23
|
+
actionview (= 6.0.2.2)
|
24
|
+
activejob (= 6.0.2.2)
|
25
25
|
mail (~> 2.5, >= 2.5.4)
|
26
26
|
rails-dom-testing (~> 2.0)
|
27
|
-
actionpack (6.0.
|
28
|
-
actionview (= 6.0.
|
29
|
-
activesupport (= 6.0.
|
30
|
-
rack (~> 2.0)
|
27
|
+
actionpack (6.0.2.2)
|
28
|
+
actionview (= 6.0.2.2)
|
29
|
+
activesupport (= 6.0.2.2)
|
30
|
+
rack (~> 2.0, >= 2.0.8)
|
31
31
|
rack-test (>= 0.6.3)
|
32
32
|
rails-dom-testing (~> 2.0)
|
33
33
|
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
34
|
-
actiontext (6.0.
|
35
|
-
actionpack (= 6.0.
|
36
|
-
activerecord (= 6.0.
|
37
|
-
activestorage (= 6.0.
|
38
|
-
activesupport (= 6.0.
|
34
|
+
actiontext (6.0.2.2)
|
35
|
+
actionpack (= 6.0.2.2)
|
36
|
+
activerecord (= 6.0.2.2)
|
37
|
+
activestorage (= 6.0.2.2)
|
38
|
+
activesupport (= 6.0.2.2)
|
39
39
|
nokogiri (>= 1.8.5)
|
40
|
-
actionview (6.0.
|
41
|
-
activesupport (= 6.0.
|
40
|
+
actionview (6.0.2.2)
|
41
|
+
activesupport (= 6.0.2.2)
|
42
42
|
builder (~> 3.1)
|
43
43
|
erubi (~> 1.4)
|
44
44
|
rails-dom-testing (~> 2.0)
|
45
45
|
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
46
|
-
activejob (6.0.
|
47
|
-
activesupport (= 6.0.
|
46
|
+
activejob (6.0.2.2)
|
47
|
+
activesupport (= 6.0.2.2)
|
48
48
|
globalid (>= 0.3.6)
|
49
|
-
activemodel (6.0.
|
50
|
-
activesupport (= 6.0.
|
51
|
-
activerecord (6.0.
|
52
|
-
activemodel (= 6.0.
|
53
|
-
activesupport (= 6.0.
|
54
|
-
activestorage (6.0.
|
55
|
-
actionpack (= 6.0.
|
56
|
-
activejob (= 6.0.
|
57
|
-
activerecord (= 6.0.
|
49
|
+
activemodel (6.0.2.2)
|
50
|
+
activesupport (= 6.0.2.2)
|
51
|
+
activerecord (6.0.2.2)
|
52
|
+
activemodel (= 6.0.2.2)
|
53
|
+
activesupport (= 6.0.2.2)
|
54
|
+
activestorage (6.0.2.2)
|
55
|
+
actionpack (= 6.0.2.2)
|
56
|
+
activejob (= 6.0.2.2)
|
57
|
+
activerecord (= 6.0.2.2)
|
58
58
|
marcel (~> 0.3.1)
|
59
|
-
activesupport (6.0.
|
59
|
+
activesupport (6.0.2.2)
|
60
60
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
61
61
|
i18n (>= 0.7, < 2)
|
62
62
|
minitest (~> 5.1)
|
63
63
|
tzinfo (~> 1.1)
|
64
|
-
zeitwerk (~> 2.
|
64
|
+
zeitwerk (~> 2.2)
|
65
65
|
addressable (2.7.0)
|
66
66
|
public_suffix (>= 2.0.2, < 5.0)
|
67
67
|
ast (2.4.0)
|
@@ -73,7 +73,7 @@ GEM
|
|
73
73
|
html_tokenizer (~> 0.0.6)
|
74
74
|
parser (>= 2.4)
|
75
75
|
smart_properties
|
76
|
-
builder (3.2.
|
76
|
+
builder (3.2.4)
|
77
77
|
capybara (3.31.0)
|
78
78
|
addressable
|
79
79
|
mini_mime (>= 0.1.3)
|
@@ -82,63 +82,63 @@ GEM
|
|
82
82
|
rack-test (>= 0.6.3)
|
83
83
|
regexp_parser (~> 1.5)
|
84
84
|
xpath (~> 3.2)
|
85
|
-
concurrent-ruby (1.1.
|
86
|
-
crass (1.0.
|
87
|
-
erubi (1.
|
85
|
+
concurrent-ruby (1.1.6)
|
86
|
+
crass (1.0.6)
|
87
|
+
erubi (1.9.0)
|
88
88
|
globalid (0.4.2)
|
89
89
|
activesupport (>= 4.2.0)
|
90
90
|
haml (5.1.2)
|
91
91
|
temple (>= 0.8.0)
|
92
92
|
tilt
|
93
93
|
html_tokenizer (0.0.7)
|
94
|
-
i18n (1.
|
94
|
+
i18n (1.8.2)
|
95
95
|
concurrent-ruby (~> 1.0)
|
96
|
-
jaro_winkler (1.5.
|
97
|
-
loofah (2.
|
96
|
+
jaro_winkler (1.5.4)
|
97
|
+
loofah (2.4.0)
|
98
98
|
crass (~> 1.0.2)
|
99
99
|
nokogiri (>= 1.5.9)
|
100
100
|
mail (2.7.1)
|
101
101
|
mini_mime (>= 0.1.1)
|
102
102
|
marcel (0.3.3)
|
103
103
|
mimemagic (~> 0.3.2)
|
104
|
-
method_source (0.
|
105
|
-
mimemagic (0.3.
|
104
|
+
method_source (1.0.0)
|
105
|
+
mimemagic (0.3.4)
|
106
106
|
mini_mime (1.0.2)
|
107
107
|
mini_portile2 (2.4.0)
|
108
108
|
minitest (5.1.0)
|
109
109
|
nio4r (2.5.2)
|
110
|
-
nokogiri (1.10.
|
110
|
+
nokogiri (1.10.9)
|
111
111
|
mini_portile2 (~> 2.4.0)
|
112
|
-
parallel (1.
|
113
|
-
parser (2.
|
112
|
+
parallel (1.19.1)
|
113
|
+
parser (2.7.0.5)
|
114
114
|
ast (~> 2.4.0)
|
115
115
|
public_suffix (4.0.3)
|
116
|
-
rack (2.
|
116
|
+
rack (2.2.2)
|
117
117
|
rack-test (1.1.0)
|
118
118
|
rack (>= 1.0, < 3)
|
119
|
-
rails (6.0.
|
120
|
-
actioncable (= 6.0.
|
121
|
-
actionmailbox (= 6.0.
|
122
|
-
actionmailer (= 6.0.
|
123
|
-
actionpack (= 6.0.
|
124
|
-
actiontext (= 6.0.
|
125
|
-
actionview (= 6.0.
|
126
|
-
activejob (= 6.0.
|
127
|
-
activemodel (= 6.0.
|
128
|
-
activerecord (= 6.0.
|
129
|
-
activestorage (= 6.0.
|
130
|
-
activesupport (= 6.0.
|
119
|
+
rails (6.0.2.2)
|
120
|
+
actioncable (= 6.0.2.2)
|
121
|
+
actionmailbox (= 6.0.2.2)
|
122
|
+
actionmailer (= 6.0.2.2)
|
123
|
+
actionpack (= 6.0.2.2)
|
124
|
+
actiontext (= 6.0.2.2)
|
125
|
+
actionview (= 6.0.2.2)
|
126
|
+
activejob (= 6.0.2.2)
|
127
|
+
activemodel (= 6.0.2.2)
|
128
|
+
activerecord (= 6.0.2.2)
|
129
|
+
activestorage (= 6.0.2.2)
|
130
|
+
activesupport (= 6.0.2.2)
|
131
131
|
bundler (>= 1.3.0)
|
132
|
-
railties (= 6.0.
|
132
|
+
railties (= 6.0.2.2)
|
133
133
|
sprockets-rails (>= 2.0.0)
|
134
134
|
rails-dom-testing (2.0.3)
|
135
135
|
activesupport (>= 4.2.0)
|
136
136
|
nokogiri (>= 1.6)
|
137
|
-
rails-html-sanitizer (1.
|
138
|
-
loofah (~> 2.
|
139
|
-
railties (6.0.
|
140
|
-
actionpack (= 6.0.
|
141
|
-
activesupport (= 6.0.
|
137
|
+
rails-html-sanitizer (1.3.0)
|
138
|
+
loofah (~> 2.3)
|
139
|
+
railties (6.0.2.2)
|
140
|
+
actionpack (= 6.0.2.2)
|
141
|
+
activesupport (= 6.0.2.2)
|
142
142
|
method_source
|
143
143
|
rake (>= 0.8.7)
|
144
144
|
thor (>= 0.20.3, < 2.0)
|
@@ -162,26 +162,26 @@ GEM
|
|
162
162
|
temple (>= 0.7.6, < 0.9)
|
163
163
|
tilt (>= 2.0.6, < 2.1)
|
164
164
|
smart_properties (1.15.0)
|
165
|
-
sprockets (
|
165
|
+
sprockets (4.0.0)
|
166
166
|
concurrent-ruby (~> 1.0)
|
167
167
|
rack (> 1, < 3)
|
168
168
|
sprockets-rails (3.2.1)
|
169
169
|
actionpack (>= 4.0)
|
170
170
|
activesupport (>= 4.0)
|
171
171
|
sprockets (>= 3.0.0)
|
172
|
-
temple (0.8.
|
173
|
-
thor (0.
|
172
|
+
temple (0.8.2)
|
173
|
+
thor (1.0.1)
|
174
174
|
thread_safe (0.3.6)
|
175
|
-
tilt (2.0.
|
176
|
-
tzinfo (1.2.
|
175
|
+
tilt (2.0.10)
|
176
|
+
tzinfo (1.2.6)
|
177
177
|
thread_safe (~> 0.1)
|
178
|
-
unicode-display_width (1.6.
|
178
|
+
unicode-display_width (1.6.1)
|
179
179
|
websocket-driver (0.7.1)
|
180
180
|
websocket-extensions (>= 0.1.0)
|
181
181
|
websocket-extensions (0.1.4)
|
182
182
|
xpath (3.2.0)
|
183
183
|
nokogiri (~> 1.8)
|
184
|
-
zeitwerk (2.
|
184
|
+
zeitwerk (2.3.0)
|
185
185
|
|
186
186
|
PLATFORMS
|
187
187
|
ruby
|
@@ -191,7 +191,7 @@ DEPENDENCIES
|
|
191
191
|
bundler (~> 1.14)
|
192
192
|
haml (~> 5)
|
193
193
|
minitest (= 5.1.0)
|
194
|
-
rails (= 6.0.
|
194
|
+
rails (= 6.0.2.2)
|
195
195
|
rake (~> 13.0)
|
196
196
|
rubocop (= 0.74)
|
197
197
|
rubocop-github (~> 0.13.0)
|
data/README.md
CHANGED
@@ -269,6 +269,58 @@ end
|
|
269
269
|
|
270
270
|
To assert that a component has not been rendered, use `refute_component_rendered` from `ViewComponent::TestHelpers`.
|
271
271
|
|
272
|
+
### Rendering collections
|
273
|
+
|
274
|
+
It's possible to render collections with components:
|
275
|
+
|
276
|
+
`app/view/products/index.html.erb`
|
277
|
+
``` erb
|
278
|
+
<%= render(ProductComponent.with_collection(@products)) %>
|
279
|
+
```
|
280
|
+
|
281
|
+
Where the `ProductComponent` and associated template might look something like the following. Notice that the constructor must take a `product` and the name of that parameter matches the name of the component.
|
282
|
+
|
283
|
+
`app/components/product_component.rb`
|
284
|
+
``` ruby
|
285
|
+
class ProductComponent < ViewComponent::Base
|
286
|
+
def initialize(product:)
|
287
|
+
@product = product
|
288
|
+
end
|
289
|
+
end
|
290
|
+
```
|
291
|
+
|
292
|
+
`app/components/product_component.html.erb`
|
293
|
+
``` erb
|
294
|
+
<li><%= @product.name %></li>
|
295
|
+
```
|
296
|
+
|
297
|
+
Additionally, extra arguments can be passed to the component and the name of the parameter can be changed:
|
298
|
+
|
299
|
+
`app/view/products/index.html.erb`
|
300
|
+
``` erb
|
301
|
+
<%= render(ProductComponent.with_collection(@products, notice: "hi")) %>
|
302
|
+
```
|
303
|
+
|
304
|
+
`app/components/product_component.rb`
|
305
|
+
``` ruby
|
306
|
+
class ProductComponent < ViewComponent::Base
|
307
|
+
with_collection_parameter :item
|
308
|
+
|
309
|
+
def initialize(item:, notice:)
|
310
|
+
@item = item
|
311
|
+
@notice = notice
|
312
|
+
end
|
313
|
+
end
|
314
|
+
```
|
315
|
+
|
316
|
+
`app/components/product_component.html.erb`
|
317
|
+
``` erb
|
318
|
+
<li>
|
319
|
+
<h2><%= @item.name %></h2>
|
320
|
+
<span><%= @notice %></span>
|
321
|
+
</li>
|
322
|
+
```
|
323
|
+
|
272
324
|
### Testing
|
273
325
|
|
274
326
|
Unit test components directly, using the `render_inline` test helper and Capybara matchers:
|
@@ -446,15 +498,10 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/github
|
|
446
498
|
|@mellowfish|@horacio|@dukex|@dark-panda|@smashwilson|
|
447
499
|
|Spring Hill, TN|Buenos Aires|São Paulo||Gambrills, MD|
|
448
500
|
|
449
|
-
|<img src="https://avatars.githubusercontent.com/blakewilliams?s=256" alt="blakewilliams" width="128" />|
|
450
|
-
|
451
|
-
|@blakewilliams|
|
452
|
-
|Boston, MA|
|
453
|
-
|
454
|
-
|<img src="https://avatars.githubusercontent.com/seanpdoyle?s=256" alt="seanpdoyle" width="128" />|
|
455
|
-
|:---:|
|
456
|
-
|@seanpdoyle|
|
457
|
-
|New York, NY|
|
501
|
+
|<img src="https://avatars.githubusercontent.com/blakewilliams?s=256" alt="blakewilliams" width="128" />|<img src="https://avatars.githubusercontent.com/seanpdoyle?s=256" alt="seanpdoyle" width="128" />|<img src="https://avatars.githubusercontent.com/tclem?s=256" alt="tclem" width="128" />|
|
502
|
+
|:---:|:---:|:---:|
|
503
|
+
|@blakewilliams|@seanpdoyle|@tclem|
|
504
|
+
|Boston, MA|New York, NY|San Francisco, CA|
|
458
505
|
|
459
506
|
## License
|
460
507
|
|
data/lib/view_component/base.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require "action_view"
|
4
4
|
require "active_support/configurable"
|
5
|
+
require "view_component/collection"
|
5
6
|
require "view_component/previewable"
|
6
7
|
|
7
8
|
module ViewComponent
|
@@ -14,6 +15,11 @@ module ViewComponent
|
|
14
15
|
class_attribute :content_areas, default: []
|
15
16
|
self.content_areas = [] # default doesn't work until Rails 5.2
|
16
17
|
|
18
|
+
# Render a component collection.
|
19
|
+
def self.with_collection(*args)
|
20
|
+
Collection.new(self, *args)
|
21
|
+
end
|
22
|
+
|
17
23
|
# Entrypoint for rendering components.
|
18
24
|
#
|
19
25
|
# view_context: ActionView context from calling view
|
@@ -213,6 +219,15 @@ module ViewComponent
|
|
213
219
|
self.content_areas = areas
|
214
220
|
end
|
215
221
|
|
222
|
+
# Support overriding this component's collection parameter name
|
223
|
+
def with_collection_parameter(param)
|
224
|
+
@with_collection_parameter = param
|
225
|
+
end
|
226
|
+
|
227
|
+
def collection_parameter_name
|
228
|
+
(@with_collection_parameter || name.demodulize.underscore.chomp("_component")).to_sym
|
229
|
+
end
|
230
|
+
|
216
231
|
private
|
217
232
|
|
218
233
|
def matching_views_in_source_location
|
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module ViewComponent
|
5
|
+
class Collection
|
6
|
+
def render_in(view_context, &block)
|
7
|
+
as = @component.collection_parameter_name
|
8
|
+
|
9
|
+
@collection.map do |item|
|
10
|
+
@component.new(@options.merge(as => item)).render_in(view_context, &block)
|
11
|
+
end.join.html_safe
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def initialize(component, object, **options)
|
17
|
+
@component = component
|
18
|
+
@collection = collection_variable(object || [])
|
19
|
+
@options = options
|
20
|
+
end
|
21
|
+
|
22
|
+
def collection_variable(object)
|
23
|
+
if object.respond_to?(:to_ary)
|
24
|
+
object.to_ary
|
25
|
+
else
|
26
|
+
raise ArgumentError.new("The value of the argument isn't a valid collection. Make sure it responds to to_ary: #{object.inspect}")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
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.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GitHub Open Source
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-03-
|
11
|
+
date: 2020-03-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: capybara
|
@@ -177,6 +177,7 @@ files:
|
|
177
177
|
- lib/railties/lib/rails/templates/rails/components/previews.html.erb
|
178
178
|
- lib/view_component.rb
|
179
179
|
- lib/view_component/base.rb
|
180
|
+
- lib/view_component/collection.rb
|
180
181
|
- lib/view_component/engine.rb
|
181
182
|
- lib/view_component/preview.rb
|
182
183
|
- lib/view_component/previewable.rb
|