Skip to content

Commit

Permalink
Merge pull request #118 from uclibs/9/paper-trail
Browse files Browse the repository at this point in the history
paper trail
  • Loading branch information
hortongn authored Jan 26, 2021
2 parents ac027c6 + 91404da commit 348efcf
Show file tree
Hide file tree
Showing 30 changed files with 775 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@

# Don't track .DS_Store on MacOS
.DS_Store

.env.production.local
.env.test.local
.env.development.local
13 changes: 12 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Metrics/AbcSize:
Exclude:
- app/helpers/in_house_repair_records_helper.rb
- app/helpers/external_repair_records_helper.rb
- app/helpers/activity_helper.rb

Metrics/BlockLength:
Exclude:
Expand All @@ -28,12 +29,22 @@ Metrics/BlockLength:
Metrics/LineLength:
Exclude:
- "app/views/conservation_records/_conservation_record.json.jbuilder"

- "spec/helpers/activity_helper_spec.rb"

Metrics/MethodLength:
Exclude:
- "app/controllers/search_controller.rb"
- "app/controllers/treatment_reports_controller.rb"
- "app/models/ability.rb"
- "app/helpers/activity_helper.rb"

Metrics/CyclomaticComplexity:
Exclude:
- "app/helpers/activity_helper.rb"

Metrics/PerceivedComplexity:
Exclude:
- "app/helpers/activity_helper.rb"

Style/ExpandPathArguments:
Exclude:
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ gem 'dotenv-rails'

gem 'cancancan'

gem 'paper_trail'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: %i[mri mingw x64_mingw]
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ GEM
mini_portile2 (~> 2.4.0)
orm_adapter (0.5.0)
pagy (3.10.0)
paper_trail (11.1.0)
activerecord (>= 5.2)
request_store (~> 1.1)
parallel (1.20.1)
parser (2.7.2.0)
ast (~> 2.4.1)
Expand Down Expand Up @@ -189,6 +192,8 @@ GEM
ffi (~> 1.0)
rb-readline (0.5.5)
regexp_parser (1.8.2)
request_store (1.5.0)
rack (>= 1.4)
responders (3.0.1)
actionpack (>= 5.0)
railties (>= 5.0)
Expand Down Expand Up @@ -320,6 +325,7 @@ DEPENDENCIES
listen (>= 3.0.5, < 3.2)
mysql2
pagy (~> 3.7)
paper_trail
pdfkit
puma (~> 3.11)
rails (~> 5.2.3)
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/activity.coffee
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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/activity.scss
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/
12 changes: 12 additions & 0 deletions app/controllers/activity_controller.rb
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
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class ApplicationController < ActionController::Base
include Pagy::Backend
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :set_paper_trail_whodunnit

rescue_from CanCan::AccessDenied do |exception|
flash[:notice] = exception.message
Expand Down
86 changes: 86 additions & 0 deletions app/helpers/activity_helper.rb
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
2 changes: 2 additions & 0 deletions app/models/conservation_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ class ConservationRecord < ApplicationRecord
has_one :cost_return_report, dependent: :destroy

validates :department, :title, :author, :imprint, :call_number, :item_record_number, presence: true

has_paper_trail
end
1 change: 1 addition & 0 deletions app/models/cost_return_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

class CostReturnReport < ApplicationRecord
belongs_to :conservation_record
has_paper_trail
end
2 changes: 2 additions & 0 deletions app/models/external_repair_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

class ExternalRepairRecord < ApplicationRecord
belongs_to :conservation_record

has_paper_trail
end
2 changes: 2 additions & 0 deletions app/models/in_house_repair_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

class InHouseRepairRecord < ApplicationRecord
belongs_to :conservation_record

has_paper_trail
end
2 changes: 2 additions & 0 deletions app/models/treatment_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

class TreatmentReport < ApplicationRecord
belongs_to :conservation_record

has_paper_trail
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ def active_for_authentication?

super && account_active?
end

has_paper_trail
end
20 changes: 20 additions & 0 deletions app/views/activity/index.html.erb
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>
52 changes: 52 additions & 0 deletions app/views/activity/show.html.erb
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>
4 changes: 2 additions & 2 deletions app/views/conservation_records/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@

<% if can? :crud, ConservationRecord %>
<td><%= link_to 'Edit', edit_conservation_record_path(conservation_record) %></td>
<td><%= link_to 'Destroy', conservation_record, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
<td><%= link_to 'Destroy', conservation_record, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
</tr>
<% end %>
</tbody>
Expand Down
3 changes: 3 additions & 0 deletions app/views/shared/_navigation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
<li class="nav-item">
<%= link_to('Users', users_path, class: 'nav-link') %>
</li>
<li class="nav-item">
<%= link_to('Activity', activity_index_path, class: 'nav-link') %>
</li>
<% end %>
<li class="nav-item">
<%= form_with url: '/search', method: :get, local: true, class: "form-inline" do |f| %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

resources :users
resources :controlled_vocabularies, except: [:destroy]
resources :activity

resources :conservation_records do
resources :in_house_repair_records
Expand Down
36 changes: 36 additions & 0 deletions db/migrate/20200131190904_create_versions.rb
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 db/migrate/20200131192546_add_object_changes_to_versions.rb
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
Loading

0 comments on commit 348efcf

Please sign in to comment.