铁轨触发嵌套协会自动架上的AASM事件

发布于 2025-01-26 16:28:22 字数 1155 浏览 2 评论 0原文

我有一个父母和子模型模型定义,每个架构都有自己的AASM状态计算机

class Project < ApplicationRecord
  has_many :tasks, autosave: true
  include AASM

  aasm do
    state :created, initial: true
    state :in_progress
    state :completed

    event :start do
      transitions from: :created, to: :in_progress
    end
 end
end
class Task < ApplicationRecord
  belongs_to :project

  include AASM

  aasm do
    state :open, initial: true
    state :in_progress
    state :failed
    state :reopened
    state :closed, after: :notify_task_closed

    event :close do
      transitions from: :in_progress, to: :closed
    end
  end
end

,现在我有了一个页面,用户可以在该页面上编辑该项目,并在任务上批量编辑/perform-aasm-actions。现在,当用户提交此类请求时,我会收到已编辑的字段和要在任务上触发的操作(事件)。

由于autosave是为任务 project 类启用的,我想执行一个保存以

  • 更新项目属性更新项目
  • 属性更新任务属性
  • 触发任务的各自事件。

捕获是在上述每个点运行验证,并在任何步骤失败的情况下还原整个交易。

我遇到以下问题,

  1. 在自动赛期间实现这一目标的正确模式是什么?
  2. 何时以及如何触发Autosave期间的AASM事件?
  3. 如果有任何验证/后卫失败,如何起泡错误?

I have a parent and child model schema defined with each having its own AASM state machine

class Project < ApplicationRecord
  has_many :tasks, autosave: true
  include AASM

  aasm do
    state :created, initial: true
    state :in_progress
    state :completed

    event :start do
      transitions from: :created, to: :in_progress
    end
 end
end
class Task < ApplicationRecord
  belongs_to :project

  include AASM

  aasm do
    state :open, initial: true
    state :in_progress
    state :failed
    state :reopened
    state :closed, after: :notify_task_closed

    event :close do
      transitions from: :in_progress, to: :closed
    end
  end
end

Now I have a page where users can edit the project and also edit/perform-aasm-actions in bulk on the tasks. Now when a user submits such request, I receive the edited fields and the actions(events) to be triggered on tasks.

Since autosave is enabled for the tasks association for the Project class, I would like to perform a single save to

  • Update project attributes
  • Update task attributes
  • Trigger respective events on the tasks.

The catch is to run validations for each of the above points and revert the whole transaction in case any step fails.

I am stuck on the following questions

  1. What would be the right pattern to achieve this during autosave?
  2. When and how to trigger AASM events during autosave?
  3. How to bubble up errors if there are any validations/guard failures?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文