kushojin 0.1.1 → 0.1.6
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/.github/workflows/CI.yml +43 -0
- data/.github/workflows/release_gem.yml +25 -0
- data/.gitignore +4 -0
- data/.rubocop.yml +9 -2
- data/.travis.yml +7 -2
- data/Appraisals +17 -0
- data/README.md +92 -2
- data/bin/console +4 -4
- data/gemfiles/rails_5.2.gemfile +9 -0
- data/gemfiles/rails_6.0.gemfile +9 -0
- data/gemfiles/rails_6.1.gemfile +9 -0
- data/kushojin.gemspec +8 -5
- data/lib/kushojin/controller_methods.rb +3 -3
- data/lib/kushojin/controller_methods/callback.rb +53 -0
- data/lib/kushojin/controller_methods/{send_change_filter.rb → send_change_callback.rb} +1 -1
- data/lib/kushojin/model_methods/callback.rb +64 -4
- data/lib/kushojin/model_methods/change.rb +44 -4
- data/lib/kushojin/model_methods/record_changes_callbacks.rb +15 -3
- data/lib/kushojin/recorder.rb +2 -2
- data/lib/kushojin/sender/base.rb +2 -2
- data/lib/kushojin/sender/each_sender.rb +6 -12
- data/lib/kushojin/sender/serializer/base.rb +1 -3
- data/lib/kushojin/sender/serializer/simple_serializer.rb +1 -2
- data/lib/kushojin/version.rb +1 -1
- metadata +71 -24
- data/lib/kushojin/controller_methods/filter.rb +0 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 50ce9f3809b8165534b824fd3d91778f3b8e005fd89dbb860edcaba4b24beab2
|
4
|
+
data.tar.gz: b93e433a3cb8129d0660d371e4be2e52ebc8920b04a2ab2af5e8e94e0f4f38f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b11796a056a93a1db3adf9e543efd1e794cba574f49598001ea619eeb64c26bf3d299961171395d798b8598ec8c2ec6588967d40e0c714cea7141240598a2999
|
7
|
+
data.tar.gz: 6d42255e6dfc6464c2bad8638e94446c5b99d64523438944a656d7f242207a1984b894ee62a95b389350a7403c4839c6a6595b2113010a1c957477169aed51da
|
@@ -0,0 +1,43 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on: [push]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
|
9
|
+
strategy:
|
10
|
+
matrix:
|
11
|
+
ruby:
|
12
|
+
- 2.5.x
|
13
|
+
- 2.6.x
|
14
|
+
|
15
|
+
gemfile:
|
16
|
+
- rails_5.2.gemfile
|
17
|
+
- rails_6.0.gemfile
|
18
|
+
- rails_6.1.gemfile
|
19
|
+
|
20
|
+
steps:
|
21
|
+
- uses: actions/checkout@v1
|
22
|
+
- name: Set up Ruby
|
23
|
+
uses: actions/setup-ruby@v1
|
24
|
+
with:
|
25
|
+
ruby-version: ${{ matrix.ruby }}
|
26
|
+
|
27
|
+
- name: Build and test with Rake
|
28
|
+
env:
|
29
|
+
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}
|
30
|
+
run: |
|
31
|
+
sudo apt-get update
|
32
|
+
sudo apt-get install -y libsqlite3-dev
|
33
|
+
gem install bundler
|
34
|
+
bundle install --jobs 4 --retry 3
|
35
|
+
bundle exec rspec
|
36
|
+
|
37
|
+
- name: Test & publish code coverage
|
38
|
+
uses: paambaati/codeclimate-action@v2.7.5
|
39
|
+
env:
|
40
|
+
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
|
41
|
+
COVERAGE: true
|
42
|
+
with:
|
43
|
+
coverageCommand: bundle exec rspec
|
@@ -0,0 +1,25 @@
|
|
1
|
+
name: Release a gem
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- master
|
7
|
+
|
8
|
+
jobs:
|
9
|
+
build:
|
10
|
+
name: Build + Publish
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
|
13
|
+
steps:
|
14
|
+
- uses: actions/checkout@master
|
15
|
+
|
16
|
+
- name: Set up Ruby 2.6
|
17
|
+
uses: actions/setup-ruby@v1
|
18
|
+
with:
|
19
|
+
version: 2.6.x
|
20
|
+
|
21
|
+
- name: Create a tag and release to rubygems.org
|
22
|
+
uses: nowlinuxing/action-release-gem@v1
|
23
|
+
env:
|
24
|
+
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
25
|
+
GEM_HOST_API_KEY: ${{secrets.RUBYGEMS_AUTH_TOKEN}}
|
data/.gitignore
CHANGED
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/.travis.yml
CHANGED
data/Appraisals
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
appraise "rails-5.2" do
|
2
|
+
gem "activerecord", "~> 5.2.2"
|
3
|
+
gem "actionpack", "~> 5.2.2"
|
4
|
+
gem "sqlite3", "~> 1.3.6"
|
5
|
+
end
|
6
|
+
|
7
|
+
appraise "rails-6.0" do
|
8
|
+
gem "activerecord", "~> 6.0.0"
|
9
|
+
gem "actionpack", "~> 6.0.0"
|
10
|
+
gem "sqlite3", "~> 1.4"
|
11
|
+
end
|
12
|
+
|
13
|
+
appraise "rails-6.1" do
|
14
|
+
gem "activerecord", "~> 6.1.0"
|
15
|
+
gem "actionpack", "~> 6.1.0"
|
16
|
+
gem "sqlite3", "~> 1.4"
|
17
|
+
end
|
data/README.md
CHANGED
@@ -1,6 +1,13 @@
|
|
1
|
-
# Kushojin
|
1
|
+
# Kushojin
|
2
2
|
|
3
|
-
|
3
|
+
[](https://badge.fury.io/rb/kushojin)
|
4
|
+
[](https://github.com/nowlinuxing/kushojin/actions)
|
5
|
+
[](https://codeclimate.com/github/nowlinuxing/kushojin/maintainability)
|
6
|
+
[](https://codeclimate.com/github/nowlinuxing/kushojin/test_coverage)
|
7
|
+
|
8
|
+
Kushojin gathers changes to the attributes of the ActiveRecord model and sends them externally via Fluentd.
|
9
|
+
This is useful for logging, tracking and real-time aggregation of accesses involving database updates.
|
10
|
+
Since Fluentd is used for external transmission, Kushojin can respond flexibly to various requests.
|
4
11
|
|
5
12
|
## Installation
|
6
13
|
|
@@ -39,6 +46,89 @@ end
|
|
39
46
|
$ curl -X POST -d "user[name]=bill&user[age]=20" http://localhost:3000/users
|
40
47
|
# 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
48
|
|
49
|
+
Changes is recorded when the model is created, updated and destroyed.
|
50
|
+
The `:only` option can be used same as filters of controller.
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
class User < ApplicationRecord
|
54
|
+
record_changes only: [:create, :destroy]
|
55
|
+
end
|
56
|
+
```
|
57
|
+
$ curl -X POST -d "user[name]=bill&user[age]=20" http://localhost:3000/users
|
58
|
+
# output: users.create {"event":"create","request_id":"4afd0731-dd25-4668-b769-2017dbdd3642","table_name":"users","id":1,"changes":{"name":[null,"bill"],"age":[null,20]}}
|
59
|
+
|
60
|
+
$ curl -X PATCH -d "user[age]=21" http://localhost:3000/users/1
|
61
|
+
# no output
|
62
|
+
|
63
|
+
### Customize sending fields of changes
|
64
|
+
|
65
|
+
Kushojin sends model changes with some information:
|
66
|
+
|
67
|
+
- tag: Controller name and action name concatenated with a period.
|
68
|
+
- event: The event which model is changed on.
|
69
|
+
- table_name: Table name of the model.
|
70
|
+
- primary key: Primary key name and value.
|
71
|
+
- changes: Model changes without its primary key, `created_at`, and `updated_at`. It contains pairs of attribute name and before/after values.
|
72
|
+
- request_id: Request ID.
|
73
|
+
|
74
|
+
It is able to pass a customized sender to add additional information.
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
class CustomSender < Kushojin::Sender::EachSender
|
78
|
+
private
|
79
|
+
|
80
|
+
# Add "user_id"
|
81
|
+
def serialize(change, controller)
|
82
|
+
super.merge!("user_id" => controller.current_user.id)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
class MessagesController < ApplicationController
|
87
|
+
send_changes Kushojin::ControllerMethods::SendChangeCallback.new(sender: CustomSender.new)
|
88
|
+
|
89
|
+
def current_user
|
90
|
+
return_user_record_with_any_authentication_logic
|
91
|
+
end
|
92
|
+
|
93
|
+
def create
|
94
|
+
Message.create(params[:message])
|
95
|
+
end
|
96
|
+
end
|
97
|
+
```
|
98
|
+
|
99
|
+
### Customize callbacks of model
|
100
|
+
|
101
|
+
You can pass in a class or an instance to change behaviors of the callbacks.
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
class CustomCallbacks
|
105
|
+
# Must be able to respond to after_create, after_update, and after_destroy.
|
106
|
+
def after_create(record); end
|
107
|
+
def after_update(record); end
|
108
|
+
def after_destroy(record); end
|
109
|
+
end
|
110
|
+
|
111
|
+
class User < ApplicationRecord
|
112
|
+
record_changes CustomCallbacks.new
|
113
|
+
end
|
114
|
+
```
|
115
|
+
|
116
|
+
|
117
|
+
### Override
|
118
|
+
|
119
|
+
You can override options of Recording changes in subclass.
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
class ApplicationRecord < ActiveRecord::Base
|
123
|
+
self.abstract_class = true
|
124
|
+
record_changes
|
125
|
+
end
|
126
|
+
|
127
|
+
class User < ApplicationRecord
|
128
|
+
record_changes only: [:create, :destroy]
|
129
|
+
end
|
130
|
+
```
|
131
|
+
|
42
132
|
## Development
|
43
133
|
|
44
134
|
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/bin/console
CHANGED
@@ -7,8 +7,8 @@ require "kushojin"
|
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
8
8
|
|
9
9
|
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
|
11
|
-
|
10
|
+
require "pry"
|
11
|
+
Pry.start
|
12
12
|
|
13
|
-
require "irb"
|
14
|
-
IRB.start(__FILE__)
|
13
|
+
# require "irb"
|
14
|
+
# IRB.start(__FILE__)
|
data/kushojin.gemspec
CHANGED
@@ -21,13 +21,16 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
22
|
spec.require_paths = ["lib"]
|
23
23
|
|
24
|
-
spec.add_dependency "activerecord", "
|
24
|
+
spec.add_dependency "activerecord", ">= 5.2"
|
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 "
|
30
|
-
spec.add_development_dependency "
|
27
|
+
spec.add_development_dependency "bundler"
|
28
|
+
spec.add_development_dependency "rake"
|
29
|
+
spec.add_development_dependency "appraisal"
|
30
|
+
spec.add_development_dependency "actionpack", ">= 5.0"
|
31
|
+
spec.add_development_dependency "rspec", ">= 3.0"
|
32
|
+
spec.add_development_dependency "sqlite3", ">= 1.3.6"
|
31
33
|
spec.add_development_dependency "pry-byebug"
|
32
34
|
spec.add_development_dependency "rubocop"
|
35
|
+
spec.add_development_dependency "simplecov"
|
33
36
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require "kushojin/controller_methods/
|
2
|
-
require "kushojin/controller_methods/
|
1
|
+
require "kushojin/controller_methods/callback"
|
2
|
+
require "kushojin/controller_methods/send_change_callback"
|
3
3
|
|
4
|
-
ActionController::Metal.extend Kushojin::ControllerMethods::
|
4
|
+
ActionController::Metal.extend Kushojin::ControllerMethods::Callback
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Kushojin
|
2
|
+
module ControllerMethods
|
3
|
+
module Callback
|
4
|
+
# Send recorded changes.
|
5
|
+
#
|
6
|
+
# class UsersController < ApplicationController
|
7
|
+
# send_changes
|
8
|
+
#
|
9
|
+
# def create
|
10
|
+
# User.create(user_params)
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# def update
|
14
|
+
# User.find(params[:id]).update(user_params)
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# def destroy
|
18
|
+
# User.find(params[:id]).destroy
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# private
|
22
|
+
#
|
23
|
+
# def user_params
|
24
|
+
# params.require(:user).permit(:name)
|
25
|
+
# end
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# You can pass in a class or an instance to change behavior of the callback.
|
29
|
+
#
|
30
|
+
# class CustomCallback < Kushojin::ControllerMethods::SendChangeCallback
|
31
|
+
# # Must respond to around.
|
32
|
+
# def around(controller)
|
33
|
+
# # Do something
|
34
|
+
# super
|
35
|
+
# end
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# class UsersController < ApplicationController
|
39
|
+
# send_changes CustomCallback.new
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# ===== Options
|
43
|
+
#
|
44
|
+
# * <tt>only</tt> - Send changes only for this action.
|
45
|
+
# * <tt>except</tt> - Send changes for all actions except this action.
|
46
|
+
#
|
47
|
+
def send_changes(callback = nil, **options)
|
48
|
+
callback ||= Kushojin::ControllerMethods::SendChangeCallback.new
|
49
|
+
around_action callback, options
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -1,15 +1,75 @@
|
|
1
1
|
module Kushojin
|
2
2
|
module ModelMethods
|
3
3
|
module Callback
|
4
|
-
RECORD_EVENTS = %i[
|
4
|
+
RECORD_EVENTS = %i[after_create after_update after_destroy].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
|
+
# You can pass in a class or an instance to change behaviors of the callbacks.
|
18
|
+
#
|
19
|
+
# class CustomCallbacks
|
20
|
+
# # Must be able to respond to after_create, after_update, and after_destroy.
|
21
|
+
# def after_create(record); end
|
22
|
+
# def after_update(record); end
|
23
|
+
# def after_destroy(record); end
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# class User < ApplicationRecord
|
27
|
+
# record_changes CustomCallbacks.new
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# Changes is recorded when the model is created, updated and destroyed.
|
31
|
+
# The +:only+ option can be used same as filters of controller.
|
32
|
+
#
|
33
|
+
# record_changes only: [:create, :destroy]
|
34
|
+
#
|
35
|
+
# ===== Options
|
36
|
+
#
|
37
|
+
# * <tt>only</tt> - Records only for this event.
|
38
|
+
# Support event is +:create+, +:update+, and +:destroy+.
|
39
|
+
#
|
40
|
+
def record_changes(callbacks = nil, only: [])
|
41
|
+
if kushojin_callbacks
|
42
|
+
remove_callbacks
|
43
|
+
self.kushojin_callbacks = callbacks if callbacks
|
44
|
+
else
|
45
|
+
self.kushojin_callbacks = callbacks || RecordChangesCallbacks.new
|
46
|
+
end
|
47
|
+
|
48
|
+
record_events = convert_to_record_events(only)
|
49
|
+
record_events.each do |event|
|
10
50
|
public_send(event, kushojin_callbacks)
|
11
51
|
end
|
12
52
|
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def remove_callbacks
|
57
|
+
kind_and_names =
|
58
|
+
RECORD_EVENTS.map { |event| event.to_s.split("_", 2).map(&:to_sym) }
|
59
|
+
|
60
|
+
kind_and_names.each do |kind, name|
|
61
|
+
skip_callback(name, kind, kushojin_callbacks, raise: false)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def convert_to_record_events(events) # :nodoc:
|
66
|
+
if events.empty?
|
67
|
+
RECORD_EVENTS
|
68
|
+
else
|
69
|
+
event_strs = Array(events).map(&:to_s)
|
70
|
+
RECORD_EVENTS.select { |event| event_strs.include?(event.to_s.sub(/^\w*_/, "")) }
|
71
|
+
end
|
72
|
+
end
|
13
73
|
end
|
14
74
|
end
|
15
75
|
end
|
@@ -1,18 +1,58 @@
|
|
1
1
|
module Kushojin
|
2
2
|
module ModelMethods
|
3
3
|
class Change
|
4
|
-
attr_reader :event, :model
|
4
|
+
attr_reader :event, :model
|
5
5
|
|
6
6
|
delegate :primary_key, :table_name, to: "@model.class"
|
7
7
|
|
8
8
|
def initialize(event, model)
|
9
9
|
@event = event
|
10
|
-
@model =
|
11
|
-
|
10
|
+
@model = take_copy_of(model).freeze
|
11
|
+
end
|
12
|
+
|
13
|
+
# Returns changes of the ActiveRecord model.
|
14
|
+
#
|
15
|
+
# class User < ApplicationRecord
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# user = User.create(name: "bill")
|
19
|
+
# Kushojin::ModelMethods::Change.new(:create, user).changes # => { "id" => [nil, 123], "name" => [nil, "bill"], "created_at" => [nil, 2019-01-23 00:00:00 UTC], "updated_at" => [nil, 2019-12-31 12:34:56 UTC]
|
20
|
+
# # }
|
21
|
+
#
|
22
|
+
# If a model is updated by +touch+ method, this returns the changed
|
23
|
+
# attributes when activerecord is 6.0.0 or later.
|
24
|
+
#
|
25
|
+
# # activerecord 5.x
|
26
|
+
# user.touch
|
27
|
+
# Kushojin::ModelMethods::Change.new(:touch, user).changes # => {}
|
28
|
+
#
|
29
|
+
# # activerecord 6.x
|
30
|
+
# user.touch
|
31
|
+
# Kushojin::ModelMethods::Change.new(:touch, user).changes # => { "updated_at" => [2019-09-16 15:25:43 UTC, 2019-09-16 16:33:48 UTC] }
|
32
|
+
#
|
33
|
+
# See https://github.com/rails/rails/issues/33429
|
34
|
+
def changes
|
35
|
+
@model.saved_changes
|
12
36
|
end
|
13
37
|
|
14
38
|
def changes_without_primary_key
|
15
|
-
|
39
|
+
changes.reject { |attr_name, _| attr_name == primary_key }
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
class_attribute :copy_ivars, instance_writer: false
|
45
|
+
self.copy_ivars = %i[
|
46
|
+
@attributes
|
47
|
+
@mutations_before_last_save
|
48
|
+
]
|
49
|
+
|
50
|
+
def take_copy_of(model)
|
51
|
+
model.class.new do |copy|
|
52
|
+
copy_ivars.each do |ivar|
|
53
|
+
copy.instance_variable_set(ivar, model.instance_variable_get(ivar).deep_dup)
|
54
|
+
end
|
55
|
+
end
|
16
56
|
end
|
17
57
|
end
|
18
58
|
end
|
@@ -2,10 +2,22 @@ module Kushojin
|
|
2
2
|
module ModelMethods
|
3
3
|
class RecordChangesCallbacks
|
4
4
|
Callback::RECORD_EVENTS.each do |event|
|
5
|
+
callback = event.to_s.split(/_/).last
|
6
|
+
|
5
7
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
6
|
-
|
7
|
-
|
8
|
-
|
8
|
+
def #{event}(model) # def after_create(model)
|
9
|
+
return unless Recorder.current # return unless Recorder.current
|
10
|
+
#
|
11
|
+
change = build_change_on_#{callback}(model) # change = build_change_on_create(model)
|
12
|
+
Recorder.record(change) # Recorder.record(change)
|
13
|
+
end # end
|
14
|
+
RUBY
|
15
|
+
|
16
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
17
|
+
def build_change_on_#{callback}(model) # def build_change_on_create(model)
|
18
|
+
ModelMethods::Change.new(:#{callback}, model) # ModelMethods::Change.new(:create, model)
|
19
|
+
end # end
|
20
|
+
private :build_change_on_#{callback} # private :build_change_on_create
|
9
21
|
RUBY
|
10
22
|
end
|
11
23
|
end
|
data/lib/kushojin/recorder.rb
CHANGED
data/lib/kushojin/sender/base.rb
CHANGED
@@ -1,32 +1,26 @@
|
|
1
1
|
module Kushojin
|
2
2
|
module Sender
|
3
3
|
class EachSender < Base
|
4
|
-
def initialize(logger, serializer: Serializer::SimpleSerializer)
|
4
|
+
def initialize(logger = nil, serializer: Serializer::SimpleSerializer)
|
5
5
|
super
|
6
6
|
@serializer = serializer
|
7
7
|
end
|
8
8
|
|
9
9
|
def send(changes, controller:)
|
10
|
+
tag = generate_tag(controller)
|
10
11
|
changes.each do |change|
|
11
|
-
@logger.post(tag
|
12
|
+
@logger.post(tag, serialize(change, controller))
|
12
13
|
end
|
13
14
|
end
|
14
15
|
|
15
16
|
private
|
16
17
|
|
17
|
-
def
|
18
|
+
def generate_tag(controller)
|
18
19
|
"#{controller.controller_name}.#{controller.action_name}"
|
19
20
|
end
|
20
21
|
|
21
|
-
def
|
22
|
-
|
23
|
-
{
|
24
|
-
"event" => change.event.to_s,
|
25
|
-
"request_id" => controller.request.request_id,
|
26
|
-
"table_name" => change.table_name,
|
27
|
-
change.primary_key => change.model.id,
|
28
|
-
"changes" => changes,
|
29
|
-
}
|
22
|
+
def serialize(change, controller)
|
23
|
+
@serializer.serialize(change).merge!("request_id" => controller.request.request_id)
|
30
24
|
end
|
31
25
|
end
|
32
26
|
end
|
@@ -3,9 +3,7 @@ module Kushojin
|
|
3
3
|
module Serializer
|
4
4
|
class Base
|
5
5
|
def self.default_ignore_columns
|
6
|
-
|
7
|
-
c.extend(ActiveRecord::Timestamp::ClassMethods)
|
8
|
-
c.class_eval { timestamp_attributes_for_create + timestamp_attributes_for_update }
|
6
|
+
%w[created_at created_on updated_at updated_on]
|
9
7
|
end
|
10
8
|
private_class_method :default_ignore_columns
|
11
9
|
|
@@ -2,10 +2,9 @@ module Kushojin
|
|
2
2
|
module Sender
|
3
3
|
module Serializer
|
4
4
|
class SimpleSerializer < Base
|
5
|
-
def self.serialize(change
|
5
|
+
def self.serialize(change)
|
6
6
|
{
|
7
7
|
"event" => change.event.to_s,
|
8
|
-
"request_id" => controller.request.request_id,
|
9
8
|
"table_name" => change.table_name,
|
10
9
|
change.primary_key => change.model.id,
|
11
10
|
"changes" => changes_hash(change),
|
data/lib/kushojin/version.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
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.6
|
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: 2020-12-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '5.
|
19
|
+
version: '5.2'
|
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: '5.
|
26
|
+
version: '5.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: fluent-logger
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -42,58 +42,86 @@ 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
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: appraisal
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
60
74
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
75
|
+
version: '0'
|
62
76
|
type: :development
|
63
77
|
prerelease: false
|
64
78
|
version_requirements: !ruby/object:Gem::Requirement
|
65
79
|
requirements:
|
66
|
-
- - "
|
80
|
+
- - ">="
|
67
81
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: actionpack
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '5.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '5.0'
|
69
97
|
- !ruby/object:Gem::Dependency
|
70
98
|
name: rspec
|
71
99
|
requirement: !ruby/object:Gem::Requirement
|
72
100
|
requirements:
|
73
|
-
- - "
|
101
|
+
- - ">="
|
74
102
|
- !ruby/object:Gem::Version
|
75
103
|
version: '3.0'
|
76
104
|
type: :development
|
77
105
|
prerelease: false
|
78
106
|
version_requirements: !ruby/object:Gem::Requirement
|
79
107
|
requirements:
|
80
|
-
- - "
|
108
|
+
- - ">="
|
81
109
|
- !ruby/object:Gem::Version
|
82
110
|
version: '3.0'
|
83
111
|
- !ruby/object:Gem::Dependency
|
84
112
|
name: sqlite3
|
85
113
|
requirement: !ruby/object:Gem::Requirement
|
86
114
|
requirements:
|
87
|
-
- - "
|
115
|
+
- - ">="
|
88
116
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
117
|
+
version: 1.3.6
|
90
118
|
type: :development
|
91
119
|
prerelease: false
|
92
120
|
version_requirements: !ruby/object:Gem::Requirement
|
93
121
|
requirements:
|
94
|
-
- - "
|
122
|
+
- - ">="
|
95
123
|
- !ruby/object:Gem::Version
|
96
|
-
version:
|
124
|
+
version: 1.3.6
|
97
125
|
- !ruby/object:Gem::Dependency
|
98
126
|
name: pry-byebug
|
99
127
|
requirement: !ruby/object:Gem::Requirement
|
@@ -122,6 +150,20 @@ dependencies:
|
|
122
150
|
- - ">="
|
123
151
|
- !ruby/object:Gem::Version
|
124
152
|
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: simplecov
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
125
167
|
description: Detect changes of model attributes and send to outside.
|
126
168
|
email:
|
127
169
|
- loosecplg@gmail.com
|
@@ -129,22 +171,28 @@ executables: []
|
|
129
171
|
extensions: []
|
130
172
|
extra_rdoc_files: []
|
131
173
|
files:
|
174
|
+
- ".github/workflows/CI.yml"
|
175
|
+
- ".github/workflows/release_gem.yml"
|
132
176
|
- ".gitignore"
|
133
177
|
- ".rspec"
|
134
178
|
- ".rubocop.yml"
|
135
179
|
- ".travis.yml"
|
180
|
+
- Appraisals
|
136
181
|
- Gemfile
|
137
182
|
- LICENSE.txt
|
138
183
|
- README.md
|
139
184
|
- Rakefile
|
140
185
|
- bin/console
|
141
186
|
- bin/setup
|
187
|
+
- gemfiles/rails_5.2.gemfile
|
188
|
+
- gemfiles/rails_6.0.gemfile
|
189
|
+
- gemfiles/rails_6.1.gemfile
|
142
190
|
- kushojin.gemspec
|
143
191
|
- lib/kushojin.rb
|
144
192
|
- lib/kushojin/config.rb
|
145
193
|
- lib/kushojin/controller_methods.rb
|
146
|
-
- lib/kushojin/controller_methods/
|
147
|
-
- lib/kushojin/controller_methods/
|
194
|
+
- lib/kushojin/controller_methods/callback.rb
|
195
|
+
- lib/kushojin/controller_methods/send_change_callback.rb
|
148
196
|
- lib/kushojin/model_methods.rb
|
149
197
|
- lib/kushojin/model_methods/callback.rb
|
150
198
|
- lib/kushojin/model_methods/change.rb
|
@@ -176,8 +224,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
176
224
|
- !ruby/object:Gem::Version
|
177
225
|
version: '0'
|
178
226
|
requirements: []
|
179
|
-
|
180
|
-
rubygems_version: 2.6.13
|
227
|
+
rubygems_version: 3.1.4
|
181
228
|
signing_key:
|
182
229
|
specification_version: 4
|
183
230
|
summary: Detect changes of model attributes and send to outside.
|