• Stars
    star
    799
  • Rank 57,011 (Top 2 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 6 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A RuboCop extension focused on enforcing Rails best practices and coding conventions.

RuboCop Rails

Gem Version CircleCI

A RuboCop extension focused on enforcing Rails best practices and coding conventions.

Note: This repository manages rubocop-rails gem (>= 2.0.0). rubocop-rails gem (<= 1.5.0) has been renamed to rubocop-rails_config gem.

Installation

Just install the rubocop-rails gem

$ gem install rubocop-rails

or if you use bundler put this in your Gemfile

gem 'rubocop-rails', require: false

Usage

You need to tell RuboCop to load the Rails extension. There are three ways to do this:

RuboCop configuration file

Put this into your .rubocop.yml.

require: rubocop-rails

Alternatively, use the following array notation when specifying multiple extensions.

require:
  - rubocop-other-extension
  - rubocop-rails

Now you can run rubocop and it will automatically load the RuboCop Rails cops together with the standard cops.

Command line

$ rubocop --require rubocop-rails

Note: --rails option is required while rubocop command supports --rails option.

Rake task

require 'rubocop/rake_task'

RuboCop::RakeTask.new do |task|
  task.requires << 'rubocop-rails'
end

Rails configuration tip

If you are using Rails 6.1 or newer, add the following config.generators.after_generate setting to your config/environments/development.rb to apply RuboCop autocorrection to code generated by bin/rails g.

# config/environments/development.rb
Rails.application.configure do
  config.generators.after_generate do |files|
    parsable_files = files.filter { |file| file.end_with?('.rb') }
    unless parsable_files.empty?
      system("bundle exec rubocop -A --fail-level=E #{parsable_files.shelljoin}", exception: true)
    end
  end
end

It uses rubocop -A to apply Style/FrozenStringLiteralComment and other unsafe autocorrection cops. rubocop -A is unsafe autocorrection, but code generated by default is simple and less likely to be incompatible with rubocop -A. If you have problems you can replace it with rubocop -a instead.

The Cops

All cops are located under lib/rubocop/cop/rails, and contain examples/documentation.

In your .rubocop.yml, you may treat the Rails cops just like any other cop. For example:

Rails/FindBy:
  Exclude:
    - lib/example.rb

Documentation

You can read a lot more about RuboCop Rails in its official docs.

Compatibility

Rails cops support the following versions:

  • Rails 4.2+

Readme Badge

If you use RuboCop Rails in your project, you can include one of these badges in your readme to let people know that your code is written following the community Rails Style Guide.

Rails Style Guide

Rails Style Guide

Here are the Markdown snippets for the two badges:

[![Rails Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop-rails)

[![Rails Style Guide](https://img.shields.io/badge/code_style-community-brightgreen.svg)](https://rails.rubystyle.guide)

Contributing

Checkout the contribution guidelines.

License

rubocop-rails is MIT licensed. See the accompanying file for the full text.

More Repositories

1

ruby-style-guide

A community-driven Ruby coding style guide
16,439
star
2

rubocop

A Ruby static code analyzer and formatter, based on the community Ruby style guide.
Ruby
12,608
star
3

rails-style-guide

A community-driven Ruby on Rails style guide
6,469
star
4

rspec-style-guide

Best practices for writing your specs!
952
star
5

rubocop-rspec

Code style checking for RSpec files.
Ruby
804
star
6

rubocop-performance

An extension of RuboCop focused on code performance checks.
Ruby
665
star
7

guard-rubocop

Guard plugin for RuboCop
Ruby
262
star
8

rubocop-minitest

Code style checking for Minitest files.
Ruby
140
star
9

rubocop-emacs

An Emacs interface for RuboCop
Emacs Lisp
128
star
10

rubocop-ast

RuboCop's AST extensions and NodePattern functionality
Ruby
99
star
11

rubocop-rake

A RuboCop plugin for Rake
Ruby
75
star
12

rubocop-md

RuboCop for Markdown code snippets
Ruby
73
star
13

minitest-style-guide

Best practices for writing your tests
68
star
14

rubocop-jp

A place for RuboCop discussions in Japanese
55
star
15

vscode-rubocop

The official VS Code extension for the RuboCop linter and code formatter.
TypeScript
50
star
16

rubocop-extension-generator

A generator of RuboCop's custom cops gem.
Ruby
48
star
17

rubocop-factory_bot

Code style checking for factory_bot files.
Ruby
44
star
18

rubocop-capybara

Code style checking for Capybara files.
Ruby
39
star
19

rubocop-sequel

Code style checking for Sequel
Ruby
28
star
20

rubocop-rubycw

Wrap ruby -cw by RuboCop
Ruby
26
star
21

rubocop-rspec_rails

Code style checking for Rails-related RSpec files.
Ruby
14
star
22

blog.rubystyle.guide

A companion blog to the community Ruby, Rails and RSpec guides
HTML
7
star
23

packaging-style-guide

Best practices for writing downstream compatible code!
6
star
24

docs.rubocop.org

RuboCop's documentation site
Handlebars
4
star
25

circleci-ruby-snapshot-image

Builds a Ruby nightly image and publishes it to Docker Hub
Ruby
3
star
26

.github

2
star
27

capybara-style-guide

A community-driven Capybara coding style guide
1
star