用事件挂钩观察者

发布于 2024-08-21 07:35:39 字数 702 浏览 5 评论 0原文

我们在很多模型中使用 AASM,但我们正在考虑稍微简化模型。我们想做的一件事是将所有通知内容从模型中移出并放入观察者中。

所以考虑一下:

class ClarificationRequest < ActiveRecord::Base
  include AASM

  aasm_initial_state :open

  # States
  aasm_state :open
  aasm_state :closed

  # Events
  aasm_event :close, :after => :notify_closed do transitions :to => :closed, :from => [:open,:replied], :guard => :can_close? end
end

我已经尝试过这个,但没有运气:

class ClarificationRequestObserver < ActiveRecord::Observer
  observe :clarification_request

  def after_close
    puts '############### message from observer!!!!!'
  end
end

如何将 :notify_close 移动到观察者?

谢谢!

.卡里姆

We are using AASM in quite a few of our models, but we're looking at simplifying a bit the models. One of the things we'd like to do is to move all the Notification stuff out of the models and into Observers.

So considering:

class ClarificationRequest < ActiveRecord::Base
  include AASM

  aasm_initial_state :open

  # States
  aasm_state :open
  aasm_state :closed

  # Events
  aasm_event :close, :after => :notify_closed do transitions :to => :closed, :from => [:open,:replied], :guard => :can_close? end
end

I've tried this, but with no luck:

class ClarificationRequestObserver < ActiveRecord::Observer
  observe :clarification_request

  def after_close
    puts '############### message from observer!!!!!'
  end
end

How can I move the :notify_closed to an Observer?

Thx!

.Karim

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冬天的雪花 2024-08-28 07:35:39

我之前在github上回复过你的评论,我在这里重复一下,以防万一

class ClarificationRequest < ActiveRecord::Base
    include AASM

    aasm_initial_state :open

    # States
    aasm_state :open
    aasm_state :closed

    # Events
    aasm_event :close, :after => :notify_closed do transitions :to => :closed, :from => [:open,:replied], :guard => :can_close? end

    # Notify Observer
    def notify_closed
     notify :close # this will trigger after_close method call in ClarificationRequestObserver
     # notify :closed # this will trigger after_closed method call in ClarificationRequestObserver
     # notify :whatever # this will trigger after_whatever method call in ClarificationRequestObserver
    end
end

I've reply to your comment on github before, I'll repeat it here, just in case

class ClarificationRequest < ActiveRecord::Base
    include AASM

    aasm_initial_state :open

    # States
    aasm_state :open
    aasm_state :closed

    # Events
    aasm_event :close, :after => :notify_closed do transitions :to => :closed, :from => [:open,:replied], :guard => :can_close? end

    # Notify Observer
    def notify_closed
     notify :close # this will trigger after_close method call in ClarificationRequestObserver
     # notify :closed # this will trigger after_closed method call in ClarificationRequestObserver
     # notify :whatever # this will trigger after_whatever method call in ClarificationRequestObserver
    end
end
暗地喜欢 2024-08-28 07:35:39

说实话,我觉得你过得很好。对于这样的事情使用 AASM 钩子是有意义的。这样您就知道它已转换正常,然后您发送通知。

您可以查看在 before_update 中使用活动记录 dirty 来检查 state_ 是否已打开且现在已关闭。

To be honest I think how you had it is fine. It makes sense to use the AASM hooks for stuff like this. This way you know it's transitioned OK and then you send the notification.

You could look at using active record dirty in a before_update to check if the state_was open and now closed.

像极了他 2024-08-28 07:35:39

我会做这样的事情:

class ClarificationRequest < ActiveRecord::Base
  include AASM

  aasm_initial_state :open

  # States
  aasm_state :open
  aasm_state :closed, :enter => :do_close

  # Events
  aasm_event :close do transitions :to => :closed, :from => [:open,:replied], :guard => :can_close? end

  def recently_closed?
    @recently_closed
  end 
protected
  def do_close
    @recently_closed = true
  end

end


class ClarificationRequestObserver < ActiveRecord::Observer
  observe :clarification_request

  def after_save(clarification_request)
    puts '############### message from observer!!!!!' if clarification_request.recently_closed?
  end
end

您还应该将观察者包含在 config/environment.rb 的 config.active_record.observers 列表中

原因是观察者应该观察一个对象。通过主动通知模型中的观察者(并与之交互),您假设有一个可用的观察者,但我不认为您可以安全地做到这一点(看看观察者通常在现实世界中的行为方式)。观察者是否对事件感兴趣应该由观察者决定。

I would do something like this:

class ClarificationRequest < ActiveRecord::Base
  include AASM

  aasm_initial_state :open

  # States
  aasm_state :open
  aasm_state :closed, :enter => :do_close

  # Events
  aasm_event :close do transitions :to => :closed, :from => [:open,:replied], :guard => :can_close? end

  def recently_closed?
    @recently_closed
  end 
protected
  def do_close
    @recently_closed = true
  end

end


class ClarificationRequestObserver < ActiveRecord::Observer
  observe :clarification_request

  def after_save(clarification_request)
    puts '############### message from observer!!!!!' if clarification_request.recently_closed?
  end
end

You should also include the observer in the config.active_record.observers list in config/environment.rb

The reason for that is that an observer should observe an object. By actively notifying (and interacting with) the observer from the model you assume that there is one available, which I don't believe that you safely can do (seeing how observers usually behave in the real world). It should be up to the observer whether it is interested of the event or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文