view_component 2.7.0 → 2.11.1
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 +4 -4
- data/CHANGELOG.md +34 -0
- data/README.md +39 -5
- data/lib/view_component/base.rb +35 -13
- data/lib/view_component/collection.rb +1 -1
- data/lib/view_component/compile_cache.rb +24 -0
- data/lib/view_component/engine.rb +4 -0
- data/lib/view_component/version.rb +2 -2
- metadata +31 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c63a550faa0964130875d2670dd2d39753acd1ff2fc14ee88168573af6661619
|
4
|
+
data.tar.gz: 5f54192e46fc3cbde5e0a5f2f3b6c186790f9ad2fe91627f0662457d7434d679
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b786100a0867df76302b15c726e3dfe4dc2c60786ef34b269d9c7c8cd8fadabb695f3f83ad00a1872e1fdb5d5f112857ca32f2da998c459e2f576cbd9a163d46
|
7
|
+
data.tar.gz: 4e30cd32923a51d7a29f78fa86bc11d21bec5653b60961b444819b821897ebf7a00284e76557a18c6e61b2baa6c7d78bc72033f465d8d99d271a51e84bd63895
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,39 @@
|
|
1
1
|
# master
|
2
2
|
|
3
|
+
# 2.11.1
|
4
|
+
|
5
|
+
* Fix kwarg warnings in Ruby 2.7.
|
6
|
+
|
7
|
+
*Joel Hawksley*
|
8
|
+
|
9
|
+
# 2.11.0
|
10
|
+
|
11
|
+
* Ensure Rails configuration is available within components.
|
12
|
+
|
13
|
+
*Trevor Broaddus*
|
14
|
+
|
15
|
+
* Fix bug where global Rails helpers are inaccessible from nested components. Before, `helpers` was pointing to parent component.
|
16
|
+
|
17
|
+
*Franco Sebregondi*
|
18
|
+
|
19
|
+
# 2.10.0
|
20
|
+
|
21
|
+
* Raise an `ArgumentError` with a helpful message when Ruby cannot parse a component class.
|
22
|
+
|
23
|
+
*Max Beizer*
|
24
|
+
|
25
|
+
# 2.9.0
|
26
|
+
|
27
|
+
* Cache components per-request in development, preventing unnecessary recompilation during a single request.
|
28
|
+
|
29
|
+
*Felipe Sateler*
|
30
|
+
|
31
|
+
# 2.8.0
|
32
|
+
|
33
|
+
* Add `before_render`, deprecating `before_render_check`.
|
34
|
+
|
35
|
+
*Joel Hawksley*
|
36
|
+
|
3
37
|
# 2.7.0
|
4
38
|
|
5
39
|
* Add `rendered_component` method to `ViewComponent::TestHelpers` which exposes the raw output of the rendered component.
|
data/README.md
CHANGED
@@ -193,7 +193,7 @@ end
|
|
193
193
|
|
194
194
|
### Sidecar Assets
|
195
195
|
|
196
|
-
ViewComponents supports two options for defining view files.
|
196
|
+
ViewComponents supports two options for defining view files.
|
197
197
|
|
198
198
|
#### Sidecar view
|
199
199
|
|
@@ -274,6 +274,19 @@ end
|
|
274
274
|
|
275
275
|
_To assert that a component has not been rendered, use `refute_component_rendered` from `ViewComponent::TestHelpers`._
|
276
276
|
|
277
|
+
### `before_render`
|
278
|
+
|
279
|
+
Components can define a `before_render` method to be called before a component is rendered, when `helpers` is able to be used:
|
280
|
+
|
281
|
+
`app/components/confirm_email_component.rb`
|
282
|
+
```ruby
|
283
|
+
class MyComponent < ViewComponent::Base
|
284
|
+
def before_render
|
285
|
+
@my_icon = helpers.star_icon
|
286
|
+
end
|
287
|
+
end
|
288
|
+
```
|
289
|
+
|
277
290
|
### Rendering collections
|
278
291
|
|
279
292
|
Use `with_collection` to render a ViewComponent with a collection:
|
@@ -439,6 +452,21 @@ test "render component" do
|
|
439
452
|
end
|
440
453
|
```
|
441
454
|
|
455
|
+
To test components that use `with_content_areas`:
|
456
|
+
|
457
|
+
```ruby
|
458
|
+
test "renders content_areas template with content " do
|
459
|
+
render_inline(ContentAreasComponent.new(footer: "Bye!")) do |component|
|
460
|
+
component.with(:title, "Hello!")
|
461
|
+
component.with(:body) { "Have a nice day." }
|
462
|
+
end
|
463
|
+
|
464
|
+
assert_selector(".title", text: "Hello!")
|
465
|
+
assert_selector(".body", text: "Have a nice day.")
|
466
|
+
assert_selector(".footer", text: "Bye!")
|
467
|
+
end
|
468
|
+
```
|
469
|
+
|
442
470
|
#### Action Pack Variants
|
443
471
|
|
444
472
|
Use the `with_variant` helper to test specific variants:
|
@@ -703,6 +731,7 @@ ViewComponent is far from a novel idea! Popular implementations of view componen
|
|
703
731
|
## Resources
|
704
732
|
|
705
733
|
- [Encapsulating Views, RailsConf 2020](https://youtu.be/YVYRus_2KZM)
|
734
|
+
- [Rethinking the View Layer with Components - Ruby Rogues Podcast](https://devchat.tv/ruby-rogues/rr-461-rethinking-the-view-layer-with-components-with-joel-hawksley/)
|
706
735
|
- [ViewComponent at GitHub with Joel Hawksley](https://the-ruby-blend.fireside.fm/9)
|
707
736
|
- [Components, HAML vs ERB, and Design Systems](https://the-ruby-blend.fireside.fm/4)
|
708
737
|
- [Choosing the Right Tech Stack with Dave Paola](https://5by5.tv/rubyonrails/307)
|
@@ -750,10 +779,15 @@ ViewComponent is built by:
|
|
750
779
|
|@blakewilliams|@seanpdoyle|@tclem|@nashby|@jaredcwhite|
|
751
780
|
|Boston, MA|New York, NY|San Francisco, CA|Minsk|Portland, OR|
|
752
781
|
|
753
|
-
|<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" />|
|
754
|
-
|
755
|
-
|@simonrand|@fugufish|@cover|@franks921|
|
756
|
-
|Dublin, Ireland|Salt Lake City, Utah|Barcelona|South Africa|
|
782
|
+
|<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" />|<img src="https://avatars.githubusercontent.com/fsateler?s=256" alt="fsateler" width="128" />|
|
783
|
+
|:---:|:---:|:---:|:---:|:---:|
|
784
|
+
|@simonrand|@fugufish|@cover|@franks921|@fsateler|
|
785
|
+
|Dublin, Ireland|Salt Lake City, Utah|Barcelona|South Africa|Chile|
|
786
|
+
|
787
|
+
|<img src="https://avatars.githubusercontent.com/maxbeizer?s=256" alt="maxbeizer" width="128" />|<img src="https://avatars.githubusercontent.com/franco?s=256" alt="franco" width="128" />|<img src="https://avatars.githubusercontent.com/tbroad-ramsey?s=256" alt="tbroad-ramsey" width="128" />|
|
788
|
+
|:---:|:---:|:---:|
|
789
|
+
|@maxbeizer|@franco|@tbroad-ramsey|
|
790
|
+
|Nashville, TN|Switzerland|Spring Hill, TN|
|
757
791
|
|
758
792
|
## License
|
759
793
|
|
data/lib/view_component/base.rb
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
require "action_view"
|
4
4
|
require "active_support/configurable"
|
5
5
|
require "view_component/collection"
|
6
|
+
require "view_component/compile_cache"
|
6
7
|
require "view_component/previewable"
|
7
8
|
|
8
9
|
module ViewComponent
|
@@ -11,7 +12,7 @@ module ViewComponent
|
|
11
12
|
include ViewComponent::Previewable
|
12
13
|
|
13
14
|
# For CSRF authenticity tokens in forms
|
14
|
-
delegate :form_authenticity_token, :protect_against_forgery?, to: :helpers
|
15
|
+
delegate :form_authenticity_token, :protect_against_forgery?, :config, to: :helpers
|
15
16
|
|
16
17
|
class_attribute :content_areas
|
17
18
|
self.content_areas = [] # class_attribute:default doesn't work until Rails 5.2
|
@@ -66,7 +67,7 @@ module ViewComponent
|
|
66
67
|
# Assign captured content passed to component as a block to @content
|
67
68
|
@content = view_context.capture(self, &block) if block_given?
|
68
69
|
|
69
|
-
|
70
|
+
before_render
|
70
71
|
|
71
72
|
if render?
|
72
73
|
send(self.class.call_method_name(@variant))
|
@@ -77,6 +78,10 @@ module ViewComponent
|
|
77
78
|
@current_template = old_current_template
|
78
79
|
end
|
79
80
|
|
81
|
+
def before_render
|
82
|
+
before_render_check
|
83
|
+
end
|
84
|
+
|
80
85
|
def before_render_check
|
81
86
|
# noop
|
82
87
|
end
|
@@ -101,9 +106,9 @@ module ViewComponent
|
|
101
106
|
@controller ||= view_context.controller
|
102
107
|
end
|
103
108
|
|
104
|
-
# Provides a proxy to access helper methods
|
109
|
+
# Provides a proxy to access helper methods from the context of the current controller
|
105
110
|
def helpers
|
106
|
-
@helpers ||= view_context
|
111
|
+
@helpers ||= controller.view_context
|
107
112
|
end
|
108
113
|
|
109
114
|
# Removes the first part of the path and the extension.
|
@@ -156,8 +161,8 @@ module ViewComponent
|
|
156
161
|
attr_accessor :source_location
|
157
162
|
|
158
163
|
# Render a component collection.
|
159
|
-
def with_collection(
|
160
|
-
Collection.new(self,
|
164
|
+
def with_collection(collection, **args)
|
165
|
+
Collection.new(self, collection, **args)
|
161
166
|
end
|
162
167
|
|
163
168
|
# Provide identifier for ActionView template annotations
|
@@ -188,9 +193,7 @@ module ViewComponent
|
|
188
193
|
end
|
189
194
|
|
190
195
|
def compiled?
|
191
|
-
|
192
|
-
|
193
|
-
@compiled && ActionView::Base.cache_template_loading
|
196
|
+
CompileCache.compiled?(self)
|
194
197
|
end
|
195
198
|
|
196
199
|
# Compile templates to instance methods, assuming they haven't been compiled already.
|
@@ -205,6 +208,12 @@ module ViewComponent
|
|
205
208
|
return false
|
206
209
|
end
|
207
210
|
|
211
|
+
if instance_methods(false).include?(:before_render_check)
|
212
|
+
ActiveSupport::Deprecation.warn(
|
213
|
+
"`before_render_check` will be removed in v3.0.0. Use `before_render` instead."
|
214
|
+
)
|
215
|
+
end
|
216
|
+
|
208
217
|
# Remove any existing singleton methods,
|
209
218
|
# as Ruby warns when redefining a method.
|
210
219
|
remove_possible_singleton_method(:variants)
|
@@ -239,8 +248,8 @@ module ViewComponent
|
|
239
248
|
# starting line number so errors that are raised will point to the
|
240
249
|
# correct line in the component template.
|
241
250
|
line_number =
|
242
|
-
if ActionView::Base.respond_to?(:
|
243
|
-
ActionView::Base.
|
251
|
+
if ActionView::Base.respond_to?(:annotate_rendered_view_with_filenames) &&
|
252
|
+
ActionView::Base.annotate_rendered_view_with_filenames
|
244
253
|
-2
|
245
254
|
else
|
246
255
|
-1
|
@@ -260,7 +269,7 @@ module ViewComponent
|
|
260
269
|
RUBY
|
261
270
|
end
|
262
271
|
|
263
|
-
|
272
|
+
CompileCache.register self
|
264
273
|
end
|
265
274
|
|
266
275
|
# we'll eventually want to update this to support other types
|
@@ -298,7 +307,16 @@ module ViewComponent
|
|
298
307
|
parameter = validate_default ? collection_parameter : provided_collection_parameter
|
299
308
|
|
300
309
|
return unless parameter
|
301
|
-
return if
|
310
|
+
return if initialize_parameters.map(&:last).include?(parameter)
|
311
|
+
|
312
|
+
# If Ruby cannot parse the component class, then the initalize
|
313
|
+
# parameters will be empty and ViewComponent will not be able to render
|
314
|
+
# the component.
|
315
|
+
if initialize_parameters.empty?
|
316
|
+
raise ArgumentError.new(
|
317
|
+
"#{self} initializer is empty or invalid."
|
318
|
+
)
|
319
|
+
end
|
302
320
|
|
303
321
|
raise ArgumentError.new(
|
304
322
|
"#{self} initializer must accept " \
|
@@ -308,6 +326,10 @@ module ViewComponent
|
|
308
326
|
|
309
327
|
private
|
310
328
|
|
329
|
+
def initialize_parameters
|
330
|
+
instance_method(:initialize).parameters
|
331
|
+
end
|
332
|
+
|
311
333
|
def provided_collection_parameter
|
312
334
|
@provided_collection_parameter ||= nil
|
313
335
|
end
|
@@ -9,7 +9,7 @@ module ViewComponent
|
|
9
9
|
@component.validate_collection_parameter!(validate_default: true)
|
10
10
|
|
11
11
|
@collection.map do |item|
|
12
|
-
content = @component.new(component_options(item, iterator)).render_in(view_context, &block)
|
12
|
+
content = @component.new(**component_options(item, iterator)).render_in(view_context, &block)
|
13
13
|
iterator.iterate!
|
14
14
|
content
|
15
15
|
end.join.html_safe
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ViewComponent
|
4
|
+
# Keeps track of which templates have already been compiled
|
5
|
+
# This is not part of the public API
|
6
|
+
module CompileCache
|
7
|
+
mattr_accessor :cache, instance_reader: false, instance_accessor: false do
|
8
|
+
Set.new
|
9
|
+
end
|
10
|
+
module_function
|
11
|
+
|
12
|
+
def register(klass)
|
13
|
+
cache << klass
|
14
|
+
end
|
15
|
+
|
16
|
+
def compiled?(klass)
|
17
|
+
cache.include? klass
|
18
|
+
end
|
19
|
+
|
20
|
+
def invalidate!
|
21
|
+
cache.clear
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -69,6 +69,10 @@ module ViewComponent
|
|
69
69
|
get "#{options.preview_route}/*path", to: "view_components#previews", as: :preview_view_component, internal: true
|
70
70
|
end
|
71
71
|
end
|
72
|
+
|
73
|
+
app.executor.to_run :before do
|
74
|
+
CompileCache.invalidate! unless ActionView::Base.cache_template_loading
|
75
|
+
end
|
72
76
|
end
|
73
77
|
end
|
74
78
|
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.11.1
|
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-
|
11
|
+
date: 2020-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -142,6 +142,34 @@ dependencies:
|
|
142
142
|
- - "~>"
|
143
143
|
- !ruby/object:Gem::Version
|
144
144
|
version: 0.13.0
|
145
|
+
- !ruby/object:Gem::Dependency
|
146
|
+
name: simplecov
|
147
|
+
requirement: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - "~>"
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: 0.18.0
|
152
|
+
type: :development
|
153
|
+
prerelease: false
|
154
|
+
version_requirements: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
156
|
+
- - "~>"
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: 0.18.0
|
159
|
+
- !ruby/object:Gem::Dependency
|
160
|
+
name: simplecov-erb
|
161
|
+
requirement: !ruby/object:Gem::Requirement
|
162
|
+
requirements:
|
163
|
+
- - "~>"
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '0.1'
|
166
|
+
type: :development
|
167
|
+
prerelease: false
|
168
|
+
version_requirements: !ruby/object:Gem::Requirement
|
169
|
+
requirements:
|
170
|
+
- - "~>"
|
171
|
+
- !ruby/object:Gem::Version
|
172
|
+
version: '0.1'
|
145
173
|
description:
|
146
174
|
email:
|
147
175
|
- opensource+view_component@github.com
|
@@ -172,6 +200,7 @@ files:
|
|
172
200
|
- lib/view_component.rb
|
173
201
|
- lib/view_component/base.rb
|
174
202
|
- lib/view_component/collection.rb
|
203
|
+
- lib/view_component/compile_cache.rb
|
175
204
|
- lib/view_component/engine.rb
|
176
205
|
- lib/view_component/preview.rb
|
177
206
|
- lib/view_component/previewable.rb
|