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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 45ac24e1dc67b91015f67efc09208e7b185c90bb1ca84de486b810dc1d699116
4
- data.tar.gz: b87bcf3c0c50e8ca497757951736e9029cbd0411bdbd97134b136511f2855740
3
+ metadata.gz: 1f6cbdc740d6c9175742269ff9b13a759e28b188d2abcdda03d85bafff44ac7a
4
+ data.tar.gz: a00eff317e112a844e65fb196e6a2e7832e0ff7b5ede73ba47196131d92f9d65
5
5
  SHA512:
6
- metadata.gz: 41aa382866ac371d48e305383df40fcfd8568c7ea4e98c0acf38784c7bee267adb40ce8a9b065de55a5c5622ca9e8584a55b66e838c7203a52fa5646eca4f0c6
7
- data.tar.gz: e66a6eccab9e8774dea217944e2a00a38f09276aac57c200e0dee7075a9cfa096029d2ad28debc5ee6ea66ad7c3af2a3248a5f6803bbcc94198364a5cb2f98c4
6
+ metadata.gz: 68ac701e9c6814d9f6384e393b0ac9ac18a2646b85bef1e972a092bd252f7e573c4c5c6aea73056d75a6597d71076fb43e40ba6edd4eb1a73c88618881fc9705
7
+ data.tar.gz: '099f69cfb27e52b9e337328c204fc512e1c151b562a22e94783a58ed99952856994575a9b5556f4a0ebf5c7b54012bfab55330956fc67478f19057784090894e'
@@ -1,5 +1,11 @@
1
1
  # master
2
2
 
3
+ # v2.1.0
4
+
5
+ * Support rendering collections (e.g., `render(MyComponent.with_collection(@items))`).
6
+
7
+ *Tim Clem*
8
+
3
9
  # v2.0.0
4
10
 
5
11
  * Move to `ViewComponent` namespace, removing all references to `ActionView`.
data/Gemfile CHANGED
@@ -3,6 +3,6 @@
3
3
  source "https://rubygems.org"
4
4
  gemspec
5
5
 
6
- rails_version = "#{ENV['RAILS_VERSION'] || '6.0.0'}"
6
+ rails_version = "#{ENV['RAILS_VERSION'] || '6.0.2.2'}"
7
7
 
8
8
  gem "rails", rails_version == "master" ? { github: "rails/rails" } : rails_version
@@ -1,67 +1,67 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- view_component (2.0.0)
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.0)
11
- actionpack (= 6.0.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.0)
15
- actionpack (= 6.0.0)
16
- activejob (= 6.0.0)
17
- activerecord (= 6.0.0)
18
- activestorage (= 6.0.0)
19
- activesupport (= 6.0.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.0)
22
- actionpack (= 6.0.0)
23
- actionview (= 6.0.0)
24
- activejob (= 6.0.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.0)
28
- actionview (= 6.0.0)
29
- activesupport (= 6.0.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.0)
35
- actionpack (= 6.0.0)
36
- activerecord (= 6.0.0)
37
- activestorage (= 6.0.0)
38
- activesupport (= 6.0.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.0)
41
- activesupport (= 6.0.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.0)
47
- activesupport (= 6.0.0)
46
+ activejob (6.0.2.2)
47
+ activesupport (= 6.0.2.2)
48
48
  globalid (>= 0.3.6)
49
- activemodel (6.0.0)
50
- activesupport (= 6.0.0)
51
- activerecord (6.0.0)
52
- activemodel (= 6.0.0)
53
- activesupport (= 6.0.0)
54
- activestorage (6.0.0)
55
- actionpack (= 6.0.0)
56
- activejob (= 6.0.0)
57
- activerecord (= 6.0.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.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.1, >= 2.1.8)
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.3)
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.5)
86
- crass (1.0.5)
87
- erubi (1.8.0)
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.6.0)
94
+ i18n (1.8.2)
95
95
  concurrent-ruby (~> 1.0)
96
- jaro_winkler (1.5.3)
97
- loofah (2.3.1)
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.9.2)
105
- mimemagic (0.3.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.8)
110
+ nokogiri (1.10.9)
111
111
  mini_portile2 (~> 2.4.0)
112
- parallel (1.17.0)
113
- parser (2.6.3.0)
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.0.8)
116
+ rack (2.2.2)
117
117
  rack-test (1.1.0)
118
118
  rack (>= 1.0, < 3)
119
- rails (6.0.0)
120
- actioncable (= 6.0.0)
121
- actionmailbox (= 6.0.0)
122
- actionmailer (= 6.0.0)
123
- actionpack (= 6.0.0)
124
- actiontext (= 6.0.0)
125
- actionview (= 6.0.0)
126
- activejob (= 6.0.0)
127
- activemodel (= 6.0.0)
128
- activerecord (= 6.0.0)
129
- activestorage (= 6.0.0)
130
- activesupport (= 6.0.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.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.2.0)
138
- loofah (~> 2.2, >= 2.2.2)
139
- railties (6.0.0)
140
- actionpack (= 6.0.0)
141
- activesupport (= 6.0.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 (3.7.2)
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.1)
173
- thor (0.20.3)
172
+ temple (0.8.2)
173
+ thor (1.0.1)
174
174
  thread_safe (0.3.6)
175
- tilt (2.0.9)
176
- tzinfo (1.2.5)
175
+ tilt (2.0.10)
176
+ tzinfo (1.2.6)
177
177
  thread_safe (~> 0.1)
178
- unicode-display_width (1.6.0)
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.1.10)
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.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
 
@@ -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
@@ -3,7 +3,7 @@
3
3
  module ViewComponent
4
4
  module VERSION
5
5
  MAJOR = 2
6
- MINOR = 0
6
+ MINOR = 1
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.0.0
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-23 00:00:00.000000000 Z
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