kushojin 0.1.3 → 0.1.4

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: aded25303e7f5437b7c4ff30cf00786404573a9b348e41ec717ed3fe2ba91a12
4
- data.tar.gz: 7a2475384378eebc452d62d2ad969c8d5c792599aa59265b47dc9ad9d08b388b
3
+ metadata.gz: 12a83022282a232735d811e92857d5662ee21021b19710909339bf284cf866c8
4
+ data.tar.gz: 62d665c8228c5d29e2f45b4bfa972ab24744cccbf4ca2a3f165576ef59a98f87
5
5
  SHA512:
6
- metadata.gz: d1c3a9cb1b8ae4cd2e07b0e2da45583b1a2f38f18d9a96bddaa1c7d74ee9dd9483059b0ca007754cdaac349fcae758f13840f0d391f26233dc59742cc9317faf
7
- data.tar.gz: 7b00fca7decfed5b27caf9e357c8cbf0813a13d1d80ea8e399dad7c12d841485cc8f4f09e2776b809242bead4dbb58338b17dd1567d85bad56887b917dd0c4fb
6
+ metadata.gz: 45732eeacc349c2a826cf297a2d55317ddcb218885bb205c4ff575c00d2fb0c2f3f4592aa7933e134dbcdfc6b48eb27ddc546c209c344984e858d8dbcb5f3b4c
7
+ data.tar.gz: db3657298bf64ea4a5633285d821be4d50d1c2c6432af5f49924406d9098c9f4ff20340549e8698785cafc4eb7848c2d96f28783d8f42e395b93a1bb194e682b
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Kushojin [![Build Status](https://travis-ci.org/nowlinuxing/kushojin.svg?branch=master)](https://travis-ci.org/nowlinuxing/kushojin.svg?branch=master) [![Maintainability](https://api.codeclimate.com/v1/badges/33c293ed9b4f9f25ab2c/maintainability)](https://codeclimate.com/github/nowlinuxing/kushojin/maintainability)
2
2
 
3
- Kushojin aims to record changed attributes of ActiveRecord model.
3
+ Kushojin gathers changes to the attributes of the ActiveRecord model and sends them externally via Fluentd.
4
+ This is useful for logging, tracking and real-time aggregation of accesses involving database updates.
5
+ Since Fluentd is used for external transmission, Kushojin can respond flexibly to various requests.
4
6
 
5
7
  ## Installation
6
8
 
@@ -53,21 +55,60 @@ end
53
55
  $ curl -X PATCH -d "user[age]=21" http://localhost:3000/users/1
54
56
  # no output
55
57
 
56
- Custom callback object can be used with `:callbacks` option.
58
+ ### Customize sending fields of changes
59
+
60
+ Kushojin sends model changes with some information:
61
+
62
+ - tag: Controller name and action name concatenated with a period.
63
+ - event: The event which model is changed on.
64
+ - table_name: Table name of the model.
65
+ - primary key: Primary key name and value.
66
+ - changes: Model changes without its primary key, `created_at`, and `updated_at`. It contains pairs of attribute name and before/after values.
67
+ - request_id: Request ID.
68
+
69
+ It is able to pass a customized sender to add additional information.
70
+
71
+ ```ruby
72
+ class CustomSender < Kushojin::Sender::EachSender
73
+ private
74
+
75
+ # Add "user_id"
76
+ def serialize(change, controller)
77
+ super.merge!("user_id" => controller.current_user.id)
78
+ end
79
+ end
80
+
81
+ class MessagesController < ApplicationController
82
+ send_changes Kushojin::ControllerMethods::SendChangeCallback.new(sender: CustomSender.new)
83
+
84
+ def current_user
85
+ return_user_record_with_any_authentication_logic
86
+ end
87
+
88
+ def create
89
+ Message.create(params[:message])
90
+ end
91
+ end
92
+ ```
93
+
94
+ ### Customize callbacks of model
95
+
96
+ You can pass in a class or an instance to change behaviors of the callbacks.
57
97
 
58
98
  ```ruby
59
99
  class CustomCallbacks
60
- # Must respond to after_create, after_update, and after_destroy.
100
+ # Must be able to respond to after_create, after_update, and after_destroy.
61
101
  def after_create(record); end
62
102
  def after_update(record); end
63
103
  def after_destroy(record); end
64
104
  end
65
105
 
66
106
  class User < ApplicationRecord
67
- record_changes callbacks: CustomCallbacks.new
107
+ record_changes CustomCallbacks.new
68
108
  end
69
109
  ```
70
110
 
111
+
71
112
  ### Override
72
113
 
73
114
  You can override options of Recording changes in subclass.
data/kushojin.gemspec CHANGED
@@ -26,8 +26,9 @@ Gem::Specification.new do |spec|
26
26
 
27
27
  spec.add_development_dependency "bundler"
28
28
  spec.add_development_dependency "rake"
29
+ spec.add_development_dependency "actionpack", ">= 5.0"
29
30
  spec.add_development_dependency "rspec", ">= 3.0"
30
- spec.add_development_dependency "sqlite3"
31
+ spec.add_development_dependency "sqlite3", "~> 1.3.6"
31
32
  spec.add_development_dependency "pry-byebug"
32
33
  spec.add_development_dependency "rubocop"
33
34
  end
@@ -1,4 +1,4 @@
1
- require "kushojin/controller_methods/filter"
2
- require "kushojin/controller_methods/send_change_filter"
1
+ require "kushojin/controller_methods/callback"
2
+ require "kushojin/controller_methods/send_change_callback"
3
3
 
4
- ActionController::Metal.extend Kushojin::ControllerMethods::Filter
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,6 +1,6 @@
1
1
  module Kushojin
2
2
  module ControllerMethods
3
- class SendChangeFilter
3
+ class SendChangeCallback
4
4
  def initialize(sender: nil)
5
5
  @sender = sender || Config.sender
6
6
  end
@@ -14,42 +14,30 @@ module Kushojin
14
14
  # record_changes
15
15
  # end
16
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.
17
+ # You can pass in a class or an instance to change behaviors of the callbacks.
23
18
  #
24
19
  # class CustomCallbacks
25
- # # Must respond to after_create, after_update, and after_destroy.
20
+ # # Must be able to respond to after_create, after_update, and after_destroy.
26
21
  # def after_create(record); end
27
22
  # def after_update(record); end
28
23
  # def after_destroy(record); end
29
24
  # end
30
25
  #
31
26
  # class User < ApplicationRecord
32
- # record_changes callbacks: CustomCallbacks.new
27
+ # record_changes CustomCallbacks.new
33
28
  # end
34
29
  #
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
30
+ # Changes is recorded when the model is created, updated and destroyed.
31
+ # The +:only+ option can be used same as filters of controller.
41
32
  #
42
- # class User < ApplicationRecord
43
- # record_changes only: [:create, :destroy], callbacks: CustomCallbacks.new
44
- # end
33
+ # record_changes only: [:create, :destroy]
45
34
  #
46
35
  # ===== Options
47
36
  #
48
37
  # * <tt>only</tt> - Records only for this event.
49
38
  # Support event is +:create+, +:update+, and +:destroy+.
50
- # * <tt>callbacks</tt> - Use callback object instead of RecordChangesCallbacks.
51
39
  #
52
- def record_changes(only: [], callbacks: nil)
40
+ def record_changes(callbacks = nil, only: [])
53
41
  if kushojin_callbacks
54
42
  remove_callbacks
55
43
  self.kushojin_callbacks = callbacks if callbacks
@@ -1,8 +1,8 @@
1
1
  module Kushojin
2
2
  module Sender
3
3
  class Base
4
- def initialize(logger, **_args)
5
- @logger = logger
4
+ def initialize(logger = nil, **_args)
5
+ @logger = logger || Config.logger
6
6
  end
7
7
 
8
8
  def send(_changes, **_args)
@@ -1,22 +1,27 @@
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(controller), @serializer.serialize(change, controller: controller))
12
+ @logger.post(tag, serialize(change, controller))
12
13
  end
13
14
  end
14
15
 
15
16
  private
16
17
 
17
- def tag(controller)
18
+ def generate_tag(controller)
18
19
  "#{controller.controller_name}.#{controller.action_name}"
19
20
  end
21
+
22
+ def serialize(change, controller)
23
+ @serializer.serialize(change).merge!("request_id" => controller.request.request_id)
24
+ end
20
25
  end
21
26
  end
22
27
  end
@@ -2,10 +2,9 @@ module Kushojin
2
2
  module Sender
3
3
  module Serializer
4
4
  class SimpleSerializer < Base
5
- def self.serialize(change, controller:)
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),
@@ -1,3 +1,3 @@
1
1
  module Kushojin
