AuditRecord
This commit is contained in:
parent
2804c55fd9
commit
c2dfc1f04a
5 changed files with 42 additions and 1 deletions
|
@ -1,3 +1,18 @@
|
|||
class ApplicationRecord < ActiveRecord::Base
|
||||
self.abstract_class = true
|
||||
def update(*args)
|
||||
result = super(*args)
|
||||
AuditRecord.create(model: self.class, action: 'update', params: self.to_json)
|
||||
result
|
||||
end
|
||||
def save(*args)
|
||||
result = super(*args)
|
||||
AuditRecord.create(model: self.class, action: 'save', params: self.to_json)
|
||||
result
|
||||
end
|
||||
def self.create(*args)
|
||||
result = super(*args)
|
||||
AuditRecord.create(model: self.class, action: 'create', params: result.to_json)
|
||||
result
|
||||
end
|
||||
end
|
||||
|
|
2
app/models/audit_record.rb
Normal file
2
app/models/audit_record.rb
Normal file
|
@ -0,0 +1,2 @@
|
|||
class AuditRecord < ActiveRecord::Base
|
||||
end
|
11
db/migrate/20210321225317_create_audit_records.rb
Normal file
11
db/migrate/20210321225317_create_audit_records.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
class CreateAuditRecords < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
create_table :audit_records do |t|
|
||||
t.string :model
|
||||
t.string :action
|
||||
t.string :params
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
10
db/schema.rb
generated
10
db/schema.rb
generated
|
@ -10,7 +10,15 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2021_03_21_213901) do
|
||||
ActiveRecord::Schema.define(version: 2021_03_21_225317) do
|
||||
|
||||
create_table "audit_records", force: :cascade do |t|
|
||||
t.string "model"
|
||||
t.string "action"
|
||||
t.string "params"
|
||||
t.datetime "created_at", precision: 6, null: false
|
||||
t.datetime "updated_at", precision: 6, null: false
|
||||
end
|
||||
|
||||
create_table "authors", force: :cascade do |t|
|
||||
t.string "first_name"
|
||||
|
|
5
spec/models/audit_record_spec.rb
Normal file
5
spec/models/audit_record_spec.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe AuditRecord, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue