SuperAPI AI 工具集

aasm-coder

编程与开发

查看项目

更新于 10-15 21:09

技能介绍

用途:在 Rails 应用中用 AASM 实现状态机,管理工作流状态流转。

主要能力:通过 Gemfile 引入 gem "aasm", "~> 5.5";在模型中 include AASM 并用 aasm column: :status 定义状态(如 pending、paid、processing、shipped、cancelled)与事件(pay、process、ship、cancel)。支持初始状态 initial: true、多来源转换 from: [:pending, :paid]、守卫 guard、事件与状态回调(before、after、before_enter、after_enter、before_exit、after_exit),以及自动生成的状态作用域(如 Order.pending)。

使用方式:调用 order.pending?、order.may_pay? 检查状态与可执行性;order.pay! 执行转换并触发回调,失败抛 AASM::InvalidTransition;order.pay 失败返回 false。多个 transitions 时首个匹配的守卫生效。

测试:用 RSpec 断言初始状态、状态变化与邮件等副作用,并覆盖非法转换抛错的情况。

限制:资料仅覆盖上述内容,多状态机、持久化选项与历史追踪需参考 references/aasm-patterns.md,具体细节暂未提供。