2
- VERSION = "0.1.3".freeze
2
+ VERSION = "0.1.4".freeze
3
3
  end
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.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Loose coupling
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-30 00:00:00.000000000 Z
11
+ date: 2019-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: actionpack
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rspec
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -84,16 +98,16 @@ dependencies:
84
98
  name: sqlite3
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
- - - ">="
101
+ - - "~>"
88
102
  - !ruby/object:Gem::Version
89
- version: '0'
103
+ version: 1.3.6
90
104
  type: :development
91
105
  prerelease: false
92
106
  version_requirements: !ruby/object:Gem::Requirement
93
107
  requirements:
94
- - - ">="
108
+ - - "~>"
95
109
  - !ruby/object:Gem::Version
96
- version: '0'
110
+ version: 1.3.6
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: pry-byebug
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -143,8 +157,8 @@ files:
143
157
  - lib/kushojin.rb
144
158
  - lib/kushojin/config.rb
145
159
  - lib/kushojin/controller_methods.rb
146
- - lib/kushojin/controller_methods/filter.rb
147
- - lib/kushojin/controller_methods/send_change_filter.rb
160
+ - lib/kushojin/controller_methods/callback.rb
161
+ - lib/kushojin/controller_methods/send_change_callback.rb
148
162
  - lib/kushojin/model_methods.rb
149
163
  - lib/kushojin/model_methods/callback.rb
150
164
  - lib/kushojin/model_methods/change.rb
@@ -1,10 +0,0 @@
1
- module Kushojin
2
- module ControllerMethods
3
- module Filter
4
- def send_changes(**args)
5
- filter = args.delete(:filter) || Kushojin::ControllerMethods::SendChangeFilter.new
6
- around_action filter, args
7
- end
8
- end
9
- end
10
- end