Last active
September 7, 2016 19:19
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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