重构模型回调与业务副作用
梳理模型回调顺序和全部保存入口,将跨业务副作用迁入显式操作,并验证事务及行为保持一致。
完整提示词
<context>
You are a Rails engineer. Callbacks are fine for keeping a record's own data consistent (normalising an email in `before_validation`, setting a slug). They become a problem when they do work about other things: sending emails, calling payment or CRM APIs, creating records in other tables, enqueuing jobs. Then every `save` anywhere (an admin edit, a data fix in the console, a seed, a test factory) triggers those side effects, tests need stubs everywhere, and the order of `before_save`, `after_save` and `after_commit` across concerns decides behaviour nobody can see in one place.
Ordering and transaction details that a refactor must preserve or deliberately change: `before_*` and `after_save` run inside the transaction (an exception rolls the save back; an email sent there goes out even if the transaction later rolls back); `after_commit` runs after commit, outside it; callbacks run in declaration order, including those added by concerns and gems; `update_column`, `update_all`, `insert_all` and `delete` skip callbacks, so some paths already bypass them; `saved_change_to_attribute?` only works in after callbacks; and a `throw :abort` in a before callback halts the save.
The target: data-consistency callbacks stay on the model; cross-cutting side effects move to an explicit operation (a service object or a model method like `Order#place!`) that the save paths call, wrapping the transaction and enqueuing jobs after commit. Paths that should not trigger the side effects (imports, admin fixes) simply do not call the operation, and that difference is listed explicitly.
</context>
<task>
Untangle the callbacks on {{model}}.
1. Map every callback, including those from concerns and gems, in execution order: hook, method, what it does, whether it touches other models or external systems, and whether it runs inside or after the transaction.
2. Find every save path for the model and which callbacks each one triggers today, including paths that skip callbacks.
3. Propose the target shape: which callbacks stay (own data only), which side effects move into which explicit operation, the transaction boundary, and when jobs and emails are enqueued. For each save path, say whether it calls the operation, and list paths whose behaviour would change. Wait for approval.
4. Pin today's behaviour with tests per save path (records created, jobs enqueued, emails delivered, external calls), then move callbacks one at a time, updating the save paths, running the tests{{#test_command}} with `{{test_command}}`{{/test_command}} after each.
5. Report the diff, the tests, and any path where behaviour changed by agreement.
</task>
<constraints>
- Same side effects, order and transaction semantics on every save path, except agreed changes listed per path.
- Keep callbacks that only maintain the record's own data.
- Do not send emails or call external services inside a transaction that can still roll back.
{{> guardrails/scope-discipline}}
{{> guardrails/verify-before-done}}
</constraints>
<output_format>
## Callback map
Table: order, hook, method, effect, inside or after transaction.
## Save paths
Table: path, callbacks triggered today, behaviour after.
## Target shape
Operations, remaining callbacks and transaction boundary.
## Diff
The change.
## Check
Tests per save path and results.
</output_format>可替换内容
- {{model}}
- {{#test_command}}
- {{test_command}}
- {{/test_command}}
- {{> guardrails/scope-discipline}}
- {{> guardrails/verify-before-done}}
分类与标签
编程开发、模型回调、业务重构、事务边界、副作用测试