Commit 43c7b57

mo <mo.khan@gmail.com>
2018-09-23 22:57:36
add auditing
1 parent 12392cf
app/javascript/packs/application.js
@@ -10,7 +10,7 @@
 import 'bootstrap/dist/js/bootstrap';
 import { Application } from 'stimulus';
 import { definitionsFromContext } from 'stimulus/webpack-helpers';
-import LocalTime from "local-time";
+import LocalTime from 'local-time';
 import Rails from 'rails-ujs';
 import '../application.scss';
 
app/models/authorization.rb
@@ -1,6 +1,7 @@
 # frozen_string_literal: true
 
 class Authorization < ApplicationRecord
+  audited associated_with: :user
   has_secure_token :code
   belongs_to :user
   belongs_to :client
app/models/client.rb
@@ -1,6 +1,7 @@
 # frozen_string_literal: true
 
 class Client < ApplicationRecord
+  audited
   has_secure_token :secret
   has_many :authorizations
 
app/models/token.rb
@@ -1,6 +1,7 @@
 # frozen_string_literal: true
 
 class Token < ApplicationRecord
+  audited associated_with: :subject
   enum token_type: { access: 0, refresh: 1 }
   belongs_to :authorization, optional: true
   belongs_to :subject, polymorphic: true
app/models/user.rb
@@ -3,6 +3,7 @@
 class User < ApplicationRecord
   VALID_TIMEZONES = ActiveSupport::TimeZone::MAPPING.values
   VALID_LOCALES = I18n.available_locales.map(&:to_s)
+  audited
   has_secure_password
   has_many :sessions, foreign_key: "user_id", class_name: UserSession.name
 
app/models/user_session.rb
@@ -1,6 +1,7 @@
 # frozen_string_literal: true
 
 class UserSession < ApplicationRecord
+  audited associated_with: :user
   belongs_to :user
   before_validation do |model|
     model.key = SecureRandom.urlsafe_base64(32)
config/initializers/audited.rb
@@ -0,0 +1,4 @@
+# frozen_string_literal: true
+
+Audited.max_audits = 100
+Audited.current_user_method = :current_user
db/migrate/20180923222720_install_audited.rb
@@ -0,0 +1,32 @@
+# frozen_string_literal: true
+
+class InstallAudited < ActiveRecord::Migration[5.2]
+  def self.up
+    create_table :audits, force: true do |t|
+      t.column :auditable_id, :integer
+      t.column :auditable_type, :string
+      t.column :associated_id, :integer
+      t.column :associated_type, :string
+      t.column :user_id, :integer
+      t.column :user_type, :string
+      t.column :username, :string
+      t.column :action, :string
+      t.column :audited_changes, :text
+      t.column :version, :integer, default: 0
+      t.column :comment, :string
+      t.column :remote_address, :string
+      t.column :request_uuid, :string
+      t.column :created_at, :datetime
+    end
+
+    add_index :audits, [:auditable_type, :auditable_id, :version]
+    add_index :audits, [:associated_type, :associated_id]
+    add_index :audits, [:user_id, :user_type]
+    add_index :audits, :request_uuid
+    add_index :audits, :created_at
+  end
+
+  def self.down
+    drop_table :audits
+  end
+end
db/schema.rb
@@ -10,7 +10,37 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 2018_09_22_211216) do
+ActiveRecord::Schema.define(version: 2018_09_23_222720) do
+
+  create_table "audits", force: :cascade do |t|
+    t.integer "auditable_id"
+    t.string "auditable_type"
+    t.integer "associated_id"
+    t.string "associated_type"
+    t.integer "user_id"
+    t.string "user_type"
+    t.string "username"
+    t.string "action"
+    t.text "audited_changes"
+    t.integer "version", default: 0
+    t.string "comment"
+    t.string "remote_address"
+    t.string "request_uuid"
+    t.datetime "created_at"
+    t.index ["associated_type", "associated_id"], name: "associated_index"
+    t.index ["auditable_type", "auditable_id", "version"], name: "auditable_index"
+    t.index ["created_at"], name: "index_audits_on_created_at"
+    t.index ["request_uuid"], name: "index_audits_on_request_uuid"
+    t.index ["user_id", "user_type"], name: "user_index"
+  end
+
+  create_table "authentications", force: :cascade do |t|
+    t.integer "user_id"
+    t.string "type", null: false
+    t.datetime "created_at", null: false
+    t.datetime "updated_at", null: false
+    t.index ["user_id"], name: "index_authentications_on_user_id"
+  end
 
   create_table "authorizations", force: :cascade do |t|
     t.integer "user_id"
Gemfile
@@ -5,6 +5,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
 ruby '2.5.1'
 
 gem 'activerecord-session_store', '~> 1.1'
+gem 'audited', '~> 4.8'
 gem 'bcrypt', '~> 3.1'
 gem 'bootsnap', '>= 1.1.0', require: false
 gem 'coffee-rails', '~> 4.2'
Gemfile.lock
@@ -52,6 +52,8 @@ GEM
       public_suffix (>= 2.0.2, < 4.0)
     arel (9.0.0)
     ast (2.4.0)
+    audited (4.8.0)
+      activerecord (>= 4.0, < 5.3)
     bcrypt (3.1.12)
     bindex (0.5.0)
     bootsnap (1.3.1)
@@ -295,6 +297,7 @@ PLATFORMS
 
 DEPENDENCIES
   activerecord-session_store (~> 1.1)
+  audited (~> 4.8)
   bcrypt (~> 3.1)
   bootsnap (>= 1.1.0)
   brakeman (~> 4.3)