2009-12-21 68 views
0

我正在编写一个为模型提供草稿的插件。删除操作是一项可设计的操作,我并不总是希望在删除发布前删除原点。所以我写了自己的破坏方法来帮助解决这个问题。一切正常,除了我想要的东西除外,:before_destroy:after_destroy的自定义回调不再被触发。我的自定义销毁方法不会在销毁回调之前和之后触发默认值

如何任何想法:

  1. 重新绑定回调到我的破坏方法
  2. 工作一些alias_method_chain巫术
  3. 得到模型的列表回调,所以我可以给他们打电话手动
  4. 解决这个问题另一种方式

这里是我破坏方法:

def destroy 
    if self.attribute_names.include?('draft') && self.skip_draft == false 
     if handle_destroy # if true is returned 
     super # go ahead and destroy as normal 
     end 
    else 
     super 
    end 
    end 

更新:我刚刚发现这个: correct way to override activerecordbasedestroy,但似乎所提出的技术不适应回调。有没有办法让我的蛋糕也吃了呢?

回答

0

我错了,当超级被调用时没有被调用的回调。我最终依赖于最初发布的确切代码。我改变了我的handle_destroy方法返回的方式

我会告诉你我是怎么想出如何在想要显式触发回调的情况下触发回调。

def destroy 
    if self.attribute_names.include?('draft') && self.skip_draft == false 
     if handle_destroy # if true is returned 
     super # go ahead and destroy as normal 
     else 
     # Execute all custom callbacks that are not dependent type callbacks (ie: if the callback method name contains "dependent") 
     # Dependent callbacks delete records and this is not what the drafting system is all about. 
     (self.class.before_destroy_callback_chain + self.class.after_destroy_callback_chain).each do |cb| 
      unless (cb.method.kind_of?(Symbol) && cb.method.to_s.match(/dependent/)) 
      cb.call(self) 
      end 
     end 
     end 
    else 
     # normal delete 
     super 
    end 
    end