logidze 0.10.0 → 0.11.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: 56aa14f81cc160897376ed2b226664fc3a93c5f4becedb7d5a45abd197d03388
4
- data.tar.gz: 5e196a2ef847e0e1d6069e793fb726f35166f05be007d9c4e4cfde2318b2cd3a
3
+ metadata.gz: 75f1f01b10a1e35d8b656a27f3d71b0e1d2f81c826338fa89d36d76ac03363b9
4
+ data.tar.gz: 3812d6d4b49d2fe803acc61854b1ca891ec816bbd4a3e05ff60e02bc62393ac0
5
5
  SHA512:
6
- metadata.gz: f8871956c3f1439fdd5cc3bb810e612a75145a49cf7f43a039b92915a2edc2ab57bb49a7bd5a4a14a48863696f37d8afcce4f052b2c620b01ffd5a179398fc30
7
- data.tar.gz: 15440ace63434523644340155bd26bdb55acb906993b4b6014d1dd8d0f0e498d4e34709bc144bb3da260c8e509f336dc68c9100cc37568d3354d025a9d88cf39
6
+ metadata.gz: 7b38094dd8e8b7de7295b354e0906067641080e90169f0222b163b43a491672921eff25a78062680650e1e8565727d01dcd4694a84fecbeb7ff60a918d2fbe6c
7
+ data.tar.gz: 7ba5a0605b9b45e63bb6c02b9d18b05f78331369ed4aeb2564434d1e627013e360b1ef0ab2f2fdd94203de6ed341d3136d800e2b8aa838d6fe6c501eecb5d98d
@@ -2,6 +2,20 @@
2
2
 
3
3
  ## master (unreleased)
4
4
 
