model_log 1.0.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.gitignore +3 -0
- data/.travis.yml +0 -3
- data/CODE_OF_CONDUCT.md +1 -1
- data/LICENSE.txt +1 -1
- data/README.md +70 -28
- data/Rakefile +0 -3
- data/lib/model_log/config.rb +14 -0
- data/lib/model_log/context.rb +13 -0
- data/lib/model_log/controller.rb +36 -0
- data/lib/model_log/default/formatter.rb +56 -0
- data/lib/model_log/helpers/context.rb +33 -0
- data/lib/model_log/log/content.rb +28 -0
- data/lib/model_log/log/initializer.rb +23 -0
- data/lib/model_log/log/processor.rb +38 -0
- data/lib/model_log/logger.rb +22 -0
- data/lib/model_log/modeller.rb +46 -0
- data/lib/model_log/store.rb +49 -0
- data/lib/model_log/version.rb +1 -1
- data/lib/model_log.rb +36 -13
- data/model_log.gemspec +12 -9
- metadata +25 -48
- data/.rspec +0 -3
- data/Gemfile.lock +0 -35
- data/lib/model_log/class_methods/controllers.rb +0 -33
- data/lib/model_log/class_methods/models.rb +0 -99
- data/lib/model_log/passport.rb +0 -18
- data/lib/model_log/request.rb +0 -17
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: f536ddd5bcec92fad2368e4e637fd678620ca82cfe38438cbdc434a36c6da913
|
|
4
|
+
data.tar.gz: 7a597e5c6e17e43da418c1b5c3e37bdb733e8b0f1b6e1af35c73fd00c4598130
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f564bfb542098de5b2aedeb67de68db8d6ccbe6077a05e657d44dd39f6ac5bfc1f75a8efeeb78a27e51e1418981bd403ba8edab4e9605413a523c36b671f54a0
|
|
7
|
+
data.tar.gz: 6b1780b79d240b9edcfd0e05777f20f5bba2d6295e5a0b3b8801ac069ab5919bd54fa15ceb06dfc05080c3a145ad5fb6cc7296a98efad15dfa8a3e1f6d56cbad
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/CODE_OF_CONDUCT.md
CHANGED
|
@@ -55,7 +55,7 @@ further defined and clarified by project maintainers.
|
|
|
55
55
|
## Enforcement
|
|
56
56
|
|
|
57
57
|
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
-
reported by contacting the project team at
|
|
58
|
+
reported by contacting the project team at jk-sun@qq.com. All
|
|
59
59
|
complaints will be reviewed and investigated and will result in a response that
|
|
60
60
|
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
61
|
obligated to maintain confidentiality with regard to the reporter of an incident.
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
# ModelLog
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
It's designed to be used to record all changes of the models to a log file for Rails. You can also use it to record who made the changes.
|
|
4
|
+
|
|
5
|
+
## Supported Versions
|
|
6
|
+
|
|
7
|
+
- Ruby 2.0.0+
|
|
8
|
+
- Rails 3.0.0+
|
|
4
9
|
|
|
5
10
|
## Installation
|
|
6
11
|
|
|
7
12
|
Add this line to your application's Gemfile:
|
|
8
13
|
|
|
9
14
|
```ruby
|
|
10
|
-
gem 'model_log',
|
|
15
|
+
gem 'model_log', '~> 2.0.0'
|
|
11
16
|
```
|
|
12
17
|
|
|
13
18
|
And then execute:
|
|
@@ -24,49 +29,86 @@ class User < ActiveRecord::Base
|
|
|
24
29
|
end
|
|
25
30
|
```
|
|
26
31
|
|
|
27
|
-
## Log Path
|
|
32
|
+
## Log File Path
|
|
28
33
|
|
|
29
34
|
The log is saved in the `log/` section of your rails app.
|
|
30
35
|
|
|
31
|
-
The development environment
|
|
36
|
+
The log in development environment is in `model_log_development.log`.
|
|
32
37
|
|
|
33
|
-
The
|
|
38
|
+
The log in production environment is in `model_log_production.log`.
|
|
34
39
|
|
|
35
|
-
##
|
|
40
|
+
## Configure
|
|
36
41
|
|
|
37
|
-
|
|
42
|
+
By default, ModelLog uses the `current_user` method in your controller. The default identity field is `id`.
|
|
38
43
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
To use a method other than `current_passport` and a field other than `username`, put the following in an initializer:
|
|
44
|
+
To use a method other than `current_user` and an identity field other than `id`, put the following in an initializer:
|
|
42
45
|
|
|
43
46
|
```ruby
|
|
44
47
|
# config/initializers/model_log.rb
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
+
ModelLog.configure do |config|
|
|
49
|
+
config.current_user_method = :current_manager # default: :current_user
|
|
50
|
+
config.identity_field = :username # default: :id
|
|
51
|
+
config.separator = ' ' # default: "\t"
|
|
52
|
+
config.logger_datetime_format = '%Y-%m-%d %H:%M:%S'
|
|
53
|
+
end
|
|
48
54
|
```
|
|
49
55
|
|
|
50
|
-
##
|
|
56
|
+
## Custom Log Formatter
|
|
51
57
|
|
|
52
|
-
|
|
58
|
+
There are default log formatter. You can also custom them here. For example:
|
|
53
59
|
|
|
54
60
|
```ruby
|
|
55
61
|
# config/initializers/model_log.rb
|
|
62
|
+
class Myformatter
|
|
63
|
+
include ModelLog::Helpers::Context
|
|
64
|
+
# Some methods are provided here. You can use them directly.
|
|
65
|
+
# current_user The information about the current user, which could be nil.
|
|
66
|
+
# requester The infomation about the current network request, which could be nil.
|
|
67
|
+
# resource Current resource.
|
|
68
|
+
# changes The changes to the current resource.
|
|
69
|
+
# changed? Whether the current resource has been changed.
|
|
70
|
+
# action update|create|destroy
|
|
71
|
+
# is_update? If the action is update.
|
|
72
|
+
# is_create? If the action is create.
|
|
73
|
+
# is_destroy? If the action is destroy.
|
|
74
|
+
|
|
75
|
+
def call
|
|
76
|
+
return unless changed?
|
|
77
|
+
content = []
|
|
78
|
+
content += requester_content if requester
|
|
79
|
+
content += user_content if current_user
|
|
80
|
+
content += resource_content
|
|
81
|
+
content.join(' ')
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
private
|
|
85
|
+
|
|
86
|
+
def user_content
|
|
87
|
+
[
|
|
88
|
+
current_user.id,
|
|
89
|
+
current_user.username
|
|
90
|
+
]
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def requester_content
|
|
94
|
+
[
|
|
95
|
+
requester.request_method,
|
|
96
|
+
requester.url,
|
|
97
|
+
]
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def resource_content
|
|
101
|
+
[
|
|
102
|
+
changes
|
|
103
|
+
]
|
|
104
|
+
end
|
|
105
|
+
end
|
|
56
106
|
|
|
57
|
-
ModelLog.
|
|
107
|
+
ModelLog.configure do |config|
|
|
108
|
+
config.formatter = MyFormatter
|
|
109
|
+
end
|
|
58
110
|
```
|
|
59
111
|
|
|
60
|
-
##
|
|
61
|
-
If you run a rails app hint `The git source git@bitbucket.org:scige/model_log.git is not yet checked out. Please run “bundle install” before trying to start your application`, Try to run
|
|
62
|
-
|
|
63
|
-
$ bundle install --deployment
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
## Gem Update
|
|
68
|
-
|
|
69
|
-
If you want to update the gem, run:
|
|
70
|
-
|
|
71
|
-
$ bundle update model_log
|
|
112
|
+
## Copyright
|
|
72
113
|
|
|
114
|
+
Copyright (c) 2018- jk-sun. See LICENSE.tet for further details.
|
data/Rakefile
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module ModelLog
|
|
2
|
+
class Config
|
|
3
|
+
attr_accessor :current_user_method, :identity_field, :separator, :formatter,
|
|
4
|
+
:logger_level, :logger_datetime_format
|
|
5
|
+
|
|
6
|
+
def initialize
|
|
7
|
+
@current_user_method ||= :current_user
|
|
8
|
+
@identity_field ||= :id
|
|
9
|
+
@separator ||= "\t"
|
|
10
|
+
@formatter ||= Default::Formatter
|
|
11
|
+
@logger_level ||= Logger::INFO # DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
module ModelLog
|
|
2
|
+
module Controller
|
|
3
|
+
def self.included(base)
|
|
4
|
+
base.extend ClassMethods
|
|
5
|
+
base.class_eval do
|
|
6
|
+
set_model_log_stroe
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
module ClassMethods
|
|
11
|
+
def set_model_log_stroe
|
|
12
|
+
if ::Rails::VERSION::MAJOR > 3
|
|
13
|
+
before_action :set_current_user
|
|
14
|
+
before_action :set_current_requester
|
|
15
|
+
else
|
|
16
|
+
before_filter :set_current_user
|
|
17
|
+
before_filter :set_current_requester
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def set_current_user
|
|
25
|
+
Store.clear_current_user!
|
|
26
|
+
Store.current_user = send(ModelLog.config.current_user_method) rescue nil
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def set_current_requester
|
|
30
|
+
Store.clear_current_requester!
|
|
31
|
+
Store.current_requester = request.is_a?(ActionDispatch::Request) && request rescue nil
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
ActionController::Base.send(:include, ModelLog::Controller) if defined? ActionController::Base
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
module ModelLog
|
|
2
|
+
module Default
|
|
3
|
+
class Formatter
|
|
4
|
+
include Helpers::Context
|
|
5
|
+
|
|
6
|
+
def call
|
|
7
|
+
return unless changed?
|
|
8
|
+
content = []
|
|
9
|
+
content += thread_content
|
|
10
|
+
content += requester_content if requester
|
|
11
|
+
content += user_content if current_user
|
|
12
|
+
content += action_content
|
|
13
|
+
content += resource_content
|
|
14
|
+
content.join(ModelLog.config.separator)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def thread_content
|
|
20
|
+
[
|
|
21
|
+
Thread.current.object_id
|
|
22
|
+
]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def requester_content
|
|
26
|
+
[
|
|
27
|
+
requester.request_method,
|
|
28
|
+
requester.url,
|
|
29
|
+
requester.referer,
|
|
30
|
+
requester.remote_ip,
|
|
31
|
+
requester.user_agent
|
|
32
|
+
]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def user_content
|
|
36
|
+
[
|
|
37
|
+
current_user.send(ModelLog.config.identity_field)
|
|
38
|
+
]
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def action_content
|
|
42
|
+
[
|
|
43
|
+
action.upcase
|
|
44
|
+
]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def resource_content
|
|
48
|
+
[
|
|
49
|
+
resource.class.name,
|
|
50
|
+
resource.id,
|
|
51
|
+
changes
|
|
52
|
+
]
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require 'forwardable'
|
|
2
|
+
|
|
3
|
+
module ModelLog
|
|
4
|
+
module Helpers
|
|
5
|
+
module Context
|
|
6
|
+
extend Forwardable
|
|
7
|
+
|
|
8
|
+
def_delegators :@context, :current_user, :requester, :action, :resource, :changes
|
|
9
|
+
|
|
10
|
+
def initialize(context)
|
|
11
|
+
@context = context
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
protected
|
|
15
|
+
|
|
16
|
+
def is_update?
|
|
17
|
+
action == :update
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def is_create?
|
|
21
|
+
action == :create
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def is_destroy?
|
|
25
|
+
action == :destroy
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def changed?
|
|
29
|
+
!changes.empty?
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module ModelLog
|
|
2
|
+
module Log
|
|
3
|
+
class Content
|
|
4
|
+
include Initializer
|
|
5
|
+
|
|
6
|
+
def initialize(resource, action, formatter)
|
|
7
|
+
super(resource, action)
|
|
8
|
+
@formatter = formatter
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def content
|
|
12
|
+
@formatter.new(context).call
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def context
|
|
18
|
+
Context.new(
|
|
19
|
+
current_user: ModelLog.current_user,
|
|
20
|
+
requester: ModelLog.requester,
|
|
21
|
+
action: @action,
|
|
22
|
+
resource: @resource,
|
|
23
|
+
changes: Processor.new(@resource, @action).data
|
|
24
|
+
).to_struct
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module ModelLog
|
|
2
|
+
module Log
|
|
3
|
+
module Initializer
|
|
4
|
+
# {
|
|
5
|
+
# resource: object_active_record,
|
|
6
|
+
# action: create|update|destroy,
|
|
7
|
+
# }
|
|
8
|
+
def initialize(resource, action)
|
|
9
|
+
@resource = resource
|
|
10
|
+
@action = action
|
|
11
|
+
verify_action!
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
def verify_action!
|
|
17
|
+
unless %i(create destroy update).include? @action
|
|
18
|
+
raise StandardError, "invalid action given #{@action}"
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
module ModelLog
|
|
2
|
+
module Log
|
|
3
|
+
class Processor
|
|
4
|
+
include Initializer
|
|
5
|
+
|
|
6
|
+
def data
|
|
7
|
+
case @action
|
|
8
|
+
when :create
|
|
9
|
+
created_data
|
|
10
|
+
when :update
|
|
11
|
+
updated_data
|
|
12
|
+
when :destroy
|
|
13
|
+
destroyed_data
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def default_data
|
|
20
|
+
@resource.changes
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def created_data
|
|
24
|
+
# key => value
|
|
25
|
+
@resource.class.content_columns.map do |column|
|
|
26
|
+
[column.name, @resource.send(column.name)]
|
|
27
|
+
end.to_h
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
alias_method :destroyed_data, :created_data
|
|
31
|
+
|
|
32
|
+
def updated_data
|
|
33
|
+
# key => [before, after]
|
|
34
|
+
default_data
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
require "logger"
|
|
2
|
+
|
|
3
|
+
module ModelLog
|
|
4
|
+
class Logger < ::Logger
|
|
5
|
+
def initialize(filename)
|
|
6
|
+
super(File.join(::Rails.root, 'log', filename))
|
|
7
|
+
self.level = level
|
|
8
|
+
self.datetime_format = datetime_format
|
|
9
|
+
self
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
private
|
|
13
|
+
|
|
14
|
+
def level
|
|
15
|
+
ModelLog.config.logger_level
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def datetime_format
|
|
19
|
+
ModelLog.config.logger_datetime_format
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
module ModelLog
|
|
2
|
+
module Modeller
|
|
3
|
+
def self.included(base)
|
|
4
|
+
base.extend ClassMethods
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
module ClassMethods
|
|
8
|
+
def model_log
|
|
9
|
+
after_create :model_log_create
|
|
10
|
+
after_destroy :model_log_destroy
|
|
11
|
+
before_update :model_log_update
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def model_log_create
|
|
18
|
+
model_log_write(self, :create)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def model_log_update
|
|
22
|
+
model_log_write(self, :update)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def model_log_destroy
|
|
26
|
+
model_log_write(self, :destroy)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def model_log_write(resource, action)
|
|
30
|
+
# ModelLog.logger.debug resource.changes
|
|
31
|
+
# update changes is not empty
|
|
32
|
+
# create and destroy changes is empty
|
|
33
|
+
if log_content(resource, action)
|
|
34
|
+
ModelLog.logger.info log_content(resource, action)
|
|
35
|
+
end
|
|
36
|
+
rescue => ex
|
|
37
|
+
ModelLog.logger.error "#{ex.class}: #{ex.message}"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def log_content(resource, action)
|
|
41
|
+
@log_content ||= Log::Content.new(resource, action, ModelLog.config.formatter).content
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
ActiveRecord::Base.send(:include, ModelLog::Modeller) if defined? ActiveRecord::Base
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
module ModelLog
|
|
2
|
+
class Store
|
|
3
|
+
class << self
|
|
4
|
+
def store
|
|
5
|
+
Thread.current[:model_log_store] ||= {}
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def store=(store)
|
|
9
|
+
Thread.current[:model_log_store] = store
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def read(key)
|
|
13
|
+
store[key]
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def write(key, value)
|
|
17
|
+
store[key] = value
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def delete(key)
|
|
21
|
+
store.delete(key)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def current_user
|
|
25
|
+
read :current_user
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def current_user=(user)
|
|
29
|
+
write :current_user, user
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def clear_current_user!
|
|
33
|
+
delete :current_user
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def current_requester
|
|
37
|
+
read :requester
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def current_requester=(obj_request)
|
|
41
|
+
write :requester, obj_request
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def clear_current_requester!
|
|
45
|
+
delete :requester
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
data/lib/model_log/version.rb
CHANGED
data/lib/model_log.rb
CHANGED
|
@@ -1,22 +1,45 @@
|
|
|
1
1
|
require "model_log/version"
|
|
2
|
-
require "model_log/
|
|
3
|
-
require "model_log/
|
|
4
|
-
require "model_log/
|
|
5
|
-
require "model_log/
|
|
2
|
+
require "model_log/config"
|
|
3
|
+
require "model_log/store"
|
|
4
|
+
require "model_log/controller"
|
|
5
|
+
require "model_log/modeller"
|
|
6
|
+
require "model_log/logger"
|
|
7
|
+
require "model_log/context"
|
|
8
|
+
require "model_log/log/initializer"
|
|
9
|
+
require "model_log/log/content"
|
|
10
|
+
require "model_log/log/processor"
|
|
11
|
+
require "model_log/helpers/context"
|
|
12
|
+
require "model_log/default/formatter"
|
|
6
13
|
|
|
7
14
|
module ModelLog
|
|
8
15
|
class << self
|
|
9
|
-
|
|
16
|
+
def configure
|
|
17
|
+
yield config
|
|
18
|
+
end
|
|
10
19
|
|
|
11
|
-
def
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
20
|
+
def config
|
|
21
|
+
@_config ||= Config.new
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def logger
|
|
25
|
+
@_logger ||= Logger.new("model_log_#{::Rails.env}.log")
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def requester
|
|
29
|
+
Store.current_requester
|
|
16
30
|
end
|
|
17
|
-
end
|
|
18
31
|
|
|
19
|
-
|
|
20
|
-
|
|
32
|
+
def requester_exist?
|
|
33
|
+
!!requester
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def current_user
|
|
37
|
+
Store.current_user
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def current_user_exist?
|
|
41
|
+
!!current_user
|
|
42
|
+
end
|
|
43
|
+
end
|
|
21
44
|
end
|
|
22
45
|
|
data/model_log.gemspec
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
1
|
lib = File.expand_path("../lib", __FILE__)
|
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
3
|
require "model_log/version"
|
|
@@ -6,12 +5,15 @@ require "model_log/version"
|
|
|
6
5
|
Gem::Specification.new do |spec|
|
|
7
6
|
spec.name = "model_log"
|
|
8
7
|
spec.version = ModelLog::VERSION
|
|
9
|
-
spec.authors = ["
|
|
10
|
-
spec.email = ["
|
|
8
|
+
spec.authors = ["jk-sun"]
|
|
9
|
+
spec.email = ["jk-sun@qq.com"]
|
|
10
|
+
|
|
11
|
+
spec.summary = "Record all changes to the models into a log file for Rails"
|
|
12
|
+
spec.description = "It's designed to be used to record all changes to the models into a log file. You can also use it to record who made the changes"
|
|
13
|
+
|
|
14
|
+
spec.required_ruby_version = ">= 2.0.0"
|
|
11
15
|
|
|
12
|
-
spec.
|
|
13
|
-
spec.description = "User operation data saves log files and records user information."
|
|
14
|
-
spec.homepage = "https://bitbucket.org/scige/model_log"
|
|
16
|
+
spec.homepage = "https://github.com/JK-Sun/model_log"
|
|
15
17
|
spec.license = "MIT"
|
|
16
18
|
|
|
17
19
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
|
@@ -24,7 +26,8 @@ Gem::Specification.new do |spec|
|
|
|
24
26
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
25
27
|
spec.require_paths = ["lib"]
|
|
26
28
|
|
|
27
|
-
spec.add_development_dependency "bundler", "
|
|
28
|
-
spec.add_development_dependency "
|
|
29
|
-
spec.add_development_dependency "
|
|
29
|
+
spec.add_development_dependency "bundler", ">= 0"
|
|
30
|
+
#spec.add_development_dependency "bundler", "~> 1.16"
|
|
31
|
+
#spec.add_development_dependency "rake", "~> 10.0"
|
|
32
|
+
#spec.add_development_dependency "rspec", "~> 3.0"
|
|
30
33
|
end
|
metadata
CHANGED
|
@@ -1,83 +1,61 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: model_log
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 2.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
-
|
|
7
|
+
- jk-sun
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2019-12-28 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
|
16
16
|
requirements:
|
|
17
|
-
- - "
|
|
17
|
+
- - ">="
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
19
|
+
version: '0'
|
|
20
20
|
type: :development
|
|
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: '
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
|
30
|
-
requirements:
|
|
31
|
-
- - "~>"
|
|
32
|
-
- !ruby/object:Gem::Version
|
|
33
|
-
version: '10.0'
|
|
34
|
-
type: :development
|
|
35
|
-
prerelease: false
|
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
-
requirements:
|
|
38
|
-
- - "~>"
|
|
39
|
-
- !ruby/object:Gem::Version
|
|
40
|
-
version: '10.0'
|
|
41
|
-
- !ruby/object:Gem::Dependency
|
|
42
|
-
name: rspec
|
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
|
44
|
-
requirements:
|
|
45
|
-
- - "~>"
|
|
46
|
-
- !ruby/object:Gem::Version
|
|
47
|
-
version: '3.0'
|
|
48
|
-
type: :development
|
|
49
|
-
prerelease: false
|
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
-
requirements:
|
|
52
|
-
- - "~>"
|
|
53
|
-
- !ruby/object:Gem::Version
|
|
54
|
-
version: '3.0'
|
|
55
|
-
description: User operation data saves log files and records user information.
|
|
26
|
+
version: '0'
|
|
27
|
+
description: It's designed to be used to record all changes to the models into a log
|
|
28
|
+
file. You can also use it to record who made the changes
|
|
56
29
|
email:
|
|
57
|
-
-
|
|
30
|
+
- jk-sun@qq.com
|
|
58
31
|
executables: []
|
|
59
32
|
extensions: []
|
|
60
33
|
extra_rdoc_files: []
|
|
61
34
|
files:
|
|
62
35
|
- ".gitignore"
|
|
63
|
-
- ".rspec"
|
|
64
36
|
- ".travis.yml"
|
|
65
37
|
- CODE_OF_CONDUCT.md
|
|
66
38
|
- Gemfile
|
|
67
|
-
- Gemfile.lock
|
|
68
39
|
- LICENSE.txt
|
|
69
40
|
- README.md
|
|
70
41
|
- Rakefile
|
|
71
42
|
- bin/console
|
|
72
43
|
- bin/setup
|
|
73
44
|
- lib/model_log.rb
|
|
74
|
-
- lib/model_log/
|
|
75
|
-
- lib/model_log/
|
|
76
|
-
- lib/model_log/
|
|
77
|
-
- lib/model_log/
|
|
45
|
+
- lib/model_log/config.rb
|
|
46
|
+
- lib/model_log/context.rb
|
|
47
|
+
- lib/model_log/controller.rb
|
|
48
|
+
- lib/model_log/default/formatter.rb
|
|
49
|
+
- lib/model_log/helpers/context.rb
|
|
50
|
+
- lib/model_log/log/content.rb
|
|
51
|
+
- lib/model_log/log/initializer.rb
|
|
52
|
+
- lib/model_log/log/processor.rb
|
|
53
|
+
- lib/model_log/logger.rb
|
|
54
|
+
- lib/model_log/modeller.rb
|
|
55
|
+
- lib/model_log/store.rb
|
|
78
56
|
- lib/model_log/version.rb
|
|
79
57
|
- model_log.gemspec
|
|
80
|
-
homepage: https://
|
|
58
|
+
homepage: https://github.com/JK-Sun/model_log
|
|
81
59
|
licenses:
|
|
82
60
|
- MIT
|
|
83
61
|
metadata: {}
|
|
@@ -89,16 +67,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
89
67
|
requirements:
|
|
90
68
|
- - ">="
|
|
91
69
|
- !ruby/object:Gem::Version
|
|
92
|
-
version:
|
|
70
|
+
version: 2.0.0
|
|
93
71
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
72
|
requirements:
|
|
95
73
|
- - ">="
|
|
96
74
|
- !ruby/object:Gem::Version
|
|
97
75
|
version: '0'
|
|
98
76
|
requirements: []
|
|
99
|
-
|
|
100
|
-
rubygems_version: 2.6.14
|
|
77
|
+
rubygems_version: 3.0.6
|
|
101
78
|
signing_key:
|
|
102
79
|
specification_version: 4
|
|
103
|
-
summary:
|
|
80
|
+
summary: Record all changes to the models into a log file for Rails
|
|
104
81
|
test_files: []
|
data/.rspec
DELETED
data/Gemfile.lock
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
PATH
|
|
2
|
-
remote: .
|
|
3
|
-
specs:
|
|
4
|
-
model_log (1.0.2)
|
|
5
|
-
|
|
6
|
-
GEM
|
|
7
|
-
remote: https://rubygems.org/
|
|
8
|
-
specs:
|
|
9
|
-
diff-lcs (1.3)
|
|
10
|
-
rake (10.5.0)
|
|
11
|
-
rspec (3.7.0)
|
|
12
|
-
rspec-core (~> 3.7.0)
|
|
13
|
-
rspec-expectations (~> 3.7.0)
|
|
14
|
-
rspec-mocks (~> 3.7.0)
|
|
15
|
-
rspec-core (3.7.1)
|
|
16
|
-
rspec-support (~> 3.7.0)
|
|
17
|
-
rspec-expectations (3.7.0)
|
|
18
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
|
19
|
-
rspec-support (~> 3.7.0)
|
|
20
|
-
rspec-mocks (3.7.0)
|
|
21
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
|
22
|
-
rspec-support (~> 3.7.0)
|
|
23
|
-
rspec-support (3.7.1)
|
|
24
|
-
|
|
25
|
-
PLATFORMS
|
|
26
|
-
ruby
|
|
27
|
-
|
|
28
|
-
DEPENDENCIES
|
|
29
|
-
bundler (~> 1.16)
|
|
30
|
-
model_log!
|
|
31
|
-
rake (~> 10.0)
|
|
32
|
-
rspec (~> 3.0)
|
|
33
|
-
|
|
34
|
-
BUNDLED WITH
|
|
35
|
-
1.16.1
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
module ModelLog
|
|
2
|
-
module ClassMethods
|
|
3
|
-
module Controllers
|
|
4
|
-
def self.included(base)
|
|
5
|
-
base.class_eval do
|
|
6
|
-
extend Invoking
|
|
7
|
-
model_log_user_setting
|
|
8
|
-
end
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
module Invoking
|
|
12
|
-
def model_log_user_setting
|
|
13
|
-
before_action :set_current_passport
|
|
14
|
-
before_action :set_current_request
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
private
|
|
19
|
-
|
|
20
|
-
def set_current_passport
|
|
21
|
-
ModelLog::Passport.clear_current_passport!
|
|
22
|
-
ModelLog::Passport.current_passport = send(ModelLog.current_user_method)
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def set_current_request
|
|
26
|
-
ModelLog::Request.clear_current_request!
|
|
27
|
-
ModelLog::Request.current_request = request
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
ActionController::Base.send(:include, ModelLog::ClassMethods::Controllers) if defined? ActionController::Base
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
module ModelLog
|
|
2
|
-
module ClassMethods
|
|
3
|
-
module Models
|
|
4
|
-
def self.included(base)
|
|
5
|
-
base.extend Invoking
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
module Invoking
|
|
9
|
-
def model_log
|
|
10
|
-
after_create :model_log_create
|
|
11
|
-
after_destroy :model_log_destroy
|
|
12
|
-
before_update :model_log_update
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
private
|
|
17
|
-
|
|
18
|
-
def model_log_create
|
|
19
|
-
write_log(self, :create)
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
def model_log_update
|
|
23
|
-
write_log(self, :update)
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
def model_log_destroy
|
|
27
|
-
write_log(self, :destroy)
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
def data_processing(field_name, field_data, **options)
|
|
31
|
-
if defined? CarrierWave::Uploader::Base and field_data.is_a? CarrierWave::Uploader::Base
|
|
32
|
-
return options[:index] == 0 ? get_uploader_filename(field_data.url) : send("#{field_name}_identifier")
|
|
33
|
-
elsif field_data.is_a? BigDecimal
|
|
34
|
-
return field_data.to_f
|
|
35
|
-
else
|
|
36
|
-
return field_data
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
def get_uploader_filename(path)
|
|
41
|
-
if path
|
|
42
|
-
rindex = path.rindex('/')
|
|
43
|
-
return path.slice(rindex+1, path.size-rindex-1)
|
|
44
|
-
end
|
|
45
|
-
return nil
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
def write_log(resource, type)
|
|
49
|
-
passport = ModelLog::Passport.current_passport
|
|
50
|
-
request = ModelLog::Request.current_request
|
|
51
|
-
type = type.to_s
|
|
52
|
-
can_write = true
|
|
53
|
-
content_text = {}
|
|
54
|
-
|
|
55
|
-
if type == "update"
|
|
56
|
-
changes = resource.changes
|
|
57
|
-
changes.each do |key, values|
|
|
58
|
-
value_arr = []
|
|
59
|
-
values.each_with_index do |value, i|
|
|
60
|
-
value_arr << data_processing(key, value, :index => i)
|
|
61
|
-
end
|
|
62
|
-
content_text.merge!(Hash[key, value_arr])
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
can_write = false if content_text.empty?
|
|
66
|
-
else
|
|
67
|
-
resource.class.content_columns.each do |column|
|
|
68
|
-
column_name = column.name
|
|
69
|
-
column_val = data_processing(column_name, resource.send(column_name))
|
|
70
|
-
content_text.merge!(Hash[column_name, column_val])
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
file_content = <<-EFO.strip_heredoc.gsub("\n", "\t")
|
|
75
|
-
#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}
|
|
76
|
-
#{request.try(:request_method)}
|
|
77
|
-
#{request.try(:url)}
|
|
78
|
-
#{request.try(:referer)}
|
|
79
|
-
#{request.try(:remote_ip)}
|
|
80
|
-
#{request.try(:user_agent)}
|
|
81
|
-
#{ModelLog.platform}
|
|
82
|
-
#{passport.try(:id)}
|
|
83
|
-
#{passport.try(ModelLog.user_name_field)}
|
|
84
|
-
#{type}
|
|
85
|
-
#{resource.class}
|
|
86
|
-
#{resource.id}
|
|
87
|
-
#{content_text}
|
|
88
|
-
EFO
|
|
89
|
-
|
|
90
|
-
ModelLog.info file_content if can_write
|
|
91
|
-
rescue => e
|
|
92
|
-
ModelLog.info e.message
|
|
93
|
-
end
|
|
94
|
-
end
|
|
95
|
-
end
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
ActiveRecord::Base.send(:include, ModelLog::ClassMethods::Models) if defined? ActiveRecord::Base
|
|
99
|
-
|
data/lib/model_log/passport.rb
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
module ModelLog
|
|
2
|
-
module Passport
|
|
3
|
-
class << self
|
|
4
|
-
def current_passport
|
|
5
|
-
Thread.current[:passport]
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
def current_passport=(value)
|
|
9
|
-
Thread.current[:passport] = value
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
def clear_current_passport!
|
|
13
|
-
Thread.current[:passport] = nil
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
|
data/lib/model_log/request.rb
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
module ModelLog
|
|
2
|
-
module Request
|
|
3
|
-
class << self
|
|
4
|
-
def current_request
|
|
5
|
-
Thread.current[:request]
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
def current_request=(value)
|
|
9
|
-
Thread.current[:request] = value
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
def clear_current_request!
|
|
13
|
-
Thread.current[:request] = nil
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
end
|