logidze 0.6.5 → 0.7.0

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: a022058afd1c2017c10dfa802227b2b88433c5f4d103b2f07b7ce8b404887bdf
4
- data.tar.gz: 33f96a549116e7ef695dc83605e3b90edd1eab2a6f1424bebf6310156622233b
3
+ metadata.gz: bfd80509e728582e7c8b3629a6704d7d2d7b8d4e835974df0bd10dac03333348
4
+ data.tar.gz: 5f3f386533f330d41d4acaeaf9e724df4a3d1ee3851cd99dbbff4ab5b6573546
5
5
  SHA512:
6
- metadata.gz: 04c1f2fc5afc500af82dfc1d76399deebc573b7b571cee870ee384cd0816b121ab52b37664706137da4acbd4ec64ce95e1df9a5ccb5766998536f1744c5de582
7
- data.tar.gz: '0925a5a4b918581b9d98c26adb5bd3121fa58e4f2037b903026e9e1fcc4f423a71df46491edbbb5892c86db6d6e5136b5649e1e52f410e2fd5c1dbf20f41648d'
6
+ metadata.gz: a17ac5c2fb65280a3897c5f672d0d0ef719870cd8f019a0d525e314d0c60795e5fda4068ba3fb0b1003f2ec5a848b3a2150237d29256fa8828a8401e775afcb6
7
+ data.tar.gz: f9023b0b9731f9ddbb28fda06ae07995eb0e0a7cb33778238d582e82d8752632c7ca3fa84bfbe639ce2553fbe9c4b10dbaf0dc92d361f01deb08a2d97aebc447
data/.rubocop.yml CHANGED
@@ -91,4 +91,4 @@ Layout/IndentHeredoc:
91
91
  Enabled: false
92
92
 
93
93
  Layout/EmptyLineAfterMagicComment:
94
- Enabled: false
94
+ Enabled: false
data/.travis.yml CHANGED
@@ -26,6 +26,8 @@ matrix:
26
26
  gemfile: gemfiles/railsmaster.gemfile
27
27
  - rvm: 2.5.0
28
28
  gemfile: gemfiles/rails5.gemfile
29
+ - rvm: 2.5.1
30
+ gemfile: gemfiles/rails52.gemfile
29
31
  - rvm: 2.4.1
30
32
  gemfile: gemfiles/rails5.gemfile
31
33
  - rvm: 2.3.1
