Skip to content

Commit 16f1486

Browse files
authored
Add paper_trail gem to track InventoryItem changes (#2448)
1 parent 5d6497d commit 16f1486

8 files changed

Lines changed: 60 additions & 8 deletions

File tree

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ gem "uglifier", ">= 1.3.0"
5353
gem 'webpacker', '~> 5.4'
5454
gem "yajl-ruby"
5555
gem "recaptcha"
56+
gem "paper_trail" # for tracking history of InventoryItem
5657

5758
group :production do
5859
gem 'lograge' # Reduce the noise of logs and include custom fields to it for easier access

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@ GEM
311311
nenv (~> 0.1)
312312
shellany (~> 0.0)
313313
orm_adapter (0.5.0)
314+
paper_trail (12.0.0)
315+
activerecord (>= 5.2)
316+
request_store (~> 1.1)
314317
paperclip (6.1.0)
315318
activemodel (>= 4.2.0)
316319
activesupport (>= 4.2.0)
@@ -607,6 +610,7 @@ DEPENDENCIES
607610
mini_racer (~> 0.3.1)
608611
momentjs-rails
609612
nokogiri (>= 1.10.4)
613+
paper_trail
610614
paperclip
611615
pg (~> 1.2.3)
612616
popper_js

app/controllers/application_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class ApplicationController < ActionController::Base
99
before_action :log_active_user
1010
before_action :swaddled
1111
before_action :configure_permitted_parameters, if: :devise_controller?
12+
before_action :set_paper_trail_whodunnit
1213

1314
rescue_from ActiveRecord::RecordNotFound, with: :not_found!
1415

app/models/inventory_item.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
class InventoryItem < ApplicationRecord
1414
MAX_INT = 2**31
1515

16+
has_paper_trail
17+
1618
belongs_to :storage_location
1719
belongs_to :item
1820

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# This migration creates the `versions` table, the only schema PT requires.
2+
# All other migrations PT provides are optional.
3+
class CreateVersions < ActiveRecord::Migration[6.1]
4+
def change
5+
create_table :versions do |t|
6+
t.string :item_type, { null: false }
7+
t.bigint :item_id, null: false
8+
t.string :event, null: false
9+
t.string :whodunnit
10+
t.jsonb :object
11+
12+
# Known issue in MySQL: fractional second precision
13+
# -------------------------------------------------
14+
#
15+
# MySQL timestamp columns do not support fractional seconds unless
16+
# defined with "fractional seconds precision". MySQL users should manually
17+
# add fractional seconds precision to this migration, specifically, to
18+
# the `created_at` column.
19+
# (https://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html)
20+
#
21+
# MySQL users should also upgrade to at least rails 4.2, which is the first
22+
# version of ActiveRecord with support for fractional seconds in MySQL.
23+
# (https://github.com/rails/rails/pull/14359)
24+
#
25+
t.datetime :created_at
26+
end
27+
add_index :versions, %i(item_type item_id)
28+
end
29+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# This migration adds the optional `object_changes` column, in which PaperTrail
2+
# will store the `changes` diff for each update event. See the readme for
3+
# details.
4+
class AddObjectChangesToVersions < ActiveRecord::Migration[6.1]
5+
def change
6+
add_column :versions, :object_changes, :jsonb
7+
end
8+
end

db/schema.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,17 @@
430430
t.index ["latitude", "longitude"], name: "index_vendors_on_latitude_and_longitude"
431431
end
432432

433+
create_table "versions", force: :cascade do |t|
434+
t.string "item_type", null: false
435+
t.bigint "item_id", null: false
436+
t.string "event", null: false
437+
t.string "whodunnit"
438+
t.jsonb "object"
439+
t.datetime "created_at"
440+
t.jsonb "object_changes"
441+
t.index %w(item_type item_id), name: "index_versions_on_item_type_and_item_id"
442+
end
443+
433444
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
434445
add_foreign_key "adjustments", "organizations"
435446
add_foreign_key "adjustments", "storage_locations"

spec/factories/users.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
#
33
# Table name: users
44
#
5-
# id :integer not null, primary key
5+
# id :bigint not null, primary key
66
# current_sign_in_at :datetime
77
# current_sign_in_ip :inet
8-
# discarded_at :datetime
98
# email :string default(""), not null
109
# encrypted_password :string default(""), not null
1110
# invitation_accepted_at :datetime
@@ -15,20 +14,17 @@
1514
# invitation_token :string
1615
# invitations_count :integer default(0)
1716
# invited_by_type :string
18-
# last_request_at :datetime
1917
# last_sign_in_at :datetime
2018
# last_sign_in_ip :inet
21-
# name :string default("CHANGEME"), not null
22-
# organization_admin :boolean
19+
# name :string
2320
# remember_created_at :datetime
2421
# reset_password_sent_at :datetime
2522
# reset_password_token :string
2623
# sign_in_count :integer default(0), not null
27-
# super_admin :boolean default(FALSE)
2824
# created_at :datetime not null
2925
# updated_at :datetime not null
30-
# invited_by_id :integer
31-
# organization_id :integer
26+
# invited_by_id :bigint
27+
# partner_id :bigint
3228
#
3329

3430
FactoryBot.define do

0 commit comments

Comments
 (0)