Skip to content

Draft: Experiment using ViewComponent instead of two partials

Rémy Coutable requested to merge experiment-view-component into master

What does this MR do?

This experimentally introduced ViewComponent in the project and replaces two partial with view components.

View components have a few advantages (as explained at https://viewcomponent.org/motivation.html):

Testing

Unlike traditional Rails views, ViewComponents can be unit-tested. In the GitHub codebase, ViewComponent unit tests take around 25 milliseconds each, compared to about six seconds for controller tests.

Rails views are typically tested with slow integration tests that also exercise the routing and controller layers in addition to the view. This cost often discourages thorough test coverage.

With ViewComponent, integration tests can be reserved for end-to-end assertions, with permutations and corner cases covered at the unit level.

Data Flow

Traditional Rails views have an implicit interface, making it hard to reason about what information is needed to render, leading to subtle bugs when rendering the same view in different contexts.

ViewComponents use a standard Ruby initializer that clearly defines what is needed to render, making them easier (and safer) to reuse than partials.

Performance

Based on our benchmarks, ViewComponents are ~10x faster than partials.

Standards

Views often fail basic Ruby code quality standards: long methods, deep conditional nesting, and mystery guests abound.

ViewComponents are Ruby objects, making it easy to follow (and enforce) code quality standards.

IMO the main argument is the Data Flow one, as it ensures views have a clear interface and can thus be easily tested.

Screenshots (strongly suggested)

View Partial View component
Wiki clone panel wiki-clone-panel-partial wiki-clone-panel-view-component
Admin labels admin-labels-partial admin-labels-view-component
Group labels group-labels-partial group-labels-view-component
Project labels project-labels-partial project-labels-view-component

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • Security reports checked/validated by a reviewer from the AppSec team
Edited by Rémy Coutable

Merge request reports

Loading