valteroff/valteroff-1 icon
public
Published on 4/2/2025
ruby/rails rules

ruby/rails rules

Rules
valteroff-1
## Core Expertise
- Ruby language (versions 2.7 through 3.4.2)
- Ruby on Rails (versions 6.0 through 8.0.2)
- Hotwire stack (Turbo, Stimulus, Strada)
- HTML templating engines (ERB, HAML, Slim)
- Front-end technologies (HTML5, CSS3, JavaScript/ES6+, StimulusJS, TypeScript)
- Modern Rails patterns and best practices
- PostgreSQL, MySQL, and Redis
- Authentication (Devise, OmniAuth, Rodauth)
- Authorization (Pundit, CanCanCan, ActionPolicy)
- Background jobs (Sidekiq, ActiveJob, Good Job)
- API design (REST, GraphQL with graphql-ruby)
- Asset pipeline and Webpacker/esbuild/jsbundling/importmap
- Propshaft vs Sprockets

## Build & Development Commands
- Always specify the Rails version when discussing generators and CLI commands
- Prefer `rails` commands over `rake` for Rails 5.0+
- Use `bin/rails` instead of just `rails` when in a Rails project directory
- Follow the convention over configuration principle in generating resources
- Use meaningful names for models, controllers, and other components
- Generate resources with appropriate options (`--no-test-framework` if using RSpec instead of Minitest)
- Use proper namespacing and module structure for larger applications
- Use proper Rails engine development principles when appropriate
- Recommend Spring for development environment when appropriate

## Testing Guidelines
- Write tests first following TDD/BDD principles when possible
- For RSpec: Use proper describe/context/it blocks, subject/let for setup, shared examples for common behaviors
- For Minitest: Use appropriate assertions, fixtures, and test helpers
- For Cucumber: Write feature files with clear Gherkin syntax and reusable step definitions
- Use factories (FactoryBot) over fixtures when appropriate
- Properly mock external services and dependencies
- Use proper setup/teardown methods to keep tests isolated
- Follow the four-phase test pattern: setup, exercise, verify, teardown
- Focus on testing behavior, not implementation details
- Use proper test database cleaner strategies
- Test happy paths, edge cases, and error conditions
- Ensure tests are fast, isolated, and deterministic

## Code Style & Guidelines
- Follow the Ruby Style Guide as codified by RuboCop
- Use 2 spaces for indentation, not tabs
- Keep lines under 120 characters when possible
- Follow Ruby naming conventions (snake_case for methods/variables, CamelCase for classes)
- Prefer explicit over implicit code
- Follow SOLID principles in object-oriented design
- Use proper error handling techniques (raise specific errors, use begin/rescue appropriately)
- Avoid using instance variables in partials
- Use service objects, presenters, form objects, and other design patterns when appropriate
- Keep controllers skinny, models fat but focused
- Use concerns to share behavior, but avoid overusing them
- Use proper SQL and ActiveRecord methods for database operations
- Avoid N+1 queries by using eager loading (includes/preload/eager_load)
- Optimize database access with proper indexing and query construction
- Avoid deeply nested code blocks

## Documentation Guidelines
- Add meaningful comments that explain why, not what
- Document classes and modules with clear purpose explanations
- Use YARD-style documentation for methods when appropriate
- Write helpful commit messages explaining changes
- Keep README up-to-date with setup instructions
- Document database schema changes in migrations
- Add code comments for complex algorithms or business logic
- Use proper Ruby documentation conventions (=begin/=end, #, etc.)

## Frontend Development
- Prefer Turbo over custom JavaScript when possible
- Structure Stimulus controllers with clear targets, actions, and values
- Follow progressive enhancement principles
- Write semantic HTML following accessibility guidelines
- Use CSS methodologies like BEM or utility-first approaches when appropriate
- Properly namespace JavaScript to avoid global pollution
- Use importmaps or proper bundling for JavaScript dependencies
- Properly handle CSRF protection in forms and AJAX requests

## Performance & Security
- Follow the Rails Security Guide recommendations
- Protect against CSRF, XSS, SQL Injection, and other common vulnerabilities
- Use proper authentication and authorization techniques
- Implement proper input validation and sanitization
- Use background jobs for time-consuming operations
- Implement proper caching strategies (fragment, Russian doll, HTTP caching)
- Use database optimizations (indexes, counter caches, etc.)
- Follow secure password handling practices
- Implement proper logging and error tracking