Skip to content

Instantly share code, notes, and snippets.

@krainboltgreene
Created July 11, 2018 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krainboltgreene/7b42135ef2c97977ebea6446f6bf523e to your computer and use it in GitHub Desktop.
Save krainboltgreene/7b42135ef2c97977ebea6446f6bf523e to your computer and use it in GitHub Desktop.
module PaperTrail
class RecordTrail
def record_create
return unless enabled?
@in_after_callback = true
@record.class.paper_trail.version_class.after_transaction do
VersionJob.perform_async(@record.class.paper_trail.version_class, data_for_create)
end
ensure
@in_after_callback = false
end
def record_destroy(recording_order)
return unless enabled?
@in_after_callback = recording_order == "after"
unless @record.new_record?
@record.class.paper_trail.version_class.after_transaction do
VersionJob.perform_async(@record.class.paper_trail.version_class, data_for_destroy)
end
end
ensure
@in_after_callback = false
end
def record_update(force:, in_after_callback:, is_touch:)
return unless enabled?
@in_after_callback = in_after_callback
if force == true || changed_notably?
@record.class.paper_trail.version_class.after_transaction do
VersionJob.perform_async(@record.class.paper_trail.version_class, data_for_update(is_touch))
end
end
ensure
@in_after_callback = false
end
end
end
class VersionJob < ApplicationJob
sidekiq_options :queue => "versions"
def perform(version_class, attributes)
version_class.constantize.create!(attributes)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment