Paper_Trail: show difference between versions
I am new to Rails ... using RubyMine as IDE.
I have a Paper_Trail keeping previous versions of "xoi_qb" data. Currently my view is showing current and previous data as I would like, but I would like to show the difference between the current version of "xoi_qb" and the previous version of "xoi_qb". For example, the current version might be "97" and the previous version might be "94" and I would like to display "XOI +/-: +3". I would like to show this difference and add "+" or "-" based on positive or negative change.
In my model, the Paper Trail is set to create versions like this:
def get_xoi_qb
xoi_qb = []
self.versions.each do |version|
unless version.reify.nil?
xoi_qb << version.reify.xoi_qb
end
end
return xoi_qb
end
And in my HTML set to display versions like this:
<th>Previous XOI</th>
<table>
<% @quarterback.versions.each do |version| %>
<tr>
<td><%= version.reify.xoi_qb %> dated <%= version.created_at %></td>
</tr>
<% end %>
Not sure how to show the difference between the two.
Really appreciate the help.
source to share
Check out the documentation for different versions from PaperTrail . Specifically, here's where you want to take note:
If you add a text column object_changes to the version table , either during installation using rails, create paper_trail: install --with-changes, or manually, PaperTrail will preserve diff changes (excluding any attributes that PaperTrail ignores) in each update version.
If this behavior is enabled, it is reasonable to just get object.versions.map {| v | [v.created_at, v.changeset]} and then navigate this structure to display the changelog.
source to share
I am assuming you have installed the paper_trail gem. You can add the text column object_changes to the version table manually or via migration.
rails g migration AddColumnToVersions
class AddColumnToVersions < ActiveRecord::Migration
def change
execute "ALTER TABLE versions ADD object_changes TEXT"
end
end
Then restart the server. Create records.
Check the difference between the latest version and the current version
@quarterback.versions.last.changeset
You can get the difference.
Source: https://github.com/airblade/paper_trail - Diffing Versions
source to share