rails_surrogate_key_logging 1.0.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f98ce688e0b5bccad6bcfc8e3fe0df9b3796f1a92fd4fe30e17d5093d7a176a8
4
- data.tar.gz: efd6b574da4f0ba06d0e47f1564e2f5c049d549d8b0fda59945bb9746028d922
3
+ metadata.gz: 8ffb01158739d8e9483393f18c917c77a17ef7632153c831f15856ec737faf2f
4
+ data.tar.gz: 54c085f0021eadfa90cb57c86c72327aa2dece0ff5b21a6b1abbf2dfdcbf6bf2
5
5
  SHA512:
6
- metadata.gz: 2dbce84a07a1c6146dbf4022d47c6528c1b189c55f053c5671d307ee1d4c378d74a2126fe646728bfbb24b528603ac26d16d777d7e49448a864ade402eedfff0
7
- data.tar.gz: e42649376a21bd2838dca825aeee9c27dd8d2ec3921aa1b311e0a3f7c71f8a6e9b029089c7c6d8726b4135dfedb421b5261fdeb24e732ae88e1df1e498549d58
6
+ metadata.gz: 332f4dbc5a940d9f761236ca335acf8314ed297e32be30146ef2f8b1d12e6c4ae2f3b29ec3c06ec21a16f5d9586134a756c17655446dd79b2d23be5aba464c9c
7
+ data.tar.gz: d59829e5426f10e971d32d3390017b6c370f4d0552cd84ff5eab16d5224959e75c1769e976f265c03150f86d2ac46913ac86474cf73f1e499813597976325b7d
@@ -15,14 +15,16 @@ module SurrogateKeyLogging
15
15
  where(hashed_value: hash_value(value)).select(:key).first&.key
16
16
  end
17
17
 
18
- def add(surrogate, value)
19
- s = new(key: surrogate, value: value, hashed_value: hash_value(value))
20
- s.save
21
- end
22
-
23
18
  def use(surrogate)
24
19
  where(key: surrogate).touch_all
25
20
  end
21
+
22
+ def find_or_create_surrogate_for_value(value, key_for)
23
+ hashed_value = hash_value(value)
24
+ upsert_all([{key: key_for.call(value), value: value, hashed_value: hashed_value}], update_only: 'updated_at')
25
+ where(hashed_value: hashed_value).select(:key).first.key
26
+ end
27
+
26
28
  end
27
29
  end
28
30
  end
@@ -34,6 +34,10 @@ module SurrogateKeyLogging
34
34
  end
35
35
  end
36
36
 
37
+ initializer 'surrogate_key_logging.middleware' do |app|
38
+ app.middleware.insert_before(0, Middleware)
39
+ end
40
+
37
41
  initializer 'surrogate_key_logging.filter_parameters' do
38
42
  if SurrogateKeyLogging.config.enabled
39
43
  end
@@ -28,13 +28,9 @@ module SurrogateKeyLogging
28
28
  end
29
29
 
30
30
  def get_non_cached(value)
31
- stored = key_store.surrogate_for_value(value)
32
- return stored if stored.present?
33
- surrogate = key_for.call(value)
34
- key_store.save(surrogate, value)
35
- surrogate
31
+ key_store.get(value)
36
32
  end
37
-
33
+
38
34
  def call(_key, value, _parents = [], _original_params = nil)
39
35
  return "" if value.blank?
40
36
  surrogate = get(value)
@@ -5,7 +5,7 @@ module SurrogateKeyLogging
5
5
 
6
6
  class ActiveRecord < Base
7
7
 
8
- attr_reader :model
8
+ attr_reader :model, :key_for
9
9
 
10
10
  def initialize
11
11
  if SurrogateKeyLogging.config.key?(:model) && SurrogateKeyLogging.config.model.present?
@@ -14,24 +14,16 @@ module SurrogateKeyLogging
14
14
  else
15
15
  @model = SurrogateKeyLogging::Surrogate
16
16
  end
17
+ @key_for = SurrogateKeyLogging.config.key_for
17
18
  end
18
19
 
