Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ gem "uglifier", ">= 1.3.0"
gem 'webpacker', '~> 5.4'
gem "yajl-ruby"
gem "recaptcha"
gem "paper_trail" # for tracking history of InventoryItem

group :production do
gem 'lograge' # Reduce the noise of logs and include custom fields to it for easier access
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ GEM
nenv (~> 0.1)
shellany (~> 0.0)
orm_adapter (0.5.0)
paper_trail (12.0.0)
activerecord (>= 5.2)
request_store (~> 1.1)
paperclip (6.1.0)
activemodel (>= 4.2.0)
activesupport (>= 4.2.0)
Expand Down Expand Up @@ -607,6 +610,7 @@ DEPENDENCIES
mini_racer (~> 0.3.1)
momentjs-rails
nokogiri (>= 1.10.4)
paper_trail
paperclip
pg (~> 1.2.3)
popper_js
Expand Down
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ApplicationController < ActionController::Base
before_action :log_active_user
before_action :swaddled
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :set_paper_trail_whodunnit

rescue_from ActiveRecord::RecordNotFound, with: :not_found!

Expand Down
2 changes: 2 additions & 0 deletions app/models/inventory_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
class InventoryItem < ApplicationRecord
MAX_INT = 2**31

has_paper_trail

belongs_to :storage_location
belongs_to :item

Expand Down
29 changes: 29 additions & 0 deletions db/migrate/20210612212116_create_versions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This migration creates the `versions` table, the only schema PT requires.
# All other migrations PT provides are optional.
class CreateVersions < ActiveRecord::Migration[6.1]
def change
create_table :versions do |t|
t.string :item_type, { null: false }
t.bigint :item_id, null: false
t.string :event, null: false
t.string :whodunnit
t.jsonb :object

# 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
8 changes: 8 additions & 0 deletions db/migrate/20210612212117_add_object_changes_to_versions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# 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[6.1]
def change
add_column :versions, :object_changes, :jsonb
end
end
11 changes: 11 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,17 @@
t.index ["latitude", "longitude"], name: "index_vendors_on_latitude_and_longitude"
end

create_table "versions", force: :cascade do |t|
t.string "item_type", null: false
t.bigint "item_id", null: false
t.string "event", null: false
t.string "whodunnit"
t.jsonb "object"
t.datetime "created_at"
t.jsonb "object_changes"
t.index %w(item_type item_id), name: "index_versions_on_item_type_and_item_id"
end

add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "adjustments", "organizations"
add_foreign_key "adjustments", "storage_locations"
Expand Down
12 changes: 4 additions & 8 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
#
# Table name: users
#
# id :integer not null, primary key
# id :bigint not null, primary key
# current_sign_in_at :datetime
# current_sign_in_ip :inet
# discarded_at :datetime
# email :string default(""), not null
# encrypted_password :string default(""), not null
# invitation_accepted_at :datetime
Expand All @@ -15,20 +14,17 @@
# invitation_token :string
# invitations_count :integer default(0)
# invited_by_type :string
# last_request_at :datetime
# last_sign_in_at :datetime
# last_sign_in_ip :inet
# name :string default("CHANGEME"), not null
# organization_admin :boolean
# name :string
# remember_created_at :datetime
# reset_password_sent_at :datetime
# reset_password_token :string
# sign_in_count :integer default(0), not null
# super_admin :boolean default(FALSE)
# created_at :datetime not null
# updated_at :datetime not null
# invited_by_id :integer
# organization_id :integer
# invited_by_id :bigint
# partner_id :bigint
#

FactoryBot.define do
Expand Down