Encapsulating Views
summary
Rails views compile into methods on ActionView::Base so templates share instance variables and execute in a global scope, which makes unit testing and reasoning hard; GitHub builds ViewComponent to attach compiled templates to component objects, enabling per-component encapsulation, precompilation at boot to avoid per-process compile pauses, and fast unit tests (views compile into methods, components unit tests ~20ms vs controller ~1.5s). At scale—about 4,400 views and 3,000 partials—112 components cover ~550 usages and Rails 6.1 adds native support for third‑party component libraries.
AI-written summary · about
chapters (20)
- 0:00Start
- 3:45600 controllers
- 8:15Ecosystem Survey
- 8:53Draper & Cells
- 9:15Testing views
- 10:09View objects
- 12:09interface
- 17:46Views are methods
- 18:38Global scope
- 19:13Unit Testing
- 19:36Decorators, Presenters, Components
- 20:03one context for all views
- 26:10component gems
- 26:48Performance
- 28:59Levels of abstraction
- 29:473,000 partials
- 32:20Resiliency
- 32:35Exposing complexity
- 33:35Consistency
- 36:24Examples
related talks
57mViewComponents in the Real World
34mViewComponents in the Real World
38mRethinking the View Layer with Components
54mThe View is Clear From Here
44m"The View Is Clear from Here"
45mView Components in Rails
31mComponent Driven UI with ViewComponent gem
33mRefactoring Towards Component-based Rails Architectures