5
+ ## 0.11.0 (2019-08-15)
6
+
7
+ - **Breaking** Return `nil` when `log_data` is not loaded instead of raising an exception. ([@palkan][])
8
+
9
+ We cannot distinguish between not loaded `log_data` and not-yet-created (i.e. for new records).
10
+ The latter could be used in frameworks/gems ([example](https://github.com/palkan/logidze/issues/127#issuecomment-518798640)).
11
+
12
+ - **Breaking** Only allow specifying `ignore_log_data` at boot time without runtime modifications. ([@palkan][])
13
+
14
+ Playing with ActiveRecord default scopes wasn't a good idea. We fallback to a more explicit way of _telling_ AR
15
+ when to load or ignore the `log_data` column.
16
+
17
+ This change removes `Logidze.with_log_data` method.
18
+
5
19
  ## 0.10.0 (2019-05-15)
6
20
 
7
21
  - **Ruby >= 2.4 is required**
data/README.md CHANGED
@@ -13,6 +13,7 @@ Currently, only PostgreSQL 9.5+ is supported (for PostgreSQL 9.4 try [jsonbx](ht
13
13
  [How is Logidze pronounced?](https://github.com/palkan/logidze/issues/73)
14
14
 
15
15
  Other requirements:
16
+
16
17
  - Ruby ~> 2.1
17
18
  - Rails >= 4.2 (**Rails 6 is supported**)
18
19
 
@@ -73,9 +74,9 @@ You can log only particular columns changes. There are mutually exclusive `black
73
74
 
74
75
  ```sh
75
76
  # track all columns, except `created_at` and `active`
76
- rails generate logidze:model Post --blacklist=created_at active
77
+ rails generate logidze:model Post --blacklist=created_at,active
77
78
  # track only `title` and `body` columns
78
- rails generate logidze:model Post --whitelist=title body
79
+ rails generate logidze:model Post --whitelist=title,body
79
80
  ```
80
81
 
81
82
  By default, Logidze tries to infer the path to the model file from the model name and may fail, for example, if you have unconventional project structure. In that case, you should specify the path explicitly:
@@ -98,7 +99,7 @@ rails generate logidze:model Post --timestamp_column nil # "null" and "false" wi
98
99
  If you want to update Logidze settings for the model, run migration with `--update` flag:
99
100
 
100
101
  ```sh
101
- rails generate logidze:model Post --update --whitelist=title body rating
102
+ rails generate logidze:model Post --update --whitelist=title,body,rating
102
103
  ```
103
104
 
104
105
  Logidze also supports associations versioning. It is an experimental feature and disabled by default. You can learn more
@@ -115,7 +116,6 @@ Here is a quick and straightforward [workaround](https://github.com/palkan/logid
115
116
 
116
117
  Nevertheless, you still need super-user privileges to enable `hstore` extension (or you can use [PostgreSQL Extension Whitelisting](https://github.com/dimitri/pgextwlist)).
117
118
 
118
-
119
119
  ## Upgrade from previous versions
120
120
 
121
121
  We try to make an upgrade process as simple as possible. For now, the only required action is to create and run a migration:
@@ -198,8 +198,8 @@ create a new version with old data. Caveat: when switching to a newer version, `
198
198
 
199
199
  ```ruby
200
200
  post = Post.create!(title: "first post") # v1
201
- post.update!(title: "new title") # v2
202
- post.undo!(append: true) # v3 (with same attributes as v1)
201
+ post.update!(title: "new title") # v2
202
+ post.undo!(append: true) # v3 (with same attributes as v1)
203
203
  ```
204
204
 
205
205
  Note that `redo!` will not work after `undo!(append: true)` because the latter will create a new version
@@ -227,29 +227,27 @@ class User < ActiveRecord::Base
227
227
  end
228
228
  ```
229
229
 
230
- If you want Logidze always to behave this way - you can set up a global configuration option:
230
+ If you want Logidze to behave this way by default, configure the global option:
231
231
 
232
232
  ```ruby
233
- Rails.application.config.logidze.ignore_log_data_by_default = true
234
- ```
233
+ # config/initializers/logidze.rb
234
+ Logidze.ignore_log_data_by_default = true
235
235
 
236
- However, you can override it by explicitly passing `ignore_log_data: false` to the `ignore_log_data`. Also, it's possible to change it temporary inside the block:
236
+ # or
237
237
 
238
- ```ruby
239
- Logidze.with_log_data do
240
- Post.find(params[:id]).log_data
241
- end
238
+ # config/application.rb
239
+ config.logidze.ignore_log_data_by_default = true
242
240
  ```
243
241
 
244
- When `ignore_log_data` is turned on, each time you use `User.all` (or any other Relation method) `log_data` won't be loaded from the DB.
242
+ However, you can override it by explicitly passing `ignore_log_data: false` to the `ignore_log_data`.
243
+ You can also enforce loading `log_data` in-place by using the `.with_log_data` scope, e.g. `User.all.with_log_data` loads all
244
+ the _users_ with `log_data` included.
245
245
 
246
246
  The chart below shows the difference in PG query time before and after turning `ignore_log_data` on. (Special thanks to [@aderyabin](https://github.com/aderyabin) for sharing it.)
247
247
 
248
248
  ![](./assets/pg_log_data_chart.png)
249
249
 
250
- If you try to call `#log_data` on the model loaded in such way, you'll get `ActiveModel::MissingAttributeError`, but if you really need it (e.g., during the console debugging) - use **`user.reload_log_data`**, which forces loading the column from the DB.
251
-
252
- If you need to select `log_data` during the initial load-use a special scope `User.with_log_data`.
250
+ If you try to call `#log_data` on the model loaded in such way, you'll get `nil`. If you want to fetch log data (e.g., during the console debugging)use **`user.reload_log_data`**, which forces loading this column from the DB.
253
251
 
254
252
  ## Track meta information
255
253
 
@@ -33,12 +33,6 @@ module Logidze
33
33
  @ignore_log_data_by_default || false
34
34
  end
35
35
 
36
- attr_writer :force_load_log_data
37
-
38
- def force_load_log_data
39
- @force_load_log_data || false
40
- end
41
-
42
36
  # Temporary disable DB triggers.
43
37
  #
44
38
  # @example
@@ -51,16 +45,5 @@ module Logidze
51
45
  res
52
46
  end
53
47
  end
54
-
55
- # Temporary turn off ignore_log_data_by_default config
56
- #
57
- # @example
58
- # Logidze.with_log_data { Post.update_all(active: true) }
59
- def with_log_data
60
- Logidze.force_load_log_data = true
61
- yield
62
- ensure
63
- Logidze.force_load_log_data = false
64
- end
65
48
  end
66
49
  end
@@ -4,6 +4,8 @@ require "logidze"
4
4
 
5
5
  module Logidze
6
6
  class Engine < Rails::Engine # :nodoc:
7
+ config.logidze = Logidze
8
+
7
9
  initializer "extend ActiveRecord with Logidze" do |_app|
8
10
  ActiveSupport.on_load(:active_record) do
9
11
  ActiveRecord::Base.send :include, Logidze::HasLogidze
@@ -10,19 +10,17 @@ module Logidze
10
10
  module ClassMethods # :nodoc:
11
11
  # Include methods to work with history.
12
12
  #
13
- def has_logidze(ignore_log_data: nil)
13
+ def has_logidze(ignore_log_data: Logidze.ignore_log_data_by_default)
14
14
  include Logidze::Model
15
15
  include Logidze::IgnoreLogData
16
16
 
17
17
  @ignore_log_data = ignore_log_data
18
+
19
+ self.ignored_columns += ["log_data"] if @ignore_log_data
18
20
  end
19
21
 
20
22
  def ignores_log_data?
21
- if @ignore_log_data.nil? && Logidze.ignore_log_data_by_default
22
- !Logidze.force_load_log_data
23
- else
24
- @ignore_log_data
25
- end
23
+ @ignore_log_data
26
24
  end
27
25
  end
28
26
  end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "logidze/ignore_log_data/association"
4
-
5
3
  module Logidze
6
4
  module IgnoreLogData # :nodoc:
7
5
  extend ActiveSupport::Concern
@@ -9,49 +7,18 @@ module Logidze
9
7
  included do
10
8
  if Rails::VERSION::MAJOR == 4
11
9
  require "logidze/ignore_log_data/ignored_columns"
12
- else
13
- if Rails::VERSION::MAJOR == 5
14
- require "logidze/ignore_log_data/cast_attribute_patch"
15
- include CastAttributePatch
16
- end
17
-
18
- require "logidze/ignore_log_data/missing_attribute_patch"
19
- include MissingAttributePatch
20
-
21
- require "logidze/ignore_log_data/default_scope_patch"
22
- include DefaultScopePatch
10
+ elsif Rails::VERSION::MAJOR == 5
11
+ require "logidze/ignore_log_data/cast_attribute_patch"
12
+ include CastAttributePatch
23
13
  end
24
14
 
25
- self.ignored_columns += ["log_data"]
26
-
27
15
  scope :with_log_data, lambda {
28
16
  if ignored_columns == ["log_data"]
29
- select("*")
17
+ select(arel_table[Arel.star])
30
18
  else
31
- select(column_names + ["log_data"])
19
+ select(column_names + [arel_table[:log_data]])
32
20
  end
33
21
  }
34
22
  end
35
-
36
- module ClassMethods # :nodoc:
37
- def self.default_scope
38
- ignores_log_data? ? super : super.with_log_data
39
- end
40
- end
41
-
42
- def association(name)
43
- association = super
44
-
45
- should_load_log_data =
46
- attributes["log_data"] &&
47
- association.klass.respond_to?(:has_logidze?) &&
48
- !association.singleton_class.include?(Logidze::IgnoreLogData::Association)
49
-
50
- return association unless should_load_log_data
51
-
52
- association.singleton_class.prepend(Logidze::IgnoreLogData::Association)
53
-
54
- association
55
- end
56
23
  end
57
24
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Logidze
4
- VERSION = "0.10.0"
4
+ VERSION = "0.11.0"
5
5
  end
@@ -5,17 +5,17 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
  require "logidze/version"
6
6
 
7
7
  Gem::Specification.new do |spec|
8
- spec.name = "logidze"
9
- spec.version = Logidze::VERSION
10
- spec.authors = ["palkan"]
11
- spec.email = ["dementiev.vm@gmail.com"]
8
+ spec.name = "logidze"
9
+ spec.version = Logidze::VERSION
10
+ spec.authors = ["palkan"]
11
+ spec.email = ["dementiev.vm@gmail.com"]
12
12
 
13
- spec.summary = "PostgreSQL JSON-based auditing"
14
- spec.description = "PostgreSQL JSON-based auditing"
15
- spec.homepage = "http://github.com/palkan/logidze"
16
- spec.license = "MIT"
13
+ spec.summary = "PostgreSQL JSON-based auditing"
14
+ spec.description = "PostgreSQL JSON-based auditing"
15
+ spec.homepage = "http://github.com/palkan/logidze"
16
+ spec.license = "MIT"
17
17
 
18
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.required_ruby_version = ">= 2.4.0"
@@ -36,6 +36,6 @@ Gem::Specification.new do |spec|
36
36
  spec.add_development_dependency "rake", ">= 10.0"
37
37
  spec.add_development_dependency "rspec-rails", ">= 3.4"
38
38
  spec.add_development_dependency "rubocop-md", "~> 0.2.0"
39
- spec.add_development_dependency "standard", "~> 0.0.39"
39
+ spec.add_development_dependency "standard", "~> 0.1.2"
40
40
  spec.add_development_dependency "timecop", "~> 0.8"
41
41
  end
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.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - palkan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-15 00:00:00.000000000 Z
11
+ date: 2019-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -114,14 +114,14 @@ dependencies:
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: 0.0.39
117
+ version: 0.1.2
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: 0.0.39
124
+ version: 0.1.2
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: timecop
127
127
  requirement: !ruby/object:Gem::Requirement
@@ -193,9 +193,7 @@ files:
193
193
  - lib/logidze/ignore_log_data.rb
194
194
  - lib/logidze/ignore_log_data/association.rb
195
195
  - lib/logidze/ignore_log_data/cast_attribute_patch.rb
196
- - lib/logidze/ignore_log_data/default_scope_patch.rb
197
196
  - lib/logidze/ignore_log_data/ignored_columns.rb
198
- - lib/logidze/ignore_log_data/missing_attribute_patch.rb
199
197
  - lib/logidze/meta.rb
200
198
  - lib/logidze/migration.rb
201
199
  - lib/logidze/model.rb
@@ -226,7 +224,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
226
224
  - !ruby/object:Gem::Version
227
225
  version: '0'
228
226
  requirements: []
229
- rubygems_version: 3.0.3
227
+ rubygems_version: 3.0.4
230
228
  signing_key:
231
229
  specification_version: 4
232
230
  summary: PostgreSQL JSON-based auditing
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Logidze
4
- module IgnoreLogData
5
- # Since Rails caches ignored_columns, we have to patch .column_names and
6
- # .unscoped methods to make conditional log_data loading possible
7
- module DefaultScopePatch
8
- extend ActiveSupport::Concern
9
-
10
- class_methods do
11
- def column_names
12
- ignores_log_data? ? super : super + ["log_data"]
13
- end
14
-
15
- def unscoped
16
- if ignores_log_data?
17
- super
18
- else
19
- block_given? ? with_log_data.scoping { yield } : with_log_data
20
- end
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Logidze
4
- module IgnoreLogData
5
- # Rails attribute API defines attributes in a way, that it returns nil
6
- # when data has not been loaded from the DB. We want it to imitate the behavior
7
- # from Rails 4 - raise ActiveModel::MissingAttributeError
8
- module MissingAttributePatch
9
- def log_data
10
- if self.class.ignores_log_data? && attributes["log_data"].nil?
11
- raise ActiveModel::MissingAttributeError
12
- end
13
-
14
- super
15
- end
16
- end
17
- end
18
- end