| name | rails-code-quality |
| description | RuboCop metrics, Brakeman security, code style conventions, and quality checks for this Rails codebase |
Code Quality Commands
bin/rubocop
bin/rubocop -a
bin/brakeman
Ruby Code Style
Basic Style
- ALWAYS start with
# frozen_string_literal: true
- Use double quotes for strings
- Use new hash syntax:
{ a: 1 } not { :a => 1 }
- Two spaces for indentation, no tabs
- No trailing whitespace
- Use
&&/|| instead of and/or
- Method definitions with parentheses:
def my_method(arg) not def my_method arg
Constants & Magic Numbers
RuboCop Metrics (Enforced)
These metrics are enforced by RuboCop in this codebase:
- Method length: max 50 lines
- Class length: max 300 lines
- Cyclomatic complexity: max 15
- Perceived complexity: max 12
- AbcSize: max 41 (excludes migrations)
- Block length: max 75 (excluded in specs)
Running Quality Checks
bin/rubocop
bin/rubocop app/models/facility.rb
bin/rubocop -a
bin/rubocop -a --safe
bin/brakeman
bin/brakeman --no-pager --no-highlight --skip-warning-list
Fixing RuboCop Issues
- Run
bin/rubocop to see issues
- Run
bin/rubocop -a to auto-fix
- Manually fix remaining issues
- Run
bin/rubocop again to verify
Security Best Practices (Brakeman)
- Never expose secrets or API keys
- Use strong parameters to prevent mass assignment
- Sanitize user input
- Use HTTPS in production
- Keep dependencies updated
- Run
bin/brakeman regularly
Code Organization
- Keep methods under 50 lines
- Keep classes under 300 lines
- Reduce complexity by extracting methods
- Use services for complex business logic
- Follow SOLID principles
Naming Conventions
Ruby/Rails
- Models: Singular PascalCase (e.g.,
Facility)
- Controllers: Plural PascalCase (e.g.,
FacilitiesController)
- Components: Namespace::Name (e.g.,
Facilities::CardComponent)
- Services: Action + Service (e.g.,
FacilitySerializer)
- Factories:
factory :name (e.g., factory :facility)
- Tests: *_spec.rb suffix (e.g.,
facility_spec.rb)
- Constants: SCREAMING_SNAKE_CASE (e.g.,
STATUS_CLASSES)
Error Handling
- Use
raise for programmer errors
- Use
Result.new(errors: [...]) for service object validation failures
- Handle exceptions with
begin/rescue blocks when necessary
- Log errors appropriately before re-raising if needed
Pre-Commit Workflow
Always run before committing:
bin/rspec
bin/rubocop
bin/brakeman
bin/rubocop -a
Important Notes
- Always run tests and linting before committing
- Follow Ruby style guide conventions
- Keep complexity low by extracting methods
- Use RuboCop metrics as guidelines
- Security scan with Brakeman regularly