Skip to content

Instantly share code, notes, and snippets.

@owen2345
Last active September 7, 2016 19:19
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 owen2345/8267f6c3aa75e3aa1b8a2b0903d5422b to your computer and use it in GitHub Desktop.
Save owen2345/8267f6c3aa75e3aa1b8a2b0903d5422b to your computer and use it in GitHub Desktop.
(PaperTrail Extension) If you just want a summary of changes for a particular attribute of a model, you can use the history_from_audits_for method
# config/initializers/paper_trail.rb
PaperTrail::Model.module_eval do
# return an array of changes for specific attribute (old to newer)
# Sample use: Subscriber.find(2).history_from_audits_for(:first_name)
# Sample result: [{version: <PaperTrail::Version>, model: <Subscriber>}, {version: <PaperTrail::Version>, model: <MyModel>},....]
# Sample print: Subscriber.find(2).history_from_audits_for(:first_name).map{|item| item[:model].first_name }
# Required: Diffing Versions of paper trail
def history_from_audits_for(attr_name)
res = []
self.versions.each do |v|
vv = v.reify
next if vv.nil?
res << {version: v, model: vv} if v.changeset.include?(attr_name.to_s)
end
res
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment