Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paper_Trail: Show Diff Between Versions

I'm new to Rails...using RubyMine as an IDE.

I have Paper_Trail saving previous versions of the data "xoi_qb". My view is currently showing the current and previous data as I'd like, but I would like to show the diff between the current version "xoi_qb" and the previous version "xoi_qb". For instance, the current version may be "97" and the previous version may be "94", and I would like to display "XOI +/-: +3". I would like to display this difference and add the "+" or "-" based on the positive or negative change.

In my model, 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 the 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.

like image 360
cwayne2 Avatar asked May 27 '15 04:05

cwayne2


1 Answers

Take a look at the documentation on diffing versions with PaperTrail. In particular, here's what you want to take note of:

If you add an object_changes text column to your versions table, either at installation time with the rails generate paper_trail:install --with-changes option or manually, PaperTrail will store the changes diff (excluding any attributes PaperTrail is ignoring) in each update version.

With this behavior enabled, it is reasonably simple to get object.versions.map{|v| [v.created_at, v.changeset]} and then iterate over that structure to render your change log.

like image 196
Maysam Torabi Avatar answered Sep 23 '22 16:09

Maysam Torabi