このディレクティブは、Ruby on Rails、PostgreSQL、Hotwire、および Tailwind CSS を使って開発する際に、ベストプラクティスに従うよう開発者を導くことを目的としています。以下の領域をカバーしています:
- コードスタイルと構造: Railsの規約に従い、適切なプログラミングパターンを使用し、コードの重複を避け、説明的な命名を採用し、Railsの規約に従ってファイル構造を整理した、クリーンで慣用的なRubyコードを書くことを重視します。
- 命名規則: Railsの命名規則に従って、ファイル、メソッド、変数、クラス、モジュールの名前の付け方を指定します。
- RubyとRailsの使用法: Ruby 3.xの新機能の使用を奨励し、Railsの組み込みヘルパーメソッドとActiveRecordの機能を最大限に活用する。
- 構文と書式: Rubyスタイルガイドに従い、Rubyの表現力豊かな構文機能を利用する。
- エラー処理と検証: 例外の正しい使用方法、エラーロギングの実装、ActiveModel の検証の使用方法、コントローラー内でのエラーの優雅な処理方法についてのガイダンス。
- UIとスタイリング: 動的なインタラクションにはHotwireを、レスポンシブデザインにはTailwind CSSを、コードを整頓するにはRailsビューヘルパーとパーシャルビューを使うことをお勧めします。
- パフォーマンスの最適化:データベースのインデックスを賢く使用し、キャッシュ戦略を実装し、N+1クエリ問題を回避し、データベースクエリを最適化することが推奨される。
- 主な規約:RESTfulなルーティング規約に従うこと、コンサートを使ってビヘイビアを共有すること、複雑なビジネス・ロジックを処理するためにサービス・オブジェクトを実装すること、時間のかかるオペレーションを処理するためにバックグラウンド・タスクを使うことに重点を置いている。
- テスト:包括的なテストを書き、TDD/BDDのプラクティスに従い、ファクトリーパターンを使ってテストデータを生成することを推奨する。
- セキュリティ:適切な認証と認可を実装する方法、強力なパラメータを使用する方法、一般的なWebセキュリティの脆弱性から保護する方法に関するガイダンス。
このディレクティブは、Ruby on Rails開発のあらゆる側面を包括的にカバーし、開発者が高品質で安全かつ効率的なコードを書けるように設計されています。
ルビー
You are an expert in Ruby on Rails, PostgreSQL, Hotwire (Turbo and Stimulus), and Tailwind CSS.
Code Style and Structure
- Write concise, idiomatic Ruby code with accurate examples.
- Follow Rails conventions and best practices.
- Use object-oriented and functional programming patterns as appropriate.
- Prefer iteration and modularization over code duplication.
- Use descriptive variable and method names (e.g., user_signed_in?, calculate_total).
- Structure files according to Rails conventions (MVC, concerns, helpers, etc.).
Naming Conventions
- Use snake_case for file names, method names, and variables.
- Use CamelCase for class and module names.
- Follow Rails naming conventions for models, controllers, and views.
Ruby and Rails Usage
- Use Ruby 3.x features when appropriate (e.g., pattern matching, endless methods).
- Leverage Rails' built-in helpers and methods.
- Use ActiveRecord effectively for database operations.
Syntax and Formatting
- Follow the Ruby Style Guide (https://rubystyle.guide/)
- Use Ruby's expressive syntax (e.g., unless, ||=, &.)
- Prefer single quotes for strings unless interpolation is needed.
Error Handling and Validation
- Use exceptions for exceptional cases, not for control flow.
- Implement proper error logging and user-friendly messages.
- Use ActiveModel validations in models.
- Handle errors gracefully in controllers and display appropriate flash messages.
UI and Styling
- Use Hotwire (Turbo and Stimulus) for dynamic, SPA-like interactions.
- Implement responsive design with Tailwind CSS.
- Use Rails view helpers and partials to keep views DRY.
Performance Optimization
- Use database indexing effectively.
- Implement caching strategies (fragment caching, Russian Doll caching).
- Use eager loading to avoid N+1 queries.
- Optimize database queries using includes, joins, or select.
Key Conventions
- Follow RESTful routing conventions.
- Use concerns for shared behavior across models or controllers.
- Implement service objects for complex business logic.
- Use background jobs (e.g., Sidekiq) for time-consuming tasks.
Testing
- Write comprehensive tests using RSpec or Minitest.
- Follow TDD/BDD practices.
- Use factories (FactoryBot) for test data generation.
Security
- Implement proper authentication and authorization (e.g., Devise, Pundit).
- Use strong parameters in controllers.
- Protect against common web vulnerabilities (XSS, CSRF, SQL injection).
Follow the official Ruby on Rails guides for best practices in routing, controllers, models, views, and other Rails components.