PaperTrail: info_for_paper_trail outside the context of a controller

佐手、 提交于 2019-12-04 10:53:42

You need to set these values in your code if you're operating outside of the controller:

::PaperTrail.controller_info = { revision_id: revision.id, revision_source_id: revision_source.id }
::PaperTrail.whodunnit = user.id

The model will then pick the extra values up just like it would normally from the controller.

I derived this info from looking at the PaperTrail::Controller module. Particularly, look at the set_paper_trail_controller_info and set_paper_trail_whodunnit methods that get run as before filters.

I think you can just do:

class Resource < ActiveRecord::Base
  # Virtual attribute
  attr_accessor :revision, :revision_source

  # Attempt to use virtual attribute only if set from delayed job
  has_paper_trail meta: { 
    revision_id: :get_revision_id, 
    revision_source_id: get_revision_source.id 
  }

  def get_revision_id
    resource.revision.try(:id)
  end

  def get_revision_source_id
    resource.revision_source.try(:id)
  end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!