rails_surrogate_key_logging 1.1.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: 59d8e483ef4291595ab8400d078f6b05d806b3f6ff2615742d1b581a339a471b
4
- data.tar.gz: 45e9c0742590d9b511b9d0895d66243c89271c7b76e583c53886e6ff8c968f9e
3
+ metadata.gz: 8ffb01158739d8e9483393f18c917c77a17ef7632153c831f15856ec737faf2f
4
+ data.tar.gz: 54c085f0021eadfa90cb57c86c72327aa2dece0ff5b21a6b1abbf2dfdcbf6bf2
5
5
  SHA512:
6
- metadata.gz: abdb1c362e0f5dc3834cbcdf3e72053ff938dc300625ec573d0d2162c3a4039fe0f6ce9ef72b2107a1ad8dba7874189d617893f754e0d07e292b36399b1998d1
7
- data.tar.gz: 21bada8eb48e7bb83bea8ed5fd982cf41c32ec97f22dfee44330d219a9db61921d89186c59fc0277b85bb29309930e5fa68e4fc0cb003f5a4981a5d3e28527bb
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
@@ -4,7 +4,7 @@ module SurrogateKeyLogging
4
4
 
5
5
  module Version
6
6
  MAJOR = 1
7
- MINOR = 1
7
+ MINOR = 2
8
8
  PATCH = 0
9
9
 
10
10
  end
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.1.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-05-02 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,7 @@ 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
123
124
  - lib/surrogate_key_logging/sidekiq.rb
124
125
  - lib/surrogate_key_logging/version.rb
125
126
  - lib/tasks/key_store/active_record.rake