logidze 0.8.1 → 0.9.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: 571e06644d88083ccf41f25a69c13530436055233406337c7458f8cdbb6d86f0
4
- data.tar.gz: f7301fee68a37cfb436a2b3f1ad80ebb8e375b8bc936ee39894f036273edbf35
3
+ metadata.gz: 0f853c7aed1f6d284150d85530b3fdc0fb8f59b856a4d962e35004507cb8c7f7
4
+ data.tar.gz: 513c193a93b6ff059a27463ee96ab754cb88d995b74a8c73b2abf16441aad346
5
5
  SHA512:
6
- metadata.gz: 4430563010f78a5f467fad89a260ba0a299d5ac892d4ca63728f4a0dd1ff2e7cb210fa05c2aba513298fd069fbd951c930aeedfd2f401e9e15af1cde6081a302
7
- data.tar.gz: 336576627fc5c4b54fadcc531406579328497bff61b4db47973a442b6b7ae0710d7f0f953da360dbe0f4e22abfeeadfe411c342919d0ab212a73bfae7a2731b5
6
+ metadata.gz: 54443a9ddd4b9bea05a9f6e8acd5f0549c359f2476e93d6cb11452597ba8d0b2580b4ac3db78a0bc78e0ec01f368e9e8c6c0de08868e4d6e7679641515c22329
7
+ data.tar.gz: 27f28a1e2b8be8b9eb8095ef8ffd192e4397ff3389298cf290991fc76de7998f9c512fe1d153505dfdd6a69186b06365d5085c71d03472b573ec3c6cb3f523b3
@@ -2,6 +2,30 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 0.9.0 (2018-11-28)
6
+
7
+ - PR [#98](https://github.com/palkan/logidze/pull/98) Add `:ignore_log_data` option to `#has_logidze` ([@dmitrytsepelev][])
8
+
9
+ Usage:
10
+
11
+ ```ruby
12
+ class User < ActiveRecord::Base
13
+ has_logidze ignore_log_data: true
14
+ end
15
+
16
+ User.all #=> SELECT id, name FROM users
17
+
18
+ User.with_log_data #=> SELECT id, name, log_data FROM users
19
+
20
+ user = User.find(params[:id])
21
+ user.log_data #=> ActiveModel::MissingAttributeError
22
+ user.reload_log_data #=> Logidze::History
23
+ ```
24
+
25
+ ## 0.8.1 (2018-10-22)
26
+
27
+ - [PR #93](https://github.com/palkan/logidze/pull/93)] Return 0 for log size when log_data is nil ([@duderman][])
28
+
5
29
  ## 0.8.0 (2018-10-01)
6
30
 
7
31
  - [PR [#87](https://github.com/palkan/logidze/pull/87)] Adding debounce time to avoid spamming changelog creation ([@zocoi][])
@@ -211,3 +235,4 @@ This is a quick fix for a more general problem (see [#59](https://github.com/pal
211
235
  [@ankursethi-uscis]: https://github.com/ankursethi-uscis
212
236
  [@dmitrytsepelev]: https://github.com/DmitryTsepelev
213
237
  [@zocoi]: https://github.com/zocoi
238
+ [@duderman]: https://github.com/duderman
data/README.md CHANGED
@@ -186,6 +186,12 @@ post.redo!
186
186
  post.switch_to!(2)
187
187
  ```
188
188
 
189
+ You can initiate reloading of `log_data` from the DB:
190
+
191
+ ```ruby
192
+ post.reload_log_data # => returns the latest log data value
193
+ ```
194
+
189
195
  Normally, if you update record after `#undo!` or `#switch_to!` you lose all "future" versions and `#redo!` is no
190
196
  longer possible. However, you can provide an `append: true` option to `#undo!` or `#switch_to!`, which will
191
197
  create a new version with old data. Caveat: when switching to a newer version, `append` will have no effect.
@@ -204,6 +210,16 @@ Alternatively, you can configure Logidze to always default to `append: true`.
204
210
  Logidze.append_on_undo = true
205
211
  ```
206
212
 
213
+ If you want to reduce the data loaded from the DB, you can turn off automatic loading of `log_data` column in the following way:
214
+
215
+ ```ruby
216
+ class User < ActiveRecord::Base
217
+ has_logidze ignore_log_data: true
218
+ end
219
+ ```
220
+
221
+ After that, each time you use `User.all` (or any other relation method) `log_data` won't be loaded from the DB. If you try to call `#log_data` on the model loaded in a 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. If you need to select `log_data` during the initial load - use a special scope `User.with_log_data`.
222
+
207
223
 
208
224
  ## Track meta information
209
225
 
@@ -7,6 +7,7 @@ module Logidze
7
7
  require 'logidze/history'
8
8
  require 'logidze/model'
9
9
  require 'logidze/versioned_association'
10
+ require 'logidze/ignore_log_data'
10
11
  require 'logidze/has_logidze'
11
12
  require 'logidze/meta'
12
13
 
@@ -10,8 +10,9 @@ module Logidze
10
10
  # Include methods to work with history.
11
11
  #
12
12
  # rubocop:disable Naming/PredicateName
13
- def has_logidze
13
+ def has_logidze(ignore_log_data: false)
14
14
  include Logidze::Model
15
+ include Logidze::IgnoreLogData if ignore_log_data
15
16
  end
16
17
  end
17
18
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Logidze
4
+ # Add `has_logidze` method to AR::Base
5
+ module IgnoreLogData
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ if Rails::VERSION::MAJOR == 4
10
+ require "logidze/ignore_log_data/ignored_columns"
11
+ else
12
+ if Rails::VERSION::MAJOR == 5
13
+ require "logidze/ignore_log_data/cast_attribute_patch"
14
+ include CastAttributePatch
15
+ end
16
+
17
+ require "logidze/ignore_log_data/missing_attribute_patch"
18
+ include MissingAttributePatch
19
+ end
20
+
21
+ self.ignored_columns += ["log_data"]
22
+ end
23
+
24
+ module ClassMethods # :nodoc:
25
+ def with_log_data
26
+ select(column_names + ["log_data"])
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Logidze
4
+ module IgnoreLogData
5
+ # Fixes unexpected behavior (see more https://github.com/rails/rails/pull/34528):
6
+ # instead of using a type passed to `attribute` call, ignored column uses
7
+ # a type coming from the DB (in this case `.log_data` would return a plain hash
8
+ # instead of `Logidze::History`)
9
+ module CastAttributePatch
10
+ def log_data
11
+ return attributes["log_data"] if attributes["log_data"].is_a?(Logidze::History)
12
+
13
+ self.log_data = Logidze::History::Type.new.cast_value(super)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Logidze
4
+ module IgnoreLogData
5
+ # Backports ignored_columns functionality to Rails < 5
6
+ module IgnoredColumns
7
+ module Base # :nodoc:
8
+ attr_writer :ignored_columns
9
+
10
+ def ignored_columns
11
+ @ignored_columns ||= []
12
+ end
13
+ end
14
+
15
+ module Relation # :nodoc:
16
+ private
17
+
18
+ def build_select(arel)
19
+ if select_values.blank? && klass.ignored_columns.any?
20
+ arel.project(*arel_columns(klass.column_names - klass.ignored_columns))
21
+ else
22
+ super
23
+ end
24
+ end
25
+ end
26
+
27
+ module Table # :nodoc:
28
+ def columns
29
+ super.reject { |column| ignored_columns.include?(column.name) }
30
+ end
31
+
32
+ private
33
+
34
+ def ignored_columns
35
+ node.base_klass.ignored_columns
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ ActiveRecord::Base.extend(Logidze::IgnoreLogData::IgnoredColumns::Base)
43
+ ActiveRecord::Relation.include(Logidze::IgnoreLogData::IgnoredColumns::Relation)
44
+ ActiveRecord::Associations::JoinDependency::Aliases::Table.prepend(
45
+ Logidze::IgnoreLogData::IgnoredColumns::Table
46
+ )
@@ -0,0 +1,16 @@
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
+ raise ActiveModel::MissingAttributeError if attributes["log_data"].nil?
11
+
12
+ super
13
+ end
14
+ end
15
+ end
16
+ end
@@ -199,6 +199,11 @@ module Logidze
199
199
  log_data&.size || 0
200
200
  end
201
201
 
202
+ # Loads log_data field from the database, stores to the attributes hash and returns it
203
+ def reload_log_data
204
+ self.log_data = self.class.where(self.class.primary_key => id).pluck(:log_data).first
205
+ end
206
+
202
207
  protected
203
208
 
204
209
  def apply_diff(version, diff)
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Logidze
3
- VERSION = "0.8.1"
3
+ VERSION = "0.9.0"
4
4
  end
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency "pry-byebug"
27
27
  spec.add_development_dependency "rake", ">= 10.0"
28
28
  spec.add_development_dependency "rspec-rails", ">= 3.4"
29
- spec.add_development_dependency "rubocop", "~> 0.59.0"
29
+ spec.add_development_dependency "rubocop", "~> 0.60.0"
30
30
  spec.add_development_dependency "rubocop-md", "~> 0.2.0"
31
31
  spec.add_development_dependency "simplecov", ">= 0.3.8"
32
32
  spec.add_development_dependency "timecop", "~> 0.8"
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.8.1
4
+ version: 0.9.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-10-22 00:00:00.000000000 Z
11
+ date: 2018-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -128,14 +128,14 @@ dependencies:
128
128
  requirements:
129
129
  - - "~>"
130
130
  - !ruby/object:Gem::Version
131
- version: 0.59.0
131
+ version: 0.60.0
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - "~>"
137
137
  - !ruby/object:Gem::Version
138
- version: 0.59.0
138
+ version: 0.60.0
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: rubocop-md
141
141
  requirement: !ruby/object:Gem::Requirement
@@ -228,6 +228,10 @@ files:
228
228
  - lib/logidze/history.rb
229
229
  - lib/logidze/history/type.rb
230
230
  - lib/logidze/history/version.rb
231
+ - lib/logidze/ignore_log_data.rb
232
+ - lib/logidze/ignore_log_data/cast_attribute_patch.rb
233
+ - lib/logidze/ignore_log_data/ignored_columns.rb
234
+ - lib/logidze/ignore_log_data/missing_attribute_patch.rb
231
235
  - lib/logidze/meta.rb
232
236
  - lib/logidze/migration.rb
233
237
  - lib/logidze/model.rb