Paper_Trail: Show Diff Between Versions

135 views
Skip to first unread message

Charles Denison

unread,
May 30, 2015, 11:57:51 PM5/30/15
to rubyonra...@googlegroups.com
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
"+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 versions.

Really appreciate the help.

--
Posted via http://www.ruby-forum.com/.

Walter Lee Davis

unread,
May 31, 2015, 10:36:17 AM5/31/15
to rubyonra...@googlegroups.com
Paper Trail saves a diff, but not at the level you're thinking of. Instead of calculating the new value from the old plus the diff, it saves the attributes that are different from one object to the next. For example, if you had Widget.create( size: 'large', color: 'blue') and you changed it to color: 'green' later, the object that Paper Trail saved to record this change would consist of only the color: 'blue' part, the updated_at from the initial creation of the widget, and a foreign key back to the original @widget. The updated @widget record would now consist of size: 'large', color: 'green', and a new updated_at. When you navigate back to the original version, Paper Trail first loads the current value, then overloads any attribute key/value pairs stored in its version record to restore back to the desired snapshot of the object.

You'll have to calculate the difference (through addition or subtraction) yourself. If you think about it, this is only natural, since for Paper Trail to save a true diff at the level you're thinking of, it would have to understand every possible attribute type and value scheme. Much of what people use this for is non-numeric data.

Walter

>
> Really appreciate the help.
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b3e22e5b73b4bc08835d4895f438d060%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages