Configurazione RuboCop
Configurazione standard RuboCop per progetti PANDEV con regole ottimizzate per Rails API.
File .rubocop.yml Standard
Crea questo file nella root del progetto:
# .rubocop.yml
require:
- rubocop-rails
- rubocop-rspec
- rubocop-performance
AllCops:
TargetRubyVersion: 3.1
NewCops: enable
Exclude:
- 'vendor/**/*'
- 'db/schema.rb'
- 'db/migrate/*'
- 'bin/*'
- 'node_modules/**/*'
- 'tmp/**/*'
- 'log/**/*'
- 'public/**/*'
# Layout
Layout/LineLength:
Max: 120
AllowedPatterns: ['(\A|\s)#']
Layout/MultilineMethodCallIndentation:
EnforcedStyle: indented
Layout/FirstArrayElementIndentation:
EnforcedStyle: consistent
Layout/FirstHashElementIndentation:
EnforcedStyle: consistent
# Style
Style/Documentation:
Enabled: false
Style/FrozenStringLiteralComment:
Enabled: true
EnforcedStyle: always
Style/StringLiterals:
EnforcedStyle: single_quotes
Style/StringLiteralsInInterpolation:
EnforcedStyle: single_quotes
Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: comma
Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: comma
Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: comma
Style/ClassAndModuleChildren:
EnforcedStyle: compact
Style/Lambda:
EnforcedStyle: literal
# Metrics
Metrics/BlockLength:
Exclude:
- 'config/routes.rb'
- 'spec/**/*'
- 'config/environments/*'
Metrics/MethodLength:
Max: 20
CountAsOne: ['array', 'hash', 'heredoc']
Metrics/AbcSize:
Max: 20
Metrics/ClassLength:
Max: 150
Metrics/ModuleLength:
Max: 150
Metrics/CyclomaticComplexity:
Max: 8
Metrics/PerceivedComplexity:
Max: 8
# Rails specific
Rails/FilePath:
Enabled: false
Rails/UnknownEnv:
Environments:
- production
- development
- test
- staging
Rails/SkipsModelValidations:
Exclude:
- 'spec/**/*'
- 'db/migrate/*'
# RSpec specific
RSpec/ExampleLength:
Max: 20
RSpec/MultipleExpectations:
Max: 5
RSpec/NestedGroups:
Max: 5
RSpec/DescribeClass:
Exclude:
- 'spec/requests/**/*'
- 'spec/features/**/*'
- 'spec/system/**/*'
# Performance
Performance/Casecmp:
Enabled: true
Performance/StringReplacement:
Enabled: true
# Naming
Naming/PredicateName:
ForbiddenPrefixes:
- is_
Naming/AccessorMethodName:
Enabled: true
# Security
Security/Open:
Enabled: true
Security/Eval:
Enabled: trueInstallazione
Gemfile
Aggiungi al tuo Gemfile:
Bundle Install
Comandi Utili
Controllo Base
Auto-correzione
Configurazione Cop Specifici
Integrazione Editor
VS Code
Installa l'estensione Ruby LSP che include RuboCop automaticamente.
Configurazione .vscode/settings.json
Pre-commit Hook
Aggiungi al file .git/hooks/pre-commit:
Rendi eseguibile:
CI/CD Integration
GitHub Actions
Personalizzazioni PANDEV
Override per Progetti Specifici
Crea .rubocop_local.yml per override specifici:
Poi esegui:
Disabilitare Cop per Codice Legacy
Best Practices
Esegui RuboCop prima di ogni commit
Usa auto-fix con cautela - controlla sempre le modifiche
Non disabilitare cop senza motivo - documenta sempre il perché
Aggiorna regolarmente le versioni delle gem RuboCop
Condividi la configurazione nel team per coerenza
Troubleshooting
Problemi Comuni
RuboCop lento:
Conflitti di configurazione:
Versioni incompatibili:
Last updated
Was this helpful?