model_log 2.0.0 → 2.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f536ddd5bcec92fad2368e4e637fd678620ca82cfe38438cbdc434a36c6da913
4
- data.tar.gz: 7a597e5c6e17e43da418c1b5c3e37bdb733e8b0f1b6e1af35c73fd00c4598130
3
+ metadata.gz: a91de84ee8086e12de0c20d29185423e70bae67dce1817914bd61ff25ba49430
4
+ data.tar.gz: 5f4673b8501238e256fa379ec279dc626372eef84d17c860054854401ea533e8
5
5
  SHA512:
6
- metadata.gz: f564bfb542098de5b2aedeb67de68db8d6ccbe6077a05e657d44dd39f6ac5bfc1f75a8efeeb78a27e51e1418981bd403ba8edab4e9605413a523c36b671f54a0
7
- data.tar.gz: 6b1780b79d240b9edcfd0e05777f20f5bba2d6295e5a0b3b8801ac069ab5919bd54fa15ceb06dfc05080c3a145ad5fb6cc7296a98efad15dfa8a3e1f6d56cbad
6
+ metadata.gz: 71b7b4458b7de39674a4e435cd202c8b6d5a24083bd2c6ae97e85e35c6d351cd9453db66758404bc2896141fb041b62dedfd5db23cccb23463c77bd02d09a444
7
+ data.tar.gz: c615dd14cd292bd9755110ffc13fbab8a11080617a490cef0232084581d3ce6488a4e935b4a20553d9faaa879fa4afe135d6f841b511b7146059c62bc2eef9b4
@@ -0,0 +1,38 @@
1
+ ---
2
+ name: Bug report
3
+ about: Create a report to help us improve
4
+ title: ''
5
+ labels: ''
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Describe the bug**
11
+ A clear and concise description of what the bug is.
12
+
13
+ **To Reproduce**
14
+ Steps to reproduce the behavior:
15
+ 1. Go to '...'
16
+ 2. Click on '....'
17
+ 3. Scroll down to '....'
18
+ 4. See error
19
+
20
+ **Expected behavior**
21
+ A clear and concise description of what you expected to happen.
22
+
23
+ **Screenshots**
24
+ If applicable, add screenshots to help explain your problem.
25
+
26
+ **Desktop (please complete the following information):**
27
+ - OS: [e.g. iOS]
28
+ - Browser [e.g. chrome, safari]
29
+ - Version [e.g. 22]
30
+
31
+ **Smartphone (please complete the following information):**
32
+ - Device: [e.g. iPhone6]
33
+ - OS: [e.g. iOS8.1]
34
+ - Browser [e.g. stock browser, safari]
35
+ - Version [e.g. 22]
36
+
37
+ **Additional context**
38
+ Add any other context about the problem here.
@@ -0,0 +1,20 @@
1
+ ---
2
+ name: Feature request
3
+ about: Suggest an idea for this project
4
+ title: ''
5
+ labels: ''
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Is your feature request related to a problem? Please describe.**
11
+ A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12
+
13
+ **Describe the solution you'd like**
14
+ A clear and concise description of what you want to happen.
15
+
16
+ **Describe alternatives you've considered**
17
+ A clear and concise description of any alternative solutions or features you've considered.
18
+
19
+ **Additional context**
20
+ Add any other context or screenshots about the feature request here.
data/.gitignore CHANGED
@@ -6,8 +6,6 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
- /spec/
10
- /.rspec
11
9
  /Gemfile.lock
12
10
 
13
11
  # rspec failure tracking
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in model_log.gemspec
6
6
  gemspec
data/README.md CHANGED
@@ -48,7 +48,7 @@ To use a method other than `current_user` and an identity field other than `id`,
48
48
  ModelLog.configure do |config|
49
49
  config.current_user_method = :current_manager # default: :current_user
50
50
  config.identity_field = :username # default: :id
51
- config.separator = ' ' # default: "\t"
51
+ config.separator = "\t" # default: ' '
52
52
  config.logger_datetime_format = '%Y-%m-%d %H:%M:%S'
53
53
  end
