Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: track history of submission changes #1491

Closed
wants to merge 1 commit into from
Closed
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 @@ -33,6 +33,7 @@ gem "jquery-rails"
gem "kaminari" # for pagination
gem "money" # for currency/money formatting
gem "neat", "1.7.2" # required for watt
gem "paper_trail" # track history of submissions
gem "pg_search" # for searching within convection's database
gem "premailer-rails" # generate text parts from HTML automatically
gem "rack-cors" # to allow cross-origin requests
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ GEM
omniauth-rails_csrf_protection (1.0.0)
actionpack (>= 4.2)
omniauth (~> 2.0)
paper_trail (15.1.0)
activerecord (>= 6.1)
request_store (~> 1.4)
parallel (1.20.1)
parser (3.0.1.1)
ast (~> 2.4.1)
Expand Down Expand Up @@ -405,6 +408,8 @@ GEM
redcarpet (3.5.1)
redis (4.8.1)
regexp_parser (2.1.1)
request_store (1.7.0)
rack (>= 1.4)
restforce (5.3.0)
faraday (>= 0.9.0, <= 1.10.0)
faraday_middleware (>= 0.8.8, <= 2.0)
Expand Down Expand Up @@ -578,6 +583,7 @@ DEPENDENCIES
kaminari
money
neat (= 1.7.2)
paper_trail
pg
pg_search
premailer-rails
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/api/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@ def current_user_roles
def jwt_token
@jwt_token ||= request.env["JWT_TOKEN"]
end

def user_for_paper_trail
current_user
end
end
end
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ApplicationController < ActionController::Base

before_action :set_current_user
before_action :set_raven_context
before_action :set_paper_trail_whodunnit

NotAuthorized = Class.new(StandardError)

Expand All @@ -29,4 +30,8 @@ def set_current_user
def set_raven_context
Raven.user_context(id: @current_user)
end

def user_for_paper_trail
@current_user
end
end
2 changes: 2 additions & 0 deletions app/models/submission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Submission < ApplicationRecord
include PgSearch::Model
include ReloadUuid

has_paper_trail

alias_attribute :deleted?, :deleted_at

scope :not_deleted, -> { where(deleted_at: nil) }
Expand Down
37 changes: 37 additions & 0 deletions db/migrate/20240711211617_create_versions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This migration creates the `versions` table, the only schema PT requires.
# All other migrations PT provides are optional.
class CreateVersions < ActiveRecord::Migration[6.1]
# 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.bigint :item_id, null: false
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)
#
# MySQL users should use the following line for `created_at`
# t.datetime :created_at, limit: 6
t.datetime :created_at
end
add_index :versions, %i[item_type item_id]
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_07_05_092437) do
ActiveRecord::Schema.define(version: 2024_07_11_211617) do

# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
Expand Down Expand Up @@ -255,6 +255,16 @@
t.index ["gravity_user_id"], name: "index_users_on_gravity_user_id"
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.text "object"
t.datetime "created_at"
t.index ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id"
end

add_foreign_key "assets", "submissions"
add_foreign_key "notes", "submissions"
add_foreign_key "notes", "users"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@

update_response = body["data"]["updateConsignmentSubmission"]
expect(update_response).to_not eq nil
expect(submission1.versions.size).to eq(2)
expect(submission1.versions.last.whodunnit).to eq("userid4")
end
end

Expand Down