AgentSkillsCN

rails-refactor

在不改变行为的前提下,对Rails代码进行重构,以提升代码的清晰度与风格。当您需要简化控制器、模型、查询或规格;提取服务;或应用Ruby/Rails/RSpec的风格规则时,可使用此功能。

SKILL.md
--- frontmatter
name: rails-refactor
description: Refactors Rails code for clarity and style without changing behavior. Use when simplifying controllers, models, queries, or specs; extracting services; or applying Ruby/Rails/RSpec style rules.

Rails Refactor

When to Use

  • Simplifying controllers (moving logic to models or services).
  • Replacing deprecated or verbose patterns (e.g. old validations, raw SQL, N+1).
  • Applying Ruby/Rails/RSpec style (see project rules in .cursor/rules/ or CLAUDE.md).
  • Extracting service objects or form objects.

Principles

  1. Behavior first: Refactors must not change observable behavior. Rely on existing tests; add or run specs if coverage is missing.
  2. Incremental: Prefer small, reviewable steps (e.g. rename, extract method, then move).
  3. Conventions: After refactor, code should align with the project's Ruby/Rails/RSpec style rules (.cursor/rules/ or CLAUDE.md).

Verification

  • Run the relevant test suite: bundle exec rspec or bin/rails test.
  • Run RuboCop if the project uses it: bundle exec rubocop [path].
  • Confirm no new warnings or failures were introduced.