54
54
  ```
@@ -111,4 +111,4 @@ end
111
111
 
112
112
  ## Copyright
113
113
 
114
- Copyright (c) 2018- jk-sun. See LICENSE.tet for further details.
114
+ Copyright (c) 2018- jk-sun. See LICENSE.txt for further details.
data/Rakefile CHANGED
@@ -1,3 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
2
5
 
3
6
  task :default => :spec
data/lib/model_log.rb CHANGED
@@ -1,17 +1,30 @@
1
- require "model_log/version"
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"
1
+ require 'active_support'
2
+ require 'active_support/core_ext/object/blank'
3
+
4
+ require 'model_log/version'
13
5
 
14
6
  module ModelLog
7
+ extend ActiveSupport::Autoload
8
+
9
+ autoload :Config, 'model_log/config'
10
+ autoload :Store, 'model_log/store'
11
+ autoload :Logger, 'model_log/logger'
12
+ autoload :Context, 'model_log/context'
13
+
14
+ module Log
15
+ autoload :Initializer, 'model_log/log/initializer'
16
+ autoload :Content, 'model_log/log/content'
17
+ autoload :Processor, 'model_log/log/processor'
18
+ end
19
+
20
+ module Default
21
+ autoload :Formatter, 'model_log/default/formatter'
22
+ end
23
+
24
+ module Helpers
25
+ autoload :Context, 'model_log/helpers/context'
26
+ end
27
+
15
28
  class << self
16
29
  def configure
17
30
  yield config
@@ -26,7 +39,7 @@ module ModelLog
26
39
  end
27
40
 
28
41
  def requester
29
- Store.current_requester
42
+ Store.requester
30
43
  end
31
44
 
32
45
  def requester_exist?
@@ -40,6 +53,12 @@ module ModelLog
40
53
  def current_user_exist?
41
54
  !!current_user
42
55
  end
56
+
57
+ def version
58
+ VERSION::STRING
59
+ end
43
60
  end
44
61
  end
45
62
 
63
+ require 'model_log/controller'
64
+ require 'model_log/modeller'
@@ -6,7 +6,7 @@ module ModelLog
6
6
  def initialize
7
7
  @current_user_method ||= :current_user
8
8
  @identity_field ||= :id
9
- @separator ||= "\t"
9
+ @separator ||= ' '
10
10
  @formatter ||= Default::Formatter
11
11
  @logger_level ||= Logger::INFO # DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
12
12
  end
@@ -1,13 +1,11 @@
1
1
  module ModelLog
2
- module Log
3
- class Context
4
- def initialize(context)
5
- @context = context
6
- end
2
+ class Context
3
+ def initialize(context)
4
+ @context = context
5
+ end
7
6
 
8
- def to_struct
9
- Struct.new(*@context.keys).new(*@context.values)
10
- end
7
+ def to_struct
8
+ Struct.new(*@context.keys).new(*@context.values)
11
9
  end
12
10
  end
13
11
  end
@@ -10,25 +10,25 @@ module ModelLog
10
10
  module ClassMethods
11
11
  def set_model_log_stroe
12
12
  if ::Rails::VERSION::MAJOR > 3
13
- before_action :set_current_user
14
- before_action :set_current_requester
13
+ before_action :set_model_log_current_user
14
+ before_action :set_model_log_requester
15
15
  else
16
- before_filter :set_current_user
17
- before_filter :set_current_requester
16
+ before_filter :set_model_log_current_user
17
+ before_filter :set_model_log_requester
18
18
  end
19
19
  end
20
20
  end
21
21
 
22
22
  private
23
23
 
24
- def set_current_user
24
+ def set_model_log_current_user
25
25
  Store.clear_current_user!
26
26
  Store.current_user = send(ModelLog.config.current_user_method) rescue nil
27
27
  end
28
28
 
29
- def set_current_requester
30
- Store.clear_current_requester!
31
- Store.current_requester = request.is_a?(ActionDispatch::Request) && request rescue nil
29
+ def set_model_log_requester
30
+ Store.clear_requester!
31
+ Store.requester = request.is_a?(ActionDispatch::Request) && request rescue nil
32
32
  end
33
33
  end
34
34
  end
@@ -18,9 +18,9 @@ module ModelLog
18
18
  Context.new(
19
19
  current_user: ModelLog.current_user,
20
20
  requester: ModelLog.requester,
21
- action: @action,
22
- resource: @resource,
23
- changes: Processor.new(@resource, @action).data
21
+ action: action,
22
+ resource: resource,
23
+ changes: Processor.new(resource, action).data
24
24
  ).to_struct
25
25
  end
26
26
  end
@@ -1,21 +1,31 @@
1
1
  module ModelLog
2
2
  module Log
3
3
  module Initializer
4
- # {
5
- # resource: object_active_record,
6
- # action: create|update|destroy,
7
- # }
4
+ attr_reader :resource, :changes, :attributes, :changed_attributes, :action
5
+
6
+ # params
7
+ # resource: object_active_record
8
+ # action: create|update|destroy
8
9
  def initialize(resource, action)
9
- @resource = resource
10
- @action = action
10
+ @action = action
11
11
  verify_action!
12
+
13
+ @resource = resource
14
+ @changes = resource.changes
15
+ @changed_attributes = resource.changed_attributes
16
+ @attributes = resource.attributes
17
+ @is_changed = resource.changed?
18
+ end
19
+
20
+ def changd?
21
+ @is_changed
12
22
  end
13
23
 
14
24
  private
15
25
 
16
26
  def verify_action!
17
- unless %i(create destroy update).include? @action
18
- raise StandardError, "invalid action given #{@action}"
27
+ unless %i(create destroy update).include? action
28
+ raise StandardError, "invalid action given #{action}"
19
29
  end
20
30
  end
21
31
  end
@@ -4,7 +4,7 @@ module ModelLog
4
4
  include Initializer
5
5
 
6
6
  def data
7
- case @action
7
+ case action
8
8
  when :create
9
9
  created_data
10
10
  when :update
@@ -16,22 +16,17 @@ module ModelLog
16
16
 
17
17
  private
18
18
 
19
- def default_data
20
- @resource.changes
19
+ def saved_data
20
+ changes
21
21
  end
22
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
23
+ alias_method :created_data, :saved_data
24
+ alias_method :updated_data, :saved_data
31
25
 
32
- def updated_data
33
- # key => [before, after]
34
- default_data
26
+ def destroyed_data
27
+ data = attributes
28
+ data = data.merge(changed_attributes) if changd?
29
+ data.map { |attr, value| [attr, [value, nil]] }.to_h
35
30
  end
36
31
  end
37
32
  end
@@ -9,6 +9,12 @@ module ModelLog
9
9
  self
10
10
  end
11
11
 
12
+ %i(debug info warn error fatal unknown).each do |method|
13
+ define_method method do |msg|
14
+ super(msg) if msg.present?
15
+ end
16
+ end
17
+
12
18
  private
13
19
 
14
20
  def level
@@ -6,9 +6,9 @@ module ModelLog
6
6
 
7
7
  module ClassMethods
8
8
  def model_log
9
- after_create :model_log_create
10
- after_destroy :model_log_destroy
9
+ before_create :model_log_create
11
10
  before_update :model_log_update
11
+ after_destroy :model_log_destroy
12
12
  end
13
13
  end
14
14
 
@@ -27,19 +27,10 @@ module ModelLog
27
27
  end
28
28
 
29
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
30
+ ModelLog.logger.info Log::Content.new(resource, action, ModelLog.config.formatter).content
36
31
  rescue => ex
37
32
  ModelLog.logger.error "#{ex.class}: #{ex.message}"
38
33
  end
39
-
40
- def log_content(resource, action)
41
- @log_content ||= Log::Content.new(resource, action, ModelLog.config.formatter).content
42
- end
43
34
  end
44
35
  end
45
36
 
@@ -33,15 +33,15 @@ module ModelLog
33
33
  delete :current_user
34
34
  end
35
35
 
36
- def current_requester
36
+ def requester
37
37
  read :requester
38
38
  end
39
39
 
40
- def current_requester=(obj_request)
41
- write :requester, obj_request
40
+ def requester=(request)
41
+ write :requester, request
42
42
  end
43
43
 
44
- def clear_current_requester!
44
+ def clear_requester!
45
45
  delete :requester
46
46
  end
47
47
  end
@@ -1,3 +1,16 @@
1
1
  module ModelLog
2
- VERSION = "2.0.0"
2
+ module VERSION
3
+ MAJOR = 2
4
+ MINOR = 0
5
+ TINY = 1
6
+
7
+ # Set PRE to nil unless it's a pre-release (beta, rc, etc.)
8
+ PRE = nil
9
+
10
+ STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".").freeze
11
+
12
+ def self.to_s
13
+ STRING
14
+ end
15
+ end
3
16
  end
data/model_log.gemspec CHANGED
@@ -4,7 +4,7 @@ require "model_log/version"
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "model_log"
7
- spec.version = ModelLog::VERSION
7
+ spec.version = ModelLog::VERSION::STRING
8
8
  spec.authors = ["jk-sun"]
9
9
  spec.email = ["jk-sun@qq.com"]
10
10
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_log
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - jk-sun
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-28 00:00:00.000000000 Z
11
+ date: 2020-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -32,7 +32,10 @@ executables: []
32
32
  extensions: []
33
33
  extra_rdoc_files: []
34
34
  files:
35
+ - ".github/ISSUE_TEMPLATE/bug_report.md"
36
+ - ".github/ISSUE_TEMPLATE/feature_request.md"
35
37
  - ".gitignore"
38
+ - ".rspec"
36
39
  - ".travis.yml"
37
40
  - CODE_OF_CONDUCT.md
38
41
  - Gemfile