audit-log 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3fe77595997c59059c5a91e1bc753aa017692ddea0a268dfa7f636304085d1d1
4
- data.tar.gz: ddc300e06f79289ae8f5078479e272468df07d95992d82d6e20571515cdd6d2d
3
+ metadata.gz: e0f1ddb5d2e2f1d9b84d0e83a02a83990cce0cc0a1c7fd931d84a607af1f4837
4
+ data.tar.gz: 8b072cf51106505f2100aa4dd7760cfef429b131d79b9d3c78e5fe738fcd7769
5
5
  SHA512:
6
- metadata.gz: a493e052a5429903138bfbde7476ad6268c2fc3eae34cce1b1d9e1c3d38ae958f17808e65a5dc8063834216dd893cb8e1c40e130a326dafa193656470c2338c0
7
- data.tar.gz: 69df72ffd3c5e8912c8006f5fe0f8159d123f2075e3590c13b7853ad0258620e206f114c77644f32a9c1050087f032388d9344ef4520f3f5fe3d3b8312e1f5ac
6
+ metadata.gz: 619bdc22b753f80b8b221c4a47531d03f17eaffc19a5f36d3ae3fab4a03562e082eae95335f94dc717de6f5b4cada0a512936c0f62353d57106ff7065081272d
7
+ data.tar.gz: f79ea23624ed6ccce2b7e51c8cdfdbbc8cc8229f9f0b508dfa349a6d637215db1c8d79404a6154daf3a3a055422250a990e4757288317aa62734a479eebf8975
data/README.md CHANGED
@@ -2,12 +2,25 @@
2
2
 
3
3
  Trail audit logs (Operation logs) into the database for user behaviors, including a web UI to query logs.
4
4
 
