重载和绕过 Active Record 更新

发布于 2024-08-19 21:35:28 字数 736 浏览 4 评论 0原文

我正在尝试在我的应用程序中实现审计跟踪,但由于某些要求,我无法使用任何现有的 gem 或插件。

我想将任何更新模型的正常尝试转移到自定义方法,该方法将所有更新保存在另一个单独的表(称为更新)中。

然后应用程序使用更新表来实际执行更新。

现在我已经重载了 create_or_update 来获取功能的第一部分

def create_or_update
  raise ReadOnlyRecord if readonly?
  result = new_record? ? create : create_updates
  result != false
end


class Update < ActiveRecord::Base
  belongs_to :updatable, :polymorphic => true

  after_create :update_model

  private

  def update_model
    self.updatable.update_attribute self.attribute, self.new_value #infinite loop
  end
end  

现在的问题是,当更新模型尝试实际执行更新时,这会导致无限循环。

我一直在浏览 Rails 核心源代码,以找到绕过第一个功能的最佳位置。我希望这些更新在事务内部执行,但我不确定活动记录堆栈中的具体开始或结束位置。我也不想开始破坏活动资源。

任何建议将不胜感激。

I am trying to implement an audit trail into my application, but because of some requirements I am unable to use any existing gems or plugins.

I would like to divert any normal attempt to update a model to a custom method that saves all the updates in another separate Table (called Updates).

The application then uses the update table to actually perform the update.

Right now I have overloaded create_or_update to get the first part of the functionality

def create_or_update
  raise ReadOnlyRecord if readonly?
  result = new_record? ? create : create_updates
  result != false
end


class Update < ActiveRecord::Base
  belongs_to :updatable, :polymorphic => true

  after_create :update_model

  private

  def update_model
    self.updatable.update_attribute self.attribute, self.new_value #infinite loop
  end
end  

The issue now is that this causes an infinite loop when the Update Model tries to actually perform the update.

I have been looking through the rails core source to find the best place to bypass the functionality of the first feature. I would like these updates to be performed inside of the transaction but I am not sure where exactly that begins or ends in the active record stack. I also do not want to start hacking away at active resource.

Any suggestions would be greatly appreciated.

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

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

发布评论

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

评论(1

奈何桥上唱咆哮 2024-08-26 21:35:28

您是否真的需要将属性保存在单独的表中,然后在管理员查看并批准后执行更新?如果是这种情况,您可能只想覆盖更新方法来执行如下操作:

def update(perform_updates = false)
  if perform_updates
    latest_approved_update = UpdateAuditor.first(:conditions => { :updatable_id => self.id, :updatable_type => self.class.name, :approved => true })
    self.attributes = latest_approved_update.attributes
    self.save
  else 
    UpdateAuditor.create(:updatable_id => self.id, :updatable_type => self.class.name, :attributes => self.attributes)
  end
end

更新: 作者评论说他们希望能够将此模型应用于所有< /em> 更新。为了实现这一点,您可以向模型添加一个 attr_accessor ,比如说“perform_updates”,默认情况下它当然是 nil 。

当您想要对数据库执行更新时,您首先必须将该属性设置为 true,然后运行更新。否则,更新只会创建一个新的 UpdateAuditor 记录,该记录需要得到管理员的批准。

class Person < ActiveRecord::Base
  has_many :audits, :class_name => "UpdateAudit", :as => :auditable

  attr_accessor :perform_updates

  private

  def create_or_update
    raise ReadOnlyRecord if readonly?

    if new_record?
      result = create
      result != false
    else
      if perform_updates
        latest_approved_update = audits.approved.last

        if latest_approved_update
          self.attributes = latest_approved_update.attributes
          update
        else
          return false
        end
      else
        audits.create(:updated_attributes => self.attributes)
      end 
    end
  end
end

根据记录,我认为覆盖默认更新方法是一个危险的游戏,这种编程最好在它所属的 before_update 回调中进行。一旦某个界面中的更新获得批准,观察者就可以执行更新,覆盖当前存在的内容,直到所做的另一个更改得到批准。如果队列中当前有待批准的对象更新,则可以提醒用户更改正在等待批准等。

Do you actually need to save the attributes in a separate table, and then perform the update after an administrator views and approves them? If this is the scenario, you may just want to overwrite the update method to do something like this:

def update(perform_updates = false)
  if perform_updates
    latest_approved_update = UpdateAuditor.first(:conditions => { :updatable_id => self.id, :updatable_type => self.class.name, :approved => true })
    self.attributes = latest_approved_update.attributes
    self.save
  else 
    UpdateAuditor.create(:updatable_id => self.id, :updatable_type => self.class.name, :attributes => self.attributes)
  end
end

UPDATE: The author has commented that they want to be able to apply this model to all updates. In order to accomplish this, you can add an attr_accessor to the model, let's say something like "perform_updates", which will of course be nil by default.

When you want to perform the update to the database, you will first have to set the attribute to true, then run update. Otherwise, the update will just create a new UpdateAuditor record which will need to be approved by an administrator.

class Person < ActiveRecord::Base
  has_many :audits, :class_name => "UpdateAudit", :as => :auditable

  attr_accessor :perform_updates

  private

  def create_or_update
    raise ReadOnlyRecord if readonly?

    if new_record?
      result = create
      result != false
    else
      if perform_updates
        latest_approved_update = audits.approved.last

        if latest_approved_update
          self.attributes = latest_approved_update.attributes
          update
        else
          return false
        end
      else
        audits.create(:updated_attributes => self.attributes)
      end 
    end
  end
end

For the record, I think that overwriting the default update methods is a dangerous game, and such programming is better off in a before_update callback where it belongs. Once an update is approved in some interface, then an observer can then perform the update, overwriting what is currently there, until another change which was made can be approved. If there are currently updates to an object in the queue to be approved, users can be alerted that changes are pending approval, etc.

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