Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

rubocop/rubocop-rails

Repository files navigation

Gem VersionCI

ARuboCop extension focused on enforcing Rails best practices and coding conventions.

Important

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

Installation

Just install therubocop-rails gem

$ gem install rubocop-rails

or if you use bundler put this in yourGemfile

gem'rubocop-rails',require:false

Usage

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

RuboCop configuration file

Put this into your.rubocop.yml.

plugins:rubocop-rails

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

plugins:  -rubocop-other-extension  -rubocop-rails

Now you can runrubocop and it will automatically load the RuboCop Railscops together with the standard cops.

Note

The plugin system is supported in RuboCop 1.72+. In earlier versions, userequire instead ofplugins.

Command line

$ rubocop --plugin rubocop-rails

Rake task

require'rubocop/rake_task'RuboCop::RakeTask.newdo |task|task.plugins <<'rubocop-rails'end

RuboCop Rails configuration

The following settings specific to RuboCop Rails can be configured in.rubocop.yml.

AllCops: TargetRailsVersion

What version of Rails is the inspected code using? If a value is specifiedforTargetRailsVersion then it is used. Acceptable values are specifiedas a float (e.g., 7.2); the patch version of Rails should not be included.

AllCops:TargetRailsVersion:7.2

IfTargetRailsVersion is not set, RuboCop will parse the Gemfile.lock orgems.locked file to find the version of Rails that has been bound to theapplication. If neither of those files exist, RuboCop will use Rails 5.0as the default.

AllCops: MigratedSchemaVersion

By specifying theMigratedSchemaVersion option, migration files that have already been run can be ignored.WhenMigratedSchemaVersion: '20241225000000' is set, migration files lower than or equal to '20241225000000' will be ignored.For example, to ignore db/migrate/20241225000000_create_articles.rb and earlier migrations you would configure it the following way:

AllCops:MigratedSchemaVersion:'20241225000000'

This prevents inspecting schema settings for already applied migration files.Changing already applied migrations should be avoided because it can lead to the schema getting out of syncbetween your local copy and what it actually is in production, depending on whenbin/rails db:migrate was executed.If you want to modify your schema to comply with the cops, you should instead create new migrations.

Rails configuration tip

In Rails 6.1+, add the followingconfig.generators.after_generate setting toyourconfig/environments/development.rb to apply RuboCop autocorrection to code generated bybin/rails g.

# config/environments/development.rbRails.application.configuredoconfig.generators.after_generatedo |files|parsable_files=files.filter{ |file|file.end_with?('.rb')}unlessparsable_files.empty?system("bundle exec rubocop -A --fail-level=E#{parsable_files.shelljoin}",exception:true)endendend

It usesrubocop -A to applyStyle/FrozenStringLiteralComment and other unsafe autocorrection cops.rubocop -A is unsafe autocorrection, but code generated by default is simple and less likely tobe incompatible withrubocop -A. If you have problems you can replace it withrubocop -a instead.

In Rails 7.2+, it is recommended to useconfig.generators.apply_rubocop_autocorrect_after_generate! instead of the above setting:

 # config/environments/development.rb Rails.application.configure do   (snip)   # Apply autocorrection by RuboCop to files generated by `bin/rails generate`.-  # config.generators.apply_rubocop_autocorrect_after_generate!+  config.generators.apply_rubocop_autocorrect_after_generate! end

You only need to uncomment.

The Cops

All cops are located underlib/rubocop/cop/rails, and containexamples/documentation.

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

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

Documentation

You can read a lot more about RuboCop Rails in itsofficial 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 thecontribution guidelines.

License

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

About

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

Topics

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp