Skip to content

Commit 68b7f7c

Browse files
authored
add basic How it Works docs (#2239)
* add basic How it Works docs * edits * lints * clarify initialization lifecyle section
1 parent 6c5dca1 commit 68b7f7c

File tree

9 files changed

+57
-8
lines changed

9 files changed

+57
-8
lines changed

docs/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: default
33
title: Changelog
4-
nav_order: 5
4+
nav_order: 6
55
---
66

77
<!-- Add unreleased changes under the "main" heading. -->
@@ -10,6 +10,10 @@ nav_order: 5
1010

1111
## main
1212

13+
* Add documentation on how ViewComponent works.
14+
15+
*Joel Hawksley*
16+
1317
## 3.22.0
1418

1519
* Rewrite `ViewComponents at GitHub` documentation as more general `Best practices`.

docs/CODE_OF_CONDUCT.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: default
33
title: Code of conduct
4-
nav_order: 6
4+
nav_order: 7
55
---
66

77
# Contributor Covenant Code of Conduct

docs/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: default
33
title: Contributing
4-
nav_order: 7
4+
nav_order: 9
55
---
66

77
# Contributing

docs/compatibility.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: default
33
title: Compatibility
4-
nav_order: 6
4+
nav_order: 8
55
---
66

77
# Compatibility

docs/history.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: default
33
title: History
4-
nav_order: 8
4+
nav_order: 10
55
---
66

77
# History

docs/how-it-works.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
layout: default
3+
title: How it works
4+
nav_order: 5
5+
---
6+
7+
## How ViewComponent Works
8+
9+
### At application boot
10+
11+
When `ViewComponent::Base` is inherited from, `ViewComponent::Compiler.compile` compiles a component's template(s) into instance method(s) on the component:
12+
13+
```ruby
14+
class MyComponent < ViewComponent::Base
15+
end
16+
```
17+
18+
```erb
19+
<%= Time.now %>
20+
```
21+
22+
Compiles to:
23+
24+
```ruby
25+
class MyComponent < ViewComponent::Base
26+
def _call_my_component
27+
@output_buffer.append = (Time.now)
28+
@output_buffer
29+
end
30+
end
31+
```
32+
33+
### At component instantiation
34+
35+
Component initializer runs if defined. ViewComponent does not perform any actions.
36+
37+
### At render
38+
39+
When a ViewComponent is passed to `render`, Rails calls `render_in`, passing the current [ActionView::Context](https://api.rubyonrails.org/classes/ActionView/Context.html). `ActionView::Context` is used to provide access to the current controller, request, and helpers, which is why they cannot be referenced until a component is rendered.
40+
41+
First, ViewComponent calls the `before_render` method, which gives ViewComponents the opportunity to execute logic before render time but after initialization.
42+
43+
Second, ViewComponent calls `render?`, returning early with an empty string if `render?` returns false.
44+
45+
ViewComponent then renders the component template.

docs/known_issues.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: default
33
title: Known issues
4-
nav_order: 9
4+
nav_order: 11
55
---
66

77
# Known issues

docs/logo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: default
33
title: Logo
4-
nav_order: 11
4+
nav_order: 12
55
---
66

77
# Logo

docs/resources.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: default
33
title: Resources
4-
nav_order: 12
4+
nav_order: 13
55
---
66

77
# Resources

0 commit comments

Comments
 (0)