base on A RuboCop extension focused on enforcing Rails best practices and coding conventions. # RuboCop Rails [![Gem Version](https://badge.fury.io/rb/rubocop-rails.svg)](https://badge.fury.io/rb/rubocop-rails) [![CI](https://github.com/rubocop/rubocop-rails/actions/workflows/test.yml/badge.svg)](https://github.com/rubocop/rubocop-rails/actions/workflows/test.yml) A [RuboCop](https://github.com/rubocop/rubocop) 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 to [rubocop-rails_config](https://rubygems.org/gems/rubocop-rails_config) gem. ## Installation Just install the `rubocop-rails` gem ```sh $ gem install rubocop-rails ``` or if you use bundler put this in your `Gemfile` ```ruby 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`. ```yaml plugins: rubocop-rails ``` Alternatively, use the following array notation when specifying multiple extensions. ```yaml plugins: - rubocop-other-extension - rubocop-rails ``` Now you can run `rubocop` and it will automatically load the RuboCop Rails cops together with the standard cops. > [!NOTE] > The plugin system is supported in RuboCop 1.72+. In earlier versions, use `require` instead of `plugins`. ### Command line ```sh $ rubocop --plugin rubocop-rails ``` ### Rake task ```ruby require 'rubocop/rake_task' RuboCop::RakeTask.new do |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 specified for `TargetRailsVersion` then it is used. Acceptable values are specified as a float (e.g., 7.2); the patch version of Rails should not be included. ```yaml AllCops: TargetRailsVersion: 7.2 ``` If `TargetRailsVersion` is not set, RuboCop will parse the Gemfile.lock or gems.locked file to find the version of Rails that has been bound to the application. If neither of those files exist, RuboCop will use Rails 5.0 as the default. ### `AllCops: MigratedSchemaVersion` By specifying the `MigratedSchemaVersion` option, migration files that have already been run can be ignored. When `MigratedSchemaVersion: '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: ```yaml 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 sync between your local copy and what it actually is in production, depending on when `bin/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 following `config.generators.after_generate` setting to your `config/environments/development.rb` to apply RuboCop autocorrection to code generated by `bin/rails g`. ```ruby # 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. In Rails 7.2+, it is recommended to use `config.generators.apply_rubocop_autocorrect_after_generate!` instead of the above setting: ```diff # 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 under [`lib/rubocop/cop/rails`](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: ```yaml Rails/FindBy: Exclude: - lib/example.rb ``` ## Documentation You can read a lot more about RuboCop Rails in its [official docs](https://docs.rubocop.org/rubocop-rails/). ## 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](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) Here are the Markdown snippets for the two badges: ``` markdown [![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](CONTRIBUTING.md). ## License `rubocop-rails` is MIT licensed. [See the accompanying file](LICENSE.txt) for the full text. ", Assign "at most 3 tags" to the expected json: {"id":"1090","tags":[]} "only from the tags list I provide: [{"id":77,"name":"3d"},{"id":89,"name":"agent"},{"id":17,"name":"ai"},{"id":54,"name":"algorithm"},{"id":24,"name":"api"},{"id":44,"name":"authentication"},{"id":3,"name":"aws"},{"id":27,"name":"backend"},{"id":60,"name":"benchmark"},{"id":72,"name":"best-practices"},{"id":39,"name":"bitcoin"},{"id":37,"name":"blockchain"},{"id":1,"name":"blog"},{"id":45,"name":"bundler"},{"id":58,"name":"cache"},{"id":21,"name":"chat"},{"id":49,"name":"cicd"},{"id":4,"name":"cli"},{"id":64,"name":"cloud-native"},{"id":48,"name":"cms"},{"id":61,"name":"compiler"},{"id":68,"name":"containerization"},{"id":92,"name":"crm"},{"id":34,"name":"data"},{"id":47,"name":"database"},{"id":8,"name":"declarative-gui "},{"id":9,"name":"deploy-tool"},{"id":53,"name":"desktop-app"},{"id":6,"name":"dev-exp-lib"},{"id":59,"name":"dev-tool"},{"id":13,"name":"ecommerce"},{"id":26,"name":"editor"},{"id":66,"name":"emulator"},{"id":62,"name":"filesystem"},{"id":80,"name":"finance"},{"id":15,"name":"firmware"},{"id":73,"name":"for-fun"},{"id":2,"name":"framework"},{"id":11,"name":"frontend"},{"id":22,"name":"game"},{"id":81,"name":"game-engine "},{"id":23,"name":"graphql"},{"id":84,"name":"gui"},{"id":91,"name":"http"},{"id":5,"name":"http-client"},{"id":51,"name":"iac"},{"id":30,"name":"ide"},{"id":78,"name":"iot"},{"id":40,"name":"json"},{"id":83,"name":"julian"},{"id":38,"name":"k8s"},{"id":31,"name":"language"},{"id":10,"name":"learning-resource"},{"id":33,"name":"lib"},{"id":41,"name":"linter"},{"id":28,"name":"lms"},{"id":16,"name":"logging"},{"id":76,"name":"low-code"},{"id":90,"name":"message-queue"},{"id":42,"name":"mobile-app"},{"id":18,"name":"monitoring"},{"id":36,"name":"networking"},{"id":7,"name":"node-version"},{"id":55,"name":"nosql"},{"id":57,"name":"observability"},{"id":46,"name":"orm"},{"id":52,"name":"os"},{"id":14,"name":"parser"},{"id":74,"name":"react"},{"id":82,"name":"real-time"},{"id":56,"name":"robot"},{"id":65,"name":"runtime"},{"id":32,"name":"sdk"},{"id":71,"name":"search"},{"id":63,"name":"secrets"},{"id":25,"name":"security"},{"id":85,"name":"server"},{"id":86,"name":"serverless"},{"id":70,"name":"storage"},{"id":75,"name":"system-design"},{"id":79,"name":"terminal"},{"id":29,"name":"testing"},{"id":12,"name":"ui"},{"id":50,"name":"ux"},{"id":88,"name":"video"},{"id":20,"name":"web-app"},{"id":35,"name":"web-server"},{"id":43,"name":"webassembly"},{"id":69,"name":"workflow"},{"id":87,"name":"yaml"}]" returns me the "expected json"