kushojin 0.1.1 → 0.1.2
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/.rubocop.yml +9 -2
- data/README.md +44 -0
- data/kushojin.gemspec +4 -4
- data/lib/kushojin/model_methods/callback.rb +75 -3
- data/lib/kushojin/model_methods/record_changes_callbacks.rb +1 -1
- data/lib/kushojin/version.rb +1 -1
- metadata +17 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 06f7c8d3b7b0fe28792b4c01bc6af184a25cca40b5c70ab0f2916acc3d696b6a
|
4
|
+
data.tar.gz: 59b0051cf2b0d30e5d6f468621fcb8719e35cc167eca2774cf638ff5a2cc97f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90c9302c8beca1dcbefff47f30fe97713ce31e232480fcb2718817efa775d696745b9de15c0242330035ade1fc78a81ccd21129264adae27f503df35098f3e52
|
7
|
+
data.tar.gz: e7626085b763c4c2ec0ca83bd68da28c0fb4484eeada3ee56150d595720b60a38bef0e7601fac96268336efb9008acec7c2d52129ba2d24301460f060004db33
|
data/.rubocop.yml
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
AllCops:
|
2
|
-
TargetRubyVersion: 2.
|
2
|
+
TargetRubyVersion: 2.5
|
3
3
|
DisplayCopNames: true
|
4
4
|
Exclude:
|
5
5
|
- 'kushojin.gemspec'
|
@@ -16,12 +16,19 @@ Style/StringLiterals:
|
|
16
16
|
Style/TrailingCommaInArguments:
|
17
17
|
EnforcedStyleForMultiline: comma
|
18
18
|
|
19
|
-
Style/
|
19
|
+
Style/TrailingCommaInArrayLiteral:
|
20
|
+
EnforcedStyleForMultiline: comma
|
21
|
+
|
22
|
+
Style/TrailingCommaInHashLiteral:
|
20
23
|
EnforcedStyleForMultiline: comma
|
21
24
|
|
22
25
|
Layout/MultilineMethodCallIndentation:
|
23
26
|
EnforcedStyle: indented
|
24
27
|
|
28
|
+
Layout/AlignHash:
|
29
|
+
EnforcedHashRocketStyle: table
|
30
|
+
EnforcedColonStyle: table
|
31
|
+
|
25
32
|
Metrics/BlockLength:
|
26
33
|
Exclude:
|
27
34
|
- 'spec/**/*'
|
data/README.md
CHANGED
@@ -39,6 +39,50 @@ end
|
|
39
39
|
$ curl -X POST -d "user[name]=bill&user[age]=20" http://localhost:3000/users
|
40
40
|
# output: users.create {"event":"create","request_id":"4afd0731-dd25-4668-b769-2017dbdd3642","table_name":"users","id":1,"changes":{"name":[null,"bill"],"age":[null,20]}}
|
41
41
|
|
42
|
+
Changes is recorded when the model is created, updated and destroyed.
|
43
|
+
The `:only` option can be used same as filters of controller.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
class User < ApplicationRecord
|
47
|
+
record_changes only: [:create, :destroy]
|
48
|
+
end
|
49
|
+
```
|
50
|
+
$ curl -X POST -d "user[name]=bill&user[age]=20" http://localhost:3000/users
|
51
|
+
# output: users.create {"event":"create","request_id":"4afd0731-dd25-4668-b769-2017dbdd3642","table_name":"users","id":1,"changes":{"name":[null,"bill"],"age":[null,20]}}
|
52
|
+
|
53
|
+
$ curl -X PATCH -d "user[age]=21" http://localhost:3000/users/1
|
54
|
+
# no output
|
55
|
+
|
56
|
+
Custom callback object can be used with `:callbacks` option.
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
class CustomCallbacks
|
60
|
+
# Must respond to after_create, after_update, and before_destroy.
|
61
|
+
def after_create(record); end
|
62
|
+
def after_update(record); end
|
63
|
+
def before_destroy(record); end
|
64
|
+
end
|
65
|
+
|
66
|
+
class User < ApplicationRecord
|
67
|
+
record_changes callbacks: CustomCallbacks.new
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
### Override
|
72
|
+
|
73
|
+
You can override options of Recording changes in subclass.
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
class ApplicationRecord < ActiveRecord::Base
|
77
|
+
self.abstract_class = true
|
78
|
+
record_changes
|
79
|
+
end
|
80
|
+
|
81
|
+
class User < ApplicationRecord
|
82
|
+
record_changes only: [:create, :destroy]
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
42
86
|
## Development
|
43
87
|
|
44
88
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/kushojin.gemspec
CHANGED
@@ -24,10 +24,10 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.add_dependency "activerecord", "~> 5.1"
|
25
25
|
spec.add_dependency "fluent-logger"
|
26
26
|
|
27
|
-
spec.add_development_dependency "bundler"
|
28
|
-
spec.add_development_dependency "rake"
|
29
|
-
spec.add_development_dependency "rspec", "
|
30
|
-
spec.add_development_dependency "sqlite3"
|
27
|
+
spec.add_development_dependency "bundler"
|
28
|
+
spec.add_development_dependency "rake"
|
29
|
+
spec.add_development_dependency "rspec", ">= 3.0"
|
30
|
+
spec.add_development_dependency "sqlite3"
|
31
31
|
spec.add_development_dependency "pry-byebug"
|
32
32
|
spec.add_development_dependency "rubocop"
|
33
33
|
end
|
@@ -4,12 +4,84 @@ module Kushojin
|
|
4
4
|
RECORD_EVENTS = %i[before_destroy after_create after_update].freeze
|
5
5
|
|
6
6
|
module ClassMethods
|
7
|
-
def
|
8
|
-
kushojin_callbacks
|
9
|
-
|
7
|
+
def self.extended(klass)
|
8
|
+
klass.class_attribute :kushojin_callbacks, instance_accessor: false
|
9
|
+
end
|
10
|
+
|
11
|
+
# Record changes of the ActiveRecord model.
|
12
|
+
#
|
13
|
+
# class User < ApplicationRecord
|
14
|
+
# record_changes
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# Changes is recorded when the model is created, updated and destroyed.
|
18
|
+
# The +:only+ option can be used same as filters of controller.
|
19
|
+
#
|
20
|
+
# record_changes only: [:create, :destroy]
|
21
|
+
#
|
22
|
+
# Custom callback object can be used with +:callbacks+ option.
|
23
|
+
#
|
24
|
+
# class CustomCallbacks
|
25
|
+
# # Must respond to after_create, after_update, and before_destroy.
|
26
|
+
# def after_create(record); end
|
27
|
+
# def after_update(record); end
|
28
|
+
# def before_destroy(record); end
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# class User < ApplicationRecord
|
32
|
+
# record_changes callbacks: CustomCallbacks.new
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# Both +:only+ and +:callbacks+ option can be overrided by subclass.
|
36
|
+
#
|
37
|
+
# class ApplicationRecord < ActiveRecord::Base
|
38
|
+
# self.abstract_class = true
|
39
|
+
# record_changes
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# class User < ApplicationRecord
|
43
|
+
# record_changes only: [:create, :destroy], callbacks: CustomCallbacks.new
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# ===== Options
|
47
|
+
#
|
48
|
+
# * <tt>only</tt> - Records only for this event.
|
49
|
+
# Support event is +:create+, +:update+, and +:destroy+.
|
50
|
+
# * <tt>callbacks</tt> - Use callback object instead of RecordChangesCallbacks.
|
51
|
+
#
|
52
|
+
def record_changes(only: [], callbacks: nil)
|
53
|
+
if kushojin_callbacks
|
54
|
+
remove_callbacks
|
55
|
+
self.kushojin_callbacks = callbacks if callbacks
|
56
|
+
else
|
57
|
+
self.kushojin_callbacks = callbacks || RecordChangesCallbacks.new
|
58
|
+
end
|
59
|
+
|
60
|
+
record_events = convert_to_record_events(only)
|
61
|
+
record_events.each do |event|
|
10
62
|
public_send(event, kushojin_callbacks)
|
11
63
|
end
|
12
64
|
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def remove_callbacks
|
69
|
+
kind_and_names =
|
70
|
+
RECORD_EVENTS.map { |event| event.to_s.split("_", 2).map(&:to_sym) }
|
71
|
+
|
72
|
+
kind_and_names.each do |kind, name|
|
73
|
+
skip_callback(name, kind, kushojin_callbacks, raise: false)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def convert_to_record_events(events) # :nodoc:
|
78
|
+
if events.empty?
|
79
|
+
RECORD_EVENTS
|
80
|
+
else
|
81
|
+
event_strs = Array(events).map(&:to_s)
|
82
|
+
RECORD_EVENTS.select { |event| event_strs.include?(event.to_s.sub(/^\w*_/, "")) }
|
83
|
+
end
|
84
|
+
end
|
13
85
|
end
|
14
86
|
end
|
15
87
|
end
|
@@ -3,7 +3,7 @@ module Kushojin
|
|
3
3
|
class RecordChangesCallbacks
|
4
4
|
Callback::RECORD_EVENTS.each do |event|
|
5
5
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
6
|
-
def #{event}(model) # def after_create
|
6
|
+
def #{event}(model) # def after_create(model)
|
7
7
|
Recorder.record(:#{event.to_s.split(/_/).last}, model) if Recorder.current # Recorder.record(:create, model) if Recorder.current
|
8
8
|
end # end
|
9
9
|
RUBY
|
data/lib/kushojin/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kushojin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Loose coupling
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-11-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -42,58 +42,58 @@ dependencies:
|
|
42
42
|
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '3.0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '3.0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: sqlite3
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- - "
|
87
|
+
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
89
|
+
version: '0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- - "
|
94
|
+
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
96
|
+
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: pry-byebug
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -177,7 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
177
177
|
version: '0'
|
178
178
|
requirements: []
|
179
179
|
rubyforge_project:
|
180
|
-
rubygems_version: 2.6
|
180
|
+
rubygems_version: 2.7.6
|
181
181
|
signing_key:
|
182
182
|
specification_version: 4
|
183
183
|
summary: Detect changes of model attributes and send to outside.
|