5
+ [![Build Status](https://travis-ci.org/rails-engine/audit-log.svg?branch=master)](https://travis-ci.org/rails-engine/audit-log)
6
+
7
+ ## Demo UI
8
+
9
+ Audit log list:
10
+
11
+ <img width="870" src="https://user-images.githubusercontent.com/5518/58676735-e9570d80-838b-11e9-8ac0-6c5145b7fbb0.png">
12
+
13
+ Detail page:
14
+
15
+ <img width="870" src="https://user-images.githubusercontent.com/5518/58676737-e9570d80-838b-11e9-9292-63389b2d54cb.png">
16
+
17
+
5
18
  ## Installation
6
19
 
7
20
  Add this line to your application's Gemfile:
8
21
 
9
22
  ```ruby
10
- gem 'audit-log'
23
+ gem "audit-log"
11
24
  ```
12
25
 
13
26
  And then execute:
@@ -15,6 +28,12 @@ And then execute:
15
28
  $ bundle
16
29
  ```
17
30
 
31
+ Generate files:
32
+
33
+ ```bash
34
+ $ rails g audit_log:install
35
+ ```
36
+
18
37
  ## Usage
19
38
 
20
39
  Use in controllers:
@@ -22,15 +41,34 @@ Use in controllers:
22
41
  ```rb
23
42
  class TicktsController < ApplicationController
24
43
  def index
25
- audit! :tickets, nil
44
+ audit! :list_ticket, nil
45
+ end
46
+
47
+ def create
48
+ if @ticket.save
49
+ audit! :create_ticket, @ticket, payload: ticket_params
50
+ else
51
+ render :new
52
+ end
26
53
  end
27
54
 
28
55
  def update
29
- audit! :update_ticket, @ticket, payload: ticket_params
56
+ if @ticket.save
57
+ audit! :update_ticket, @ticket, payload: ticket_params
58
+ else
59
+ render :edit
60
+ end
61
+ end
62
+
63
+ def approve
64
+ if @ticket.approve
65
+ audit! :approve_ticket, @ticket, payload: ticket_params
66
+ end
30
67
  end
31
68
 
32
69
  def destroy
33
- audit! :delete_ticket
70
+ # store original attributes for destroy for keep values
71
+ audit! :delete_ticket, nil, @ticket.attributes
34
72
  end
35
73
 
36
74
  private
@@ -49,6 +87,32 @@ AuditLog.audit!(:sign_in, @user, payload: { ip: request.ip })
49
87
  AuditLog.audit!(:create_address, nil, payload: params)
50
88
  ```
51
89
 
90
+ Change `config/routes.rb` to add Route:
91
+
92
+ ```rb
93
+ Rails.application.routes.draw do
94
+ authenticate :user, -> (u) { u.admin? } do
95
+ mount AuditLog::Engine => "/audit-log"
96
+ end
97
+ end
98
+ ```
99
+
100
+ I18n for audit names, you need create a `config/locales/audit_log.zh-CN.yml`:
101
+
102
+ ```yml
103
+ zh-CN:
104
+ audit_log:
105
+ action:
106
+ sign_in: 登录
107
+ update_password: 修改密码
108
+ create_address: 添加住址
109
+ list_ticket: 查看工单列表
110
+ create_ticket: 创建工单
111
+ update_ticket: 更新工单
112
+ delete_ticket: 删除工单
113
+ approve_ticket: 审批工单
114
+ ```
115
+
52
116
  ## License
53
117
 
54
118
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,34 +1,34 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  begin
4
- require "bundler/setup"
4
+ require 'bundler/setup'
5
5
  rescue LoadError
6
- puts "You must `gem install bundler` and `bundle install` to run rake tasks"
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
7
  end
8
8
 
9
- require "rdoc/task"
9
+ require 'rdoc/task'
10
10
 
11
11
  RDoc::Task.new(:rdoc) do |rdoc|
12
- rdoc.rdoc_dir = "rdoc"
13
- rdoc.title = "AuditLog"
14
- rdoc.options << "--line-numbers"
15
- rdoc.rdoc_files.include("README.md")
16
- rdoc.rdoc_files.include("lib/**/*.rb")
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'AuditLog'
14
+ rdoc.options << '--line-numbers'
15
+ rdoc.rdoc_files.include('README.md')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
17
  end
18
18
 
19
- APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
20
- load "rails/tasks/engine.rake"
19
+ APP_RAKEFILE = File.expand_path('test/dummy/Rakefile', __dir__)
20
+ load 'rails/tasks/engine.rake'
21
21
 
22
- load "rails/tasks/statistics.rake"
22
+ load 'rails/tasks/statistics.rake'
23
23
 
24
- require "bundler/gem_tasks"
24
+ require 'bundler/gem_tasks'
25
25
 
26
- require "rake/testtask"
26
+ require 'rake/testtask'
27
27
 
28
28
  Rake::TestTask.new(:test) do |t|
29
- t.libs << "lib"
30
- t.libs << "test"
31
- t.pattern = "test/**/*_test.rb"
29
+ t.libs << 'lib'
30
+ t.libs << 'test'
31
+ t.pattern = 'test/**/*_test.rb'
32
32
  t.verbose = false
33
33
  end
34
34
 
@@ -2,14 +2,14 @@
2
2
 
3
3
  module AuditLog
4
4
  class LogsController < ActionController::Base
5
- layout "audit-log/application"
5
+ layout 'audit-log/application'
6
6
  before_action :set_log, only: %i[show destroy]
7
7
 
8
8
  def index
9
- @logs = Log.order("id desc").includes(:user)
9
+ @logs = Log.order('id desc').includes(:user)
10
10
 
11
11
  if params[:q]
12
- @logs = @logs.where("action like ?", "%#{params[:q]}%")
12
+ @logs = @logs.where('action like ?', "%#{params[:q]}%")
13
13
  end
14
14
 
15
15
  @logs = @logs.page(params[:page]).per(15)
@@ -19,9 +19,9 @@ module AuditLog
19
19
 
20
20
  private
21
21
 
22
- # Use callbacks to share common setup or constraints between actions.
23
- def set_log
24
- @log = Log.find(params[:id])
25
- end
22
+ # Use callbacks to share common setup or constraints between actions.
23
+ def set_log
24
+ @log = Log.find(params[:id])
25
+ end
26
26
  end
27
27
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  AuditLog::Engine.routes.draw do
4
- resources :logs, path: ""
4
+ resources :logs, path: ''
5
5
  end
@@ -1,17 +1,17 @@
1
1
  class CreateAuditLogs < ActiveRecord::Migration[5.0]
2
2
  def change
3
- create_table "audit_logs", force: :cascade do |t|
4
- t.string "action", null: false
5
- t.integer "user_id"
6
- t.integer "record_id"
7
- t.string "record_type"
8
- t.text "payload"
9
- t.text "request"
10
- t.datetime "created_at"
11
- t.datetime "updated_at"
12
- t.index ["record_type", "record_id"], using: :btree
13
- t.index ["user_id", "action"], using: :btree
14
- t.index ["action"], using: :btree
3
+ create_table 'audit_logs', force: :cascade do |t|
4
+ t.string 'action', null: false
5
+ t.integer 'user_id'
6
+ t.integer 'record_id'
7
+ t.string 'record_type'
8
+ t.text 'payload'
9
+ t.text 'request'
10
+ t.datetime 'created_at'
11
+ t.datetime 'updated_at'
12
+ t.index %w[record_type record_id], using: :btree
13
+ t.index %w[user_id action], using: :btree
14
+ t.index ['action'], using: :btree
15
15
  end
16
16
  end
17
17
  end
@@ -1,17 +1,18 @@
1
- require_relative "./audit-log/version"
2
- require_relative "./audit-log/configuration"
3
- require_relative "./audit-log/model"
4
- require_relative "./audit-log/engine"
5
- require "kaminari"
1
+ require_relative './audit-log/version'
2
+ require_relative './audit-log/configuration'
3
+ require_relative './audit-log/model'
4
+ require_relative './audit-log/engine'
5
+ require 'kaminari'
6
6
 
7
7
  module AuditLog
8
8
  class << self
9
9
  def config
10
10
  return @config if defined?(@config)
11
+
11
12
  @config = Configuration.new
12
- @config.user_class = "User"
13
- @config.current_user_method = "current_user"
14
- @config.user_name_method = "name"
13
+ @config.user_class = 'User'
14
+ @config.current_user_method = 'current_user'
15
+ @config.user_name_method = 'name'
15
16
  @config
16
17
  end
17
18
 
@@ -28,7 +29,7 @@ module AuditLog
28
29
  request_info = {
29
30
  ip: request.ip,
30
31
  url: request.url,
31
- user_agent: request.user_agent,
32
+ user_agent: request.user_agent
32
33
  }
33
34
  end
34
35
 
@@ -41,4 +42,4 @@ module AuditLog
41
42
  )
42
43
  end
43
44
  end
44
- end
45
+ end
@@ -8,4 +8,4 @@ module AuditLog
8
8
  AuditLog.audit!(action, record, payload: payload, request: request, user: user)
9
9
  end
10
10
  end
11
- end
11
+ end
@@ -1,10 +1,10 @@
1
- require_relative "./controller_helper"
1
+ require_relative './controller_helper'
2
2
 
3
3
  module AuditLog
4
4
  class Engine < Rails::Engine
5
5
  isolate_namespace AuditLog
6
6
 
7
- ActiveSupport.on_load(:action_controller_base) do
7
+ ActiveSupport.on_load(:action_controller) do
8
8
  prepend AuditLog::ControllerHelper
9
9
  end
10
10
  end
@@ -3,7 +3,7 @@ module AuditLog
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
- self.table_name = "audit_logs"
6
+ self.table_name = 'audit_logs'
7
7
 
8
8
  serialize :payload, JSON
9
9
  serialize :request, JSON
@@ -22,7 +22,7 @@ module AuditLog
22
22
  end
23
23
 
24
24
  def user_name
25
- return "none" if self.user.blank?
25
+ return 'none' if self.user.blank?
26
26
 
27
27
  self.user.send(AuditLog.config.user_name_method)
28
28
  end
@@ -31,4 +31,4 @@ module AuditLog
31
31
  I18n.t("audit_log.action.#{self.action}", default: self.action)
32
32
  end
33
33
  end
34
- end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module AuditLog
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -1,18 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "rails/generators"
3
+ require 'rails/generators'
4
4
  module AuditLog
5
5
  module Generators
6
6
  class InstallGenerator < Rails::Generators::Base
7
7
  desc "Create AuditLog's base files"
8
- source_root File.expand_path("../../..", __dir__)
8
+ source_root File.expand_path('../../..', __dir__)
9
9
 
10
10
  def add_initializer
11
- template "config/initializers/audit-log.rb", "config/initializers/audit-log.rb"
11
+ template 'config/initializers/audit-log.rb', 'config/initializers/audit-log.rb'
12
12
  end
13
13
 
14
14
  def add_migrations
15
- exec("rake audit_log:install:migrations")
15
+ exec('rake audit_log:install:migrations')
16
16
  end
17
17
  end
18
18
  end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: audit-log
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-27 00:00:00.000000000 Z
11
+ date: 2019-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails
14
+ name: kaminari
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '4.0'
19
+ version: '0.15'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '4.0'
26
+ version: '0.15'
27
27
  - !ruby/object:Gem::Dependency
28
- name: kaminari
28
+ name: rails
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0.15'
33
+ version: '4.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '0.15'
40
+ version: '4.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: mysql2
43
43
  requirement: !ruby/object:Gem::Requirement