Paper_Trail: Show Diff Between Versions
Asked Answered
I

2

6

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.

Isaacson answered 27/5, 2015 at 4:27 Comment(1)
Do you want to display the difference between last version and current version record?Charlesettacharleston
P
8

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.

Pneumonoultramicroscopicsilicovolcanoconiosis answered 16/10, 2015 at 11:5 Comment(0)
C
-1

I am assuming you have installed paper_trail gem. You can add an object_changes text column to your versions table either manually or through 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 the records.

Check the difference between last version and current version

@quarterback.versions.last.changeset

You can get the difference.

Source: https://github.com/airblade/paper_trail - Diffing Versions

Charlesettacharleston answered 27/5, 2015 at 6:8 Comment(4)
This doesn't seem to be working. Yes, definitely installed paper_gem...the versioning is working for me. I restarted the server. What do you mean by "crate"? Does the @quarterback.versions.last.changeset just go in the html file? For instance: '<tr> <th>XOI +/-</th> <td><%= quarterback.versions.last.changeset %></td> </tr>'Isaacson
Im assuming quarterback is your table name.. So after restart the server, create one more record.(i.e) Ex: Quarterback.create(name: 'test') depends on your table. Then try, Quarterback.last.versions.last.changesetCharlesettacharleston
I'm sorry, this still isn't working. I am very new to rails. Is it possible that it's because in my "versions" model (created for paper_trail) already has "change" defined as: def change create_table :versions do |t| t.string :item_type, :null => false t.integer :item_id, :null => false t.string :event, :null => false t.string :whodunnit t.text :object t.datetime :created_at t.datetime :updated_at end add_index :versions, [:item_type, :item_id] endIsaacson
Also, I'm using RubyMine as an IDE, which I find very helpful.Isaacson

© 2022 - 2024 — McMap. All rights reserved.