data/CHANGELOG.md CHANGED
@@ -2,6 +2,29 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 0.7.0 (2018-08-29)
6
+
7
+ - [Fixes [#75](https://github.com/palkan/logidze/issues/70)] Fix association versioning with an optional belongs to ([@ankursethi-uscis][])
8
+
9
+ - [PR [#79](https://github.com/palkan/logidze/pull/13)] Allow adding meta information to versions using `with_meta` (addressed [Issue [#60]](https://github.com/palkan/logidze/issues/60)). ([@DmitryTsepelev][])
10
+
11
+ Usage:
12
+
13
+ ```ruby
14
+ Logidze.with_meta(ip: request.ip) { post.save }
15
+ puts post.meta # => { 'ip' => '95.66.157.226' }
16
+ ```
17
+
18
+ How to upgrade.
19
+
20
+ Please run `rails generate logidze:install --update` to regenerate stored functions.
21
+
22
+ This feature replaces the implementation of `with_responsible`, now `responsible_id` is stored inside of the meta hash with the key `_r`.
23
+
24
+ There is fallback to the old data structure (`{ 'r' => 42 }` opposed to `{ 'm' => { '_r' => 42 } }` in the current implementation), so `responsible_id` should work as usual for the existing data.
25
+
26
+ If you've accessed the value manually (e.g. `post.log_data.current_version.data['r']`), you'll have to add the fallback too.
27
+
5
28
  ## 0.6.5 (2018-08-08)
6
29
 
7
30
  - Make compatible with Rails 5.2.1 ([@palkan][])
@@ -99,3 +122,5 @@ This is a quick fix for a more general problem (see [#59](https://github.com/pal
99
122
  [@charlie-wasp]: https://github.com/charlie-wasp
100
123
  [@akxcv]: https://github.com/akxcv
101
124
  [@vassilevsky]: https://github.com/vassilevsky
125
+ [@ankursethi-uscis]: https://github.com/ankursethi-uscis
126
+ [@DmitryTsepelev]: https://github.com/DmitryTsepelev
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
- [![Gem Version](https://badge.fury.io/rb/logidze.svg)](https://rubygems.org/gems/logidze) [![Build Status](https://travis-ci.org/palkan/logidze.svg?branch=master)](https://travis-ci.org/palkan/logidze) [![Circle CI](https://circleci.com/gh/palkan/logidze/tree/master.svg?style=svg)](https://circleci.com/gh/palkan/logidze/tree/master)
2
- [![Dependency Status](https://dependencyci.com/github/palkan/logidze/badge)](https://dependencyci.com/github/palkan/logidze)
1
+ [![Cult Of Martians](http://cultofmartians.com/assets/badges/badge.svg)](http://cultofmartians.com)
2
+ [![Gem Version](https://badge.fury.io/rb/logidze.svg)](https://rubygems.org/gems/logidze) [![Build Status](https://travis-ci.org/palkan/logidze.svg?branch=master)](https://travis-ci.org/palkan/logidze)
3
3
  [![Open Source Helpers](https://www.codetriage.com/palkan/logidze/badges/users.svg)](https://www.codetriage.com/palkan/logidze)
4
4
 
5
5
  # Logidze
@@ -207,9 +207,21 @@ Logidze.append_on_undo = true
207
207
  ```
208
208
 
209
209
 
210
+ ## Track meta information
211
+
212
+ You can store any meta information you want inside your version (it could be IP address, user agent etc). In order to add it you should wrap your code with a block:
213
+
214
+ ```ruby
215
+ Logidze.with_meta(ip: request.ip) do
216
+ post.save!
217
+ end
218
+ ```
219
+
220
+ Meta expects a hash to be passed so you won't need to encode and decode JSON manually.
221
+
210
222
  ## Track responsibility (aka _whodunnit_)
211
223
 
212
- You can store additional information in the version object, which is called _Responsible ID_. There is more likely that you would like to store the `current_user.id` that way.
224
+ 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.
213
225
 
214
226
  To provide `responsible_id` you should wrap your code in a block:
215
227
 
@@ -1,6 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'rails', '~> 5.0'
3
+ gem 'rails', '~> 5.1.0'
4
4
  gem 'rspec-rails', '~> 3.5'
5
5
 
6
6
  gemspec path: '..'
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '~> 5.2.1'
4
+ gem 'rspec-rails', '~> 3.5'
5
+
6
+ gemspec path: '..'
@@ -8,7 +8,7 @@ class <%= @migration_class_name %> < ActiveRecord::Migration<%= ActiveRecord::VE
8
8
  DO $$
9
9
  BEGIN
10
10
  EXECUTE 'ALTER DATABASE ' || quote_ident(current_database()) || ' SET logidze.disabled=' || quote_literal('');
11
- EXECUTE 'ALTER DATABASE ' || quote_ident(current_database()) || ' SET logidze.responsible=' || quote_literal('');
11
+ EXECUTE 'ALTER DATABASE ' || quote_ident(current_database()) || ' SET logidze.meta=' || quote_literal('');
12
12
  END;
13
13
  $$
14
14
  LANGUAGE plpgsql;
@@ -37,8 +37,8 @@ class <%= @migration_class_name %> < ActiveRecord::Migration<%= ActiveRecord::VE
37
37
  'c',
38
38
  logidze_exclude_keys(data, VARIADIC array_append(blacklist, 'log_data'))
39
39
  );
40
- IF coalesce(#{current_setting('logidze.responsible')}, '') <> '' THEN
41
- buf := jsonb_set(buf, ARRAY['r'], to_jsonb(current_setting('logidze.responsible')));
40
+ IF coalesce(#{current_setting('logidze.meta')}, '') <> '' THEN
41
+ buf := jsonb_set(buf, ARRAY['m'], current_setting('logidze.meta')::jsonb);
42
42
  END IF;
43
43
  RETURN buf;
44
44
  END;
@@ -92,8 +92,8 @@ class <%= @migration_class_name %> < ActiveRecord::Migration<%= ActiveRecord::VE
92
92
  (log_data#>'{h,0,c}') || (log_data#>'{h,1,c}')
93
93
  );
94
94
 
95
- IF (log_data#>'{h,1}' ? 'r') THEN
96
- merged := jsonb_set(merged, ARRAY['r'], log_data#>'{h,1,r}');
95
+ IF (log_data#>'{h,1}' ? 'm') THEN
96
+ merged := jsonb_set(merged, ARRAY['m'], log_data#>'{h,1,m}');
97
97
  END IF;
98
98
 
99
99
  return jsonb_set(
data/lib/logidze.rb CHANGED
@@ -8,9 +8,9 @@ module Logidze
8
8
  require 'logidze/model'
9
9
  require 'logidze/versioned_association'
10
10
  require 'logidze/has_logidze'
11
- require 'logidze/responsible'
11
+ require 'logidze/meta'
12
12
 
13
- extend Logidze::Responsible
13
+ extend Logidze::Meta
14
14
 
15
15
  require 'logidze/engine' if defined?(Rails)
16
16
 
@@ -14,7 +14,7 @@ module Logidze
14
14
  attr_reader :data
15
15
 
16
16
  delegate :size, to: :versions
17
- delegate :responsible_id, to: :current_version
17
+ delegate :responsible_id, :meta, to: :current_version
18
18
 
19
19
  ### Rails 4 ###
20
20
  def self.dump(object)
@@ -9,6 +9,10 @@ module Logidze
9
9
  CHANGES = 'c'
10
10
  # Responsible ID
11
11
  RESPONSIBLE = 'r'
12
+ # Meta Responsible ID
13
+ META_RESPONSIBLE = '_r'
14
+ # Meta key
15
+ META = 'm'
12
16
 
13
17
  attr_reader :data
14
18
 
@@ -29,7 +33,11 @@ module Logidze
29
33
  end
30
34
 
31
35
  def responsible_id
32
- data[RESPONSIBLE]
36
+ meta && meta[META_RESPONSIBLE] || data[RESPONSIBLE]
37
+ end
38
+
39
+ def meta
40
+ data[META]
33
41
  end
34
42
  end
35
43
  end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Logidze # :nodoc:
4
+ # Provide methods to attach meta information
5
+ module Meta
6
+ def with_meta(meta, &block)
7
+ MetaTransaction.wrap_with(meta, &block)
8
+ end
9
+
10
+ def with_responsible(responsible_id, &block)
11
+ return yield if responsible_id.nil?
12
+ meta = { Logidze::History::Version::META_RESPONSIBLE => responsible_id }
13
+ with_meta(meta, &block)
14
+ end
15
+
16
+ class MetaTransaction # :nodoc:
17
+ def self.wrap_with(meta, &block)
18
+ new(meta, &block).perform
19
+ end
20
+
21
+ attr_reader :meta, :block
22
+
23
+ delegate :connection, to: ActiveRecord::Base
24
+
25
+ def initialize(meta, &block)
26
+ @meta = meta
27
+ @block = block
28
+ end
29
+
30
+ def perform
31
+ return if block.nil?
32
+ return block.call if meta.nil?
33
+ ActiveRecord::Base.transaction { call_block_in_meta_context }
34
+ end
35
+
36
+ private
37
+
38
+ def call_block_in_meta_context
39
+ prev_meta = current_meta
40
+
41
+ meta_stack.push(meta)
42
+
43
+ pg_set_meta_param(current_meta)
44
+ result = block.call
45
+ pg_reset_meta_param(prev_meta)
46
+
47
+ result
48
+ ensure
49
+ meta_stack.pop
50
+ end
51
+
52
+ def current_meta
53
+ meta_stack.reduce(:merge) || {}
54
+ end
55
+
56
+ def meta_stack
57
+ Thread.current[:meta] ||= []
58
+ Thread.current[:meta]
59
+ end
60
+
61
+ def pg_set_meta_param(value)
62
+ encoded_meta = connection.quote(ActiveSupport::JSON.encode(value))
63
+ connection.execute("SET LOCAL logidze.meta = #{encoded_meta};")
64
+ end
65
+
66
+ def pg_reset_meta_param(prev_meta)
67
+ if prev_meta.empty?
68
+ connection.execute("SET LOCAL logidze.meta TO DEFAULT;")
69
+ else
70
+ pg_set_meta_param(prev_meta)
71
+ end
72
+ end
73
+ end
74
+
75
+ private_constant :MetaTransaction
76
+ end
77
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Logidze
3
- VERSION = "0.6.5"
3
+ VERSION = "0.7.0"
4
4
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  # `inversed` attr_accessor has been removed in Rails 5.2.1
4
4
  # See https://github.com/rails/rails/commit/39e57daffb9ff24726b1e86dfacf76836dd0637b#diff-c47e1c26ae8a3d486119e0cc91f40a30
5
- unless ::ActiveRecord::Associations::Association.instance_methods.include?(:inveresed)
5
+ unless ::ActiveRecord::Associations::Association.instance_methods.include?(:inversed)
6
6
  using(Module.new do
7
7
  refine ::ActiveRecord::Associations::Association do
8
8
  attr_reader :inversed
@@ -15,7 +15,7 @@ module Logidze # :nodoc: all
15
15
  # rubocop: disable Metrics/MethodLength, Metrics/AbcSize
16
16
  def load_target
17
17
  target = super
18
-
18
+ return unless target
19
19
  return target if inversed
20
20
 
21
21
  time = owner.logidze_requested_ts
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.6.5
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - palkan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-08 00:00:00.000000000 Z
11
+ date: 2018-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -211,9 +211,9 @@ files:
211
211
  - bench/triggers/keys_trigger_setup.sql
212
212
  - bin/console
213
213
  - bin/setup
214
- - circle.yml
215
214
  - gemfiles/rails42.gemfile
216
215
  - gemfiles/rails5.gemfile
216
+ - gemfiles/rails52.gemfile
217
217
  - gemfiles/railsmaster.gemfile
218
218
  - lib/generators/logidze/install/USAGE
219
219
  - lib/generators/logidze/install/install_generator.rb
@@ -228,9 +228,9 @@ files:
228
228
  - lib/logidze/history.rb
229
229
  - lib/logidze/history/type.rb
230
230
  - lib/logidze/history/version.rb
231
+ - lib/logidze/meta.rb
231
232
  - lib/logidze/migration.rb
232
233
  - lib/logidze/model.rb
233
- - lib/logidze/responsible.rb
234
234
  - lib/logidze/version.rb
235
235
  - lib/logidze/versioned_association.rb
236
236
  - logidze.gemspec
data/circle.yml DELETED
@@ -1,15 +0,0 @@
1
- machine:
2
- ruby:
3
- version: 2.5.0
4
-
5
- environment:
6
- DATABASE_URL: postgres://postgres@127.0.0.1:5432/test_database
7
-
8
- dependencies:
9
- pre:
10
- - gem install bundler -v 1.11.2
11
-
12
- database:
13
- override:
14
- - bundle exec rake dummy:db:test:prepare
15
-
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
- module Logidze # :nodoc:
3
- # Provide methods to work with "responsibility" feature
4
- module Responsible
5
- def with_responsible(responsible_id)
6
- return yield if responsible_id.nil?
7
- ActiveRecord::Base.transaction do
8
- ActiveRecord::Base.connection.execute(
9
- "SET LOCAL logidze.responsible = #{ActiveRecord::Base.connection.quote(responsible_id)};"
10
- )
11
- res = yield
12
- ActiveRecord::Base.connection.execute "SET LOCAL logidze.responsible TO DEFAULT;"
13
- res
14
- end
15
- end
16
- end
17
- end