-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from uclibs/9/paper-trail
paper trail
- Loading branch information
Showing
30 changed files
with
775 additions
and
5 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,7 @@ | |
|
||
# Don't track .DS_Store on MacOS | ||
.DS_Store | ||
|
||
.env.production.local | ||
.env.test.local | ||
.env.development.local |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Place all the behaviors and hooks related to the matching controller here. | ||
# All this logic will automatically be available in application.js. | ||
# You can use CoffeeScript in this file: http://coffeescript.org/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Place all the styles related to the activity controller here. | ||
// They will automatically be included in application.css. | ||
// You can use Sass (SCSS) here: http://sass-lang.com/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
class ActivityController < ApplicationController | ||
def index | ||
@pagy, @versions = pagy(PaperTrail::Version.all, items: 50) | ||
end | ||
|
||
def show | ||
@version = PaperTrail::Version.find(params[:id]) | ||
@other_versions = PaperTrail::Version.where(item_id: @version.item_id, item_type: @version.item_type).order('created_at DESC') | ||
end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActivityHelper | ||
def version_summarizer(version) | ||
display_name = 'Someone' | ||
display_name = User.find(version.whodunnit).display_name unless version.whodunnit.nil? | ||
"#{display_name} #{event_to_summary(version.event)} the #{item_type_to_summary(version.item_type)}: #{name_to_summary(version)}" | ||
end | ||
|
||
def event_to_summary(event) | ||
case event | ||
when 'create' | ||
'created' | ||
when 'update' | ||
'updated' | ||
when 'destroy' | ||
'deleted' | ||
else | ||
'did something to' | ||
end | ||
end | ||
|
||
def item_type_to_summary(item_type) | ||
case item_type | ||
when 'ConservationRecord' | ||
'conservation record' | ||
when 'TreatmentReport' | ||
'treatment report' | ||
when 'ExternalRepairRecord' | ||
'external repair record' | ||
when 'InHouseRepairRecord' | ||
'in house repair record' | ||
when 'User' | ||
'user' | ||
when 'CostReturnReport' | ||
'cost return report' | ||
else | ||
'some item' | ||
end | ||
end | ||
|
||
def name_to_summary(version) | ||
case version.item_type | ||
when 'ConservationRecord' | ||
if ConservationRecord.where(id: version.item_id).exists? | ||
link_to ConservationRecord.find(version.item_id).title, conservation_record_path(version.item_id) | ||
else | ||
version.object.nil? ? ' ' : version.object.try(:split, 'title: ').last.try(:split, 'author: ').first.chomp | ||
end | ||
when 'TreatmentReport' | ||
if TreatmentReport.where(id: version.item_id).exists? | ||
treatment_report_id = TreatmentReport.find(version.item_id).conservation_record.id | ||
link_to ConservationRecord.find(treatment_report_id).title.to_s, conservation_record_path(treatment_report_id) | ||
else | ||
# version.object.split('title: ').last.split('author: ').first.chomp | ||
'Record has been deleted' | ||
end | ||
when 'User' | ||
User.find(version.item_id).display_name | ||
when 'InHouseRepairRecord' | ||
if InHouseRepairRecord.where(id: version.item_id).exists? | ||
in_house_repair_id = InHouseRepairRecord.find(version.item_id).conservation_record.id | ||
link_to InHouseRepairRecord.find(version.item_id).conservation_record.title, conservation_record_path(in_house_repair_id) | ||
else | ||
'Record has been deleted' | ||
end | ||
when 'ExternalRepairRecord' | ||
if ExternalRepairRecord.where(id: version.item_id).exists? | ||
external_repair_id = ExternalRepairRecord.find(version.item_id).conservation_record.id | ||
link_to ExternalRepairRecord.find(version.item_id).conservation_record.title, conservation_record_path(external_repair_id) | ||
else | ||
'Record has been deleted' | ||
end | ||
when 'CostReturnReport' | ||
if CostReturnReport.where(id: version.item_id).exists? | ||
cost_return_report_id = CostReturnReport.find(version.item_id).conservation_record.id | ||
link_to ConservationRecord.find(cost_return_report_id).title.to_s, conservation_record_path(cost_return_report_id) | ||
else | ||
# CostReturnReport.find(version.item_id).conservation_record.title | ||
'Record has been deleted' | ||
end | ||
end | ||
end | ||
|
||
def changeset_format(field, value); end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
|
||
class CostReturnReport < ApplicationRecord | ||
belongs_to :conservation_record | ||
has_paper_trail | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,6 @@ | |
|
||
class ExternalRepairRecord < ApplicationRecord | ||
belongs_to :conservation_record | ||
|
||
has_paper_trail | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,6 @@ | |
|
||
class InHouseRepairRecord < ApplicationRecord | ||
belongs_to :conservation_record | ||
|
||
has_paper_trail | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,6 @@ | |
|
||
class TreatmentReport < ApplicationRecord | ||
belongs_to :conservation_record | ||
|
||
has_paper_trail | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,6 @@ def active_for_authentication? | |
|
||
super && account_active? | ||
end | ||
|
||
has_paper_trail | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<h1>Recent Activity</h1> | ||
|
||
<table class="table"> | ||
<thead> | ||
<th>Activity</th> | ||
<th>When</th> | ||
<th>Detail</th> | ||
</thead> | ||
|
||
<tbody> | ||
<% @versions.order('created_at DESC').each do |version| %> | ||
<tr> | ||
<td><%= version_summarizer(version).html_safe %></td> | ||
<td><%= version.created_at.in_time_zone("America/New_York").strftime("%-m/%-d/%y: %H:%M %Z") %> | ||
<td><%= link_to "Details", activity_path(version) %></td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
<div class="float-right"> <%== pagy_bootstrap_nav(@pagy).html_safe %></div> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<h2><%= version_summarizer(@version).html_safe %></h2> | ||
|
||
<% if @version.event == 'create' %> | ||
<center> | ||
<div class="card" style="width: 18rem;"> | ||
<div class="card-body"> | ||
<h5 class="card-title">No Diff Information to Show</h5> | ||
<h6 class="card-subtitle mb-2 text-muted"></h6> | ||
<p class="card-text">This version was logged right after it was created, so there is nothing to compare against.</p> | ||
</div> | ||
</div> | ||
</center> | ||
<% else %> | ||
<table class="table"> | ||
<thead> | ||
<th>Field</th> | ||
<th>Was</th> | ||
<th>Is</th> | ||
</thead> | ||
|
||
<tbody> | ||
<% @version.changeset.each do |change| %> | ||
<tr> | ||
<td><%= change[0] %></td> | ||
<td><%= change[1][0] %></td> | ||
<td><%= change[1][1] %></td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
<% end %> | ||
|
||
|
||
<h3>Other Activity on this Record</h3> | ||
|
||
<table class="table"> | ||
<thead> | ||
<th>Action</th> | ||
<th>When</th> | ||
<th>Detail</th> | ||
</thead> | ||
|
||
<tbody> | ||
<% @other_versions.all.each do |version| %> | ||
<tr> | ||
<td><%= version_summarizer(version).html_safe %></td> | ||
<td><%= version.created_at.in_time_zone("America/New_York").strftime("%-m/%-d/%y: %H:%M %Z") %> | ||
<td><%= link_to "Details", activity_path(version) %></td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# This migration creates the `versions` table, the only schema PT requires. | ||
# All other migrations PT provides are optional. | ||
class CreateVersions < ActiveRecord::Migration[5.2] | ||
|
||
# The largest text column available in all supported RDBMS is | ||
# 1024^3 - 1 bytes, roughly one gibibyte. We specify a size | ||
# so that MySQL will use `longtext` instead of `text`. Otherwise, | ||
# when serializing very large objects, `text` might not be big enough. | ||
TEXT_BYTES = 1_073_741_823 | ||
|
||
def change | ||
create_table :versions do |t| | ||
t.string :item_type, {:null=>false} | ||
t.integer :item_id, null: false, limit: 8 | ||
t.string :event, null: false | ||
t.string :whodunnit | ||
t.text :object, limit: TEXT_BYTES | ||
|
||
# Known issue in MySQL: fractional second precision | ||
# ------------------------------------------------- | ||
# | ||
# MySQL timestamp columns do not support fractional seconds unless | ||
# defined with "fractional seconds precision". MySQL users should manually | ||
# add fractional seconds precision to this migration, specifically, to | ||
# the `created_at` column. | ||
# (https://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html) | ||
# | ||
# MySQL users should also upgrade to at least rails 4.2, which is the first | ||
# version of ActiveRecord with support for fractional seconds in MySQL. | ||
# (https://github.com/rails/rails/pull/14359) | ||
# | ||
t.datetime :created_at | ||
end | ||
add_index :versions, %i(item_type item_id) | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
db/migrate/20200131192546_add_object_changes_to_versions.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# This migration adds the optional `object_changes` column, in which PaperTrail | ||
# will store the `changes` diff for each update event. See the readme for | ||
# details. | ||
class AddObjectChangesToVersions < ActiveRecord::Migration[5.2] | ||
# The largest text column available in all supported RDBMS. | ||
# See `create_versions.rb` for details. | ||
TEXT_BYTES = 1_073_741_823 | ||
|
||
def change | ||
add_column :versions, :object_changes, :text, limit: TEXT_BYTES | ||
end | ||
end |
Oops, something went wrong.