19
- def surrogate_for_value(value)
20
- key = model.surrogate_for_value(value)
21
- use(key)
22
- key
23
- end
24
-
25
- def value_for_surrogate(surrogate)
26
- model.value_for_surrogate(surrogate)
27
- end
28
-
29
- def save(surrogate, value)
30
- model.add(surrogate, value)
31
- end
32
-
33
- def use(surrogate)
34
- model.use(surrogate)
20
+ def get(value)
21
+ _get = -> { model.find_or_create_surrogate_for_value(value, key_for) }
22
+ if SurrogateKeyLogging.config.debug
23
+ _get.call
24
+ else
25
+ ::ActiveRecord::Base.logger.silence { _get.call }
26
+ end
35
27
  end
36
28
 
37
29
  end
@@ -4,19 +4,7 @@ module SurrogateKeyLogging
4
4
  module KeyStore
5
5
  class Base
6
6
 
7
- def surrogate_for_value(value)
8
- raise NotImplementedError
9
- end
10
-
11
- def value_for_surrogate(surrogate)
12
- raise NotImplementedError
13
- end
14
-
15
- def save(surrogate, value)
16
- raise NotImplementedError
17
- end
18
-
19
- def use(surrogate)
7
+ def get(value, generator)
20
8
  raise NotImplementedError
21
9
  end
22
10
 
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SurrogateKeyLogging
4
+ class Middleware
5
+
6
+ def initialize(app)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ SurrogateKeyLogging.reset
12
+ @app.call(env)
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ if defined?(::Sidekiq)
4
+ module Sidekiq
5
+ if defined?(Worker)
6
+ module Worker::ClassMethods
7
+ def surrogate_params(*attrs)
8
+ @surrogate_params ||= []
9
+ attrs.each do |attr|
10
+ @surrogate_params << attr.to_sym
11
+ end
12
+ @surrogate_params
13
+ end
14
+ end
15
+ end
16
+
17
+ if defined?(JobLogger)
18
+ class JobLogger
19
+ alias_method :job_hash_context__before_surrogate_key_logging, :job_hash_context
20
+ def job_hash_context(job_hash)
21
+ hash = job_hash_context__before_surrogate_key_logging(job_hash).stringify_keys
22
+ hash['args'] = {}
23
+ if job_hash.key?('args')
24
+ begin
25
+ klass = hash['class'].constantize
26
+ perform = klass.instance_method(:perform)
27
+ params = perform.parameters
28
+ params.each_with_index do |param, i|
29
+ hash['args'][param.last] = job_hash['args'][i]
30
+ end
31
+ hash['args'] = SurrogateKeyLogging.filter_for_attributes(klass.surrogate_params).filter(hash['args'])
32
+ end
33
+ end
34
+ hash
35
+ end
36
+ end
37
+ end
38
+
39
+ end
40
+ end
@@ -4,7 +4,7 @@ module SurrogateKeyLogging
4
4
 
5
5
  module Version
6
6
  MAJOR = 1
7
- MINOR = 0
7
+ MINOR = 2
8
8
  PATCH = 0
9
9
 
10
10
  end
@@ -12,12 +12,14 @@ module SurrogateKeyLogging
12
12
  autoload :ActiveRecord
13
13
  autoload :Config, 'surrogate_key_logging/configuration'
14
14
  autoload :Configuration
15
+ autoload :Engine
15
16
  autoload :KeyManager
16
17
  autoload :KeyStore
17
18
  autoload :Middleware
18
- autoload :Engine
19
19
  autoload :Version
20
20
 
21
+ require 'surrogate_key_logging/sidekiq' if defined?(::Sidekiq)
22
+
21
23
  @config = Config.new
22
24
 
23
25
  class << self
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_surrogate_key_logging
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taylor Yelverton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-31 00:00:00.000000000 Z
11
+ date: 2023-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -120,6 +120,8 @@ files:
120
120
  - lib/surrogate_key_logging/key_store.rb
121
121
  - lib/surrogate_key_logging/key_store/active_record.rb
122
122
  - lib/surrogate_key_logging/key_store/base.rb
123
+ - lib/surrogate_key_logging/middleware.rb
124
+ - lib/surrogate_key_logging/sidekiq.rb
123
125
  - lib/surrogate_key_logging/version.rb
124
126
  - lib/tasks/key_store/active_record.rake
125
127
  - lib/tasks/surrogate_key_logging.rake