logidze 0.11.0 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/.travis.yml +10 -6
- data/CHANGELOG.md +22 -0
- data/Gemfile +1 -1
- data/README.md +15 -0
- data/gemfiles/rails6.gemfile +2 -2
- data/lib/logidze/has_logidze.rb +1 -1
- data/lib/logidze/ignore_log_data.rb +1 -0
- data/lib/logidze/meta.rb +43 -16
- data/lib/logidze/version.rb +1 -1
- metadata +3 -4
- data/lib/logidze/ignore_log_data/association.rb +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f13cd8a6226e2595de2a96ecb146e387d2fc2f1d0267e2b9076d822773703b92
|
4
|
+
data.tar.gz: 0f2644d225ec6457bcda305158e60a486cdb71e4a6c3cc0b092db930491f60d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 21cdbd6b6aa0d8a6295813943671f01d617a62cdf63bfd9df3389941cc29232f00edceaf6f25a1812fcd08f205c2f56cb212fc0c417a215628f7d604ec48e8db
|
7
|
+
data.tar.gz: 118d8b66bd7f0f661b6aed153bd08453c21f59afde5f85c712b7278ca19397922cb726dcf96dd5140d0bbe5bf7fb9eecd208bfe7e513b706087c7cdf1bacf850
|
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
@@ -21,22 +21,26 @@ before_script:
|
|
21
21
|
- psql -U postgres -d logidze -c 'CREATE EXTENSION IF NOT EXISTS hstore;'
|
22
22
|
- RAILS_ENV=test bundle exec rake dummy:db:create dummy:db:migrate
|
23
23
|
|
24
|
+
before_install:
|
25
|
+
- gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
|
26
|
+
- gem install bundler -v '< 2'
|
27
|
+
|
24
28
|
matrix:
|
25
29
|
include:
|
26
30
|
- rvm: ruby-head
|
27
31
|
gemfile: gemfiles/railsmaster.gemfile
|
28
|
-
- rvm: 2.
|
32
|
+
- rvm: 2.7
|
29
33
|
gemfile: gemfiles/rails6.gemfile
|
30
|
-
- rvm: 2.6
|
34
|
+
- rvm: 2.6
|
31
35
|
gemfile: gemfiles/rails5.gemfile
|
32
|
-
- rvm: 2.5
|
36
|
+
- rvm: 2.5
|
33
37
|
gemfile: gemfiles/rails52.gemfile
|
34
|
-
- rvm: 2.4
|
38
|
+
- rvm: 2.4
|
35
39
|
gemfile: gemfiles/rails5.gemfile
|
36
|
-
- rvm: 2.4
|
40
|
+
- rvm: 2.4
|
37
41
|
gemfile: gemfiles/rails42.gemfile
|
38
42
|
allow_failures:
|
39
43
|
- rvm: ruby-head
|
40
44
|
gemfile: gemfiles/railsmaster.gemfile
|
41
|
-
- rvm: 2.5
|
45
|
+
- rvm: 2.5
|
42
46
|
gemfile: gemfiles/railsmaster.gemfile
|
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,28 @@
|
|
1
|
+
|
1
2
|
# Change log
|
2
3
|
|
3
4
|
## master (unreleased)
|
4
5
|
|
6
|
+
## 0.12.0 (2020-01-02)
|
7
|
+
|
8
|
+
- PR [#143](https://github.com/palkan/logidze/pull/143) Add `:transactional` option to `#with_meta` and `#with_responsible` ([@oleg-kiviljov][])
|
9
|
+
|
10
|
+
Now it's possible to set meta and responsible without wrapping the block into a DB transaction. For backward compatibility `:transactional` option by default is set to `true`.
|
11
|
+
|
12
|
+
Usage:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
Logidze.with_meta({ip: request.ip}, transactional: false) do
|
16
|
+
post.save!
|
17
|
+
end
|
18
|
+
```
|
19
|
+
or
|
20
|
+
```ruby
|
21
|
+
Logidze.with_responsible(user.id, transactional: false) do
|
22
|
+
post.save!
|
23
|
+
end
|
24
|
+
```
|
25
|
+
|
5
26
|
## 0.11.0 (2019-08-15)
|
6
27
|
|
7
28
|
- **Breaking** Return `nil` when `log_data` is not loaded instead of raising an exception. ([@palkan][])
|
@@ -284,3 +305,4 @@ This is a quick fix for a more general problem (see [#59](https://github.com/pal
|
|
284
305
|
[@dmitrytsepelev]: https://github.com/DmitryTsepelev
|
285
306
|
[@zocoi]: https://github.com/zocoi
|
286
307
|
[@duderman]: https://github.com/duderman
|
308
|
+
[@oleg-kiviljov]: https://github.com/oleg-kiviljov
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -261,6 +261,14 @@ end
|
|
261
261
|
|
262
262
|
Meta expects a hash to be passed so you won't need to encode and decode JSON manually.
|
263
263
|
|
264
|
+
By default `.with_meta` wraps the block into a DB transaction. That could lead to an unexpected behavior, especially, when using `.with_meta` within an around_action. To avoid wrapping the block into a DB transaction use `transactional: false` option.
|
265
|
+
|
266
|
+
```ruby
|
267
|
+
Logidze.with_meta({ip: request.ip}, transactional: false) do
|
268
|
+
post.save!
|
269
|
+
end
|
270
|
+
```
|
271
|
+
|
264
272
|
## Track responsibility (aka _whodunnit_)
|
265
273
|
|
266
274
|
A special application of meta information is storing the author of the change, which is called _Responsible ID_. There is more likely that you would like to store the `current_user.id` that way.
|
@@ -304,6 +312,13 @@ class ApplicationController < ActionController::Base
|
|
304
312
|
end
|
305
313
|
```
|
306
314
|
|
315
|
+
By default `.with_responsible` wraps the block into a DB transaction. That could lead to an unexpected behavior, especially, when using `.with_responsible` within an around_action. To avoid wrapping the block into a DB transaction use `transactional: false` option.
|
316
|
+
|
317
|
+
```ruby
|
318
|
+
Logidze.with_responsible(user.id, transactional: false) do
|
319
|
+
post.save!
|
320
|
+
end
|
321
|
+
```
|
307
322
|
## Disable logging temporary
|
308
323
|
|
309
324
|
If you want to make update without logging (e.g., mass update), you can turn it off the following way:
|
data/gemfiles/rails6.gemfile
CHANGED
data/lib/logidze/has_logidze.rb
CHANGED
@@ -11,8 +11,8 @@ module Logidze
|
|
11
11
|
# Include methods to work with history.
|
12
12
|
#
|
13
13
|
def has_logidze(ignore_log_data: Logidze.ignore_log_data_by_default)
|
14
|
-
include Logidze::Model
|
15
14
|
include Logidze::IgnoreLogData
|
15
|
+
include Logidze::Model
|
16
16
|
|
17
17
|
@ignore_log_data = ignore_log_data
|
18
18
|
|
@@ -7,6 +7,7 @@ module Logidze
|
|
7
7
|
included do
|
8
8
|
if Rails::VERSION::MAJOR == 4
|
9
9
|
require "logidze/ignore_log_data/ignored_columns"
|
10
|
+
attribute :log_data, ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Jsonb.new
|
10
11
|
elsif Rails::VERSION::MAJOR == 5
|
11
12
|
require "logidze/ignore_log_data/cast_attribute_patch"
|
12
13
|
include CastAttributePatch
|
data/lib/logidze/meta.rb
CHANGED
@@ -3,18 +3,19 @@
|
|
3
3
|
module Logidze # :nodoc:
|
4
4
|
# Provide methods to attach meta information
|
5
5
|
module Meta
|
6
|
-
def with_meta(meta, &block)
|
7
|
-
|
6
|
+
def with_meta(meta, transactional: true, &block)
|
7
|
+
wrapper = transactional ? MetaWithTransaction : MetaWithoutTransaction
|
8
|
+
wrapper.wrap_with(meta, &block)
|
8
9
|
end
|
9
10
|
|
10
|
-
def with_responsible(responsible_id, &block)
|
11
|
+
def with_responsible(responsible_id, transactional: true, &block)
|
11
12
|
return yield if responsible_id.nil?
|
12
13
|
|
13
14
|
meta = {Logidze::History::Version::META_RESPONSIBLE => responsible_id}
|
14
|
-
with_meta(meta, &block)
|
15
|
+
with_meta(meta, transactional: transactional, &block)
|
15
16
|
end
|
16
17
|
|
17
|
-
class
|
18
|
+
class MetaWrapper # :nodoc:
|
18
19
|
def self.wrap_with(meta, &block)
|
19
20
|
new(meta, &block).perform
|
20
21
|
end
|
@@ -29,14 +30,12 @@ module Logidze # :nodoc:
|
|
29
30
|
end
|
30
31
|
|
31
32
|
def perform
|
32
|
-
|
33
|
+
raise ArgumentError, "Block must be given" unless block
|
33
34
|
return block.call if meta.nil?
|
34
35
|
|
35
|
-
|
36
|
+
call_block_in_meta_context
|
36
37
|
end
|
37
38
|
|
38
|
-
private
|
39
|
-
|
40
39
|
def call_block_in_meta_context
|
41
40
|
prev_meta = current_meta
|
42
41
|
|
@@ -44,10 +43,9 @@ module Logidze # :nodoc:
|
|
44
43
|
|
45
44
|
pg_set_meta_param(current_meta)
|
46
45
|
result = block.call
|
47
|
-
pg_reset_meta_param(prev_meta)
|
48
|
-
|
49
46
|
result
|
50
47
|
ensure
|
48
|
+
pg_reset_meta_param(prev_meta)
|
51
49
|
meta_stack.pop
|
52
50
|
end
|
53
51
|
|
@@ -60,20 +58,49 @@ module Logidze # :nodoc:
|
|
60
58
|
Thread.current[:meta]
|
61
59
|
end
|
62
60
|
|
63
|
-
def
|
64
|
-
|
65
|
-
connection.execute("SET LOCAL logidze.meta = #{encoded_meta};")
|
61
|
+
def encode_meta(value)
|
62
|
+
connection.quote(ActiveSupport::JSON.encode(value))
|
66
63
|
end
|
67
64
|
|
68
65
|
def pg_reset_meta_param(prev_meta)
|
69
66
|
if prev_meta.empty?
|
70
|
-
|
67
|
+
pg_clear_meta_param
|
71
68
|
else
|
72
69
|
pg_set_meta_param(prev_meta)
|
73
70
|
end
|
74
71
|
end
|
75
72
|
end
|
76
73
|
|
77
|
-
|
74
|
+
class MetaWithTransaction < MetaWrapper # :nodoc:
|
75
|
+
private
|
76
|
+
|
77
|
+
def call_block_in_meta_context
|
78
|
+
connection.transaction { super }
|
79
|
+
end
|
80
|
+
|
81
|
+
def pg_set_meta_param(value)
|
82
|
+
connection.execute("SET LOCAL logidze.meta = #{encode_meta(value)};")
|
83
|
+
end
|
84
|
+
|
85
|
+
def pg_clear_meta_param
|
86
|
+
connection.execute("SET LOCAL logidze.meta TO DEFAULT;")
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
class MetaWithoutTransaction < MetaWrapper # :nodoc:
|
91
|
+
private
|
92
|
+
|
93
|
+
def pg_set_meta_param(value)
|
94
|
+
connection.execute("SET logidze.meta = #{encode_meta(value)};")
|
95
|
+
end
|
96
|
+
|
97
|
+
def pg_clear_meta_param
|
98
|
+
connection.execute("SET logidze.meta TO DEFAULT;")
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
private_constant :MetaWrapper
|
103
|
+
private_constant :MetaWithTransaction
|
104
|
+
private_constant :MetaWithoutTransaction
|
78
105
|
end
|
79
106
|
end
|
data/lib/logidze/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: logidze
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- palkan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-01-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -191,7 +191,6 @@ files:
|
|
191
191
|
- lib/logidze/history/type.rb
|
192
192
|
- lib/logidze/history/version.rb
|
193
193
|
- lib/logidze/ignore_log_data.rb
|
194
|
-
- lib/logidze/ignore_log_data/association.rb
|
195
194
|
- lib/logidze/ignore_log_data/cast_attribute_patch.rb
|
196
195
|
- lib/logidze/ignore_log_data/ignored_columns.rb
|
197
196
|
- lib/logidze/meta.rb
|
@@ -224,7 +223,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
224
223
|
- !ruby/object:Gem::Version
|
225
224
|
version: '0'
|
226
225
|
requirements: []
|
227
|
-
rubygems_version: 3.0.
|
226
|
+
rubygems_version: 3.0.6
|
228
227
|
signing_key:
|
229
228
|
specification_version: 4
|
230
229
|
summary: PostgreSQL JSON-based auditing
|