I am using the paper_trail gem for versioning my models.
So far, my model depends on the info_for_paper_trail
method in ApplicationController
:
class ApplicationController < ActionController::Base
# Extra columns to store along with PaperTrail `versions`
def info_for_paper_trail
{ revision_id: @revision.id, revision_source_id: @revision_source.id }
end
end
This works great in context of the controller, but is there a way that I can replicate this sort of thing outside the context of the controller (e.g., a delayed job)?
I tried creating a virtual attribute called revision
and passing a proc
into has_paper_trail
, but it errors out with a method not found
exception:
# Attempt to solve this in the model
class Resource < ActiveRecord::Base
# Virtual attribute
attr_accessor :revision
# Attempt to use virtual attribute only if set from delayed job
has_paper_trail meta: proc { |resource| resource.revision.present? ? { revision_id: resource.revision.id, revision_source_id: revision.revision_source.id } : {} }
end
# Gist of what I'm trying to do in the delayed job
resource = Resource.new
resource.revision = Revision.new(user: user, revision_source: revision_source)
resource.save!
I assume based on this result that meta
cannot take a proc
, and plus I don't like how this solution smells anyway...