A RuboCop extension that encourages ViewComponent best practices.
Add to your Gemfile:
gem 'rubocop-view_component', require: falseAdd to your .rubocop.yml:
require:
- rubocop-view_componentThis gem provides several cops to enforce ViewComponent best practices:
- ViewComponent/ComponentSuffix - Enforce
-Componentsuffix for ViewComponent classes - ViewComponent/NoGlobalState - Prevent direct access to
params,request,session, etc. - ViewComponent/PreferPrivateMethods - Suggest making helper methods private (analyzes ERB templates to avoid flagging methods used in views)
- ViewComponent/PreferSlots - Detect HTML parameters that should be slots
- ViewComponent/PreferComposition - Avoid inheriting one ViewComponent from another (prefer composition)
- ViewComponent/TestRenderedOutput - Encourage testing rendered output over private methods
- ViewComponent/MissingPreview - Ensure every ViewComponent has a corresponding preview file (requires
PreviewPathsconfiguration). Classes listed inViewComponentParentClassesare automatically exempt, as abstract base classes are not rendered standalone.
By default, the cops detect classes that inherit from ViewComponent::Base or ApplicationComponent. To add extra parent classes, use inherit_mode: merge so the defaults are preserved:
# .rubocop.yml
ViewComponent:
inherit_mode:
merge:
- ViewComponentParentClasses
ViewComponentParentClasses:
- MyApp::BaseComponentTo replace the defaults entirely (e.g. if your project has renamed ApplicationComponent), omit inherit_mode:
# .rubocop.yml
ViewComponent:
ViewComponentParentClasses:
- ViewComponent::Base
- MyApp::BaseComponentSeveral cops (ComponentSuffix, PreferComposition, MissingPreview, TestRenderedOutput) default to running only on files under app/components/. If your project uses a different path, override Include in your .rubocop.yml:
# .rubocop.yml
ViewComponent/ComponentSuffix:
Include:
- 'app/components/**/*.rb'
- 'engines/*/app/components/**/*.rb'ViewComponent convention is to not call super in component initializers, but that may cause Lint/MissingSuper failures from RuboCop. We suggest disabling that rule for your view components directory, for example:
# .rubocop.yml
Lint/MissingSuper:
Exclude:
- 'app/components/**/*'After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install.
To release a new version:
- Update the version number in
lib/rubocop/view_component/version.rb - Run
bundle installto updateGemfile.lock - Commit and push both files
- Trigger the Push Gem workflow via GitHub Actions (uses trusted publishing — no API key needed)
The cops are tested against real-world component libraries as baselines to catch regressions.
The script/verify script downloads component libraries (cached in verification/), runs all ViewComponent cops against them, and compares the results to checked-in snapshots. This runs automatically in CI.
To verify against primer/view_components locally:
script/verify primerIf you intentionally change cop behavior, regenerate the snapshot:
script/verify primer --regenerateTo force download the latest Primer source:
script/verify primer --updateTo verify against x-govuk/govuk-components locally:
script/verify govukIf you intentionally change cop behavior, regenerate the snapshot:
script/verify govuk --regenerateTo force download the latest x-govuk source:
script/verify govuk --updateTo verify against baoagency/polaris_view_components locally:
script/verify polarisIf you intentionally change cop behavior, regenerate the snapshot:
script/verify polaris --regenerateTo force download the latest Polaris source:
script/verify polaris --updateBug reports and pull requests are welcome on GitHub at https://github.com/andyw8/rubocop-view_component.
The gem is available as open source under the terms of the MIT License.