historiographer 1.3.1 → 1.4.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: f2d85a348f2e7196cccef533c9562b53c9449df6a0dde03509a1d48a5d66c490
4
- data.tar.gz: f3b9f822e53fdadc04d20a0f4066b8b47e019c11333d9dccc0140ea1e9f12991
3
+ metadata.gz: e2c323e0b16003b066c298e281147b86e810ee0f2b7cc5bc859bfad1c861795b
4
+ data.tar.gz: 04b81492d7a2a24a8aa46935f64874f77521884dec9cd9aa2fd568785b399847
5
5
  SHA512:
6
- metadata.gz: 5011fe73d7a09abd888f362b1e3923436c5f29068f9b46650ac4e39f09ab3226d7f2d66072293d9908dc37693a7afc5b73f73c2db2c1f6f1b0e9119c153c511b
7
- data.tar.gz: ccca124547a124739d68183b9374ddd1b8fc452ed29b568da485981abb37cab8f055384065b00016981ea3844177c5e7e050d0fb6e5f8faef7844e5835387591
6
+ metadata.gz: 8716822fb73b440fedb427ea732d87e15a4ffb4bc3ca902bfa57e4cbf5e9303fbc6a25482eadcbc9d00830eede9664685db8d05b05438a5fe08c32fa79c8e3ee
7
+ data.tar.gz: 86ce1d84a483a5384aea9e0d043911edc0f1cb3f3bf6f970d1dccee22d3369a2ef6437f65e2de6e46f9df4ec68a76b189411805ba98cca4874b39513c1162b69
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.1
1
+ 1.4.0
@@ -2,11 +2,11 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: historiographer 1.3.1 ruby lib
5
+ # stub: historiographer 1.4.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "historiographer".freeze
9
- s.version = "1.3.1"
9
+ s.version = "1.4.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
@@ -15,35 +15,42 @@ module Historiographer
15
15
  super(updates)
16
16
  else
17
17
  updates.symbolize_keys!
18
+ model_changes = updates.except(:history_user_id)
18
19
 
19
20
  ActiveRecord::Base.transaction do
20
- super(updates.except(:history_user_id))
21
- now = UTC.now
22
- records = self.reload
23
- history_class = records.klass.history_class
24
-
25
- records.new.send(:history_user_absent_action) if updates[:history_user_id].nil?
26
- history_user_id = updates[:history_user_id]
27
-
28
- new_histories = records.map do |record|
29
- attrs = record.attributes.clone
30
- foreign_key = history_class.history_foreign_key
31
-
32
- now = UTC.now
33
- attrs.merge!(foreign_key => attrs["id"], history_started_at: now, history_user_id: history_user_id)
34
-
35
- attrs = attrs.except("id")
36
-
37
- record.histories.build(attrs)
21
+ changed_records = select do |record|
22
+ !(record.attributes.symbolize_keys >= model_changes)
38
23
  end
39
24
 
40
- current_histories = history_class.current.where("#{history_class.history_foreign_key} IN (?)", records.map(&:id))
25
+ super(model_changes)
26
+ bulk_record_history(self.reload.where(id: changed_records.pluck(:id)), updates)
27
+ end
28
+ end
29
+ end
41
30
 
42
- current_histories.update_all(history_ended_at: now)
31
+ def bulk_record_history(records, updates = {})
32
+ now = UTC.now
33
+ history_class = self.klass.history_class
43
34
 
44
- history_class.import new_histories
45
- end
35
+ records.new.send(:history_user_absent_action) if updates[:history_user_id].nil?
36
+ history_user_id = updates[:history_user_id]
37
+
38
+ new_histories = records.map do |record|
39
+ attrs = record.attributes.clone
40
+ foreign_key = history_class.history_foreign_key
41
+
42
+ attrs.merge!(foreign_key => attrs["id"], history_started_at: now, history_user_id: history_user_id)
43
+
44
+ attrs = attrs.except("id")
45
+
46
+ record.histories.build(attrs)
46
47
  end
48
+
49
+ current_histories = history_class.current.where("#{history_class.history_foreign_key} IN (?)", records.map(&:id))
50
+
51
+ current_histories.update_all(history_ended_at: now)
52
+
53
+ history_class.import new_histories
47
54
  end
48
55
 
49
56
  def delete_all_without_history
@@ -91,4 +98,4 @@ module Historiographer
91
98
  records.each { |r| r.destroy(history_user_id: history_user_id) }.tap { reset }
92
99
  end
93
100
  end
94
- end
101
+ end
@@ -164,9 +164,14 @@ describe Historiographer do
164
164
  expect(posts.second.current_history.title).to eq "My New Post Title"
165
165
  expect(posts.third.current_history.title).to eq "Brett's Post"
166
166
 
167
+ # It does not update histories if nothing changed
168
+ Post.all.update_all(title: "Brett's Post", history_user_id: 1)
169
+ posts = Post.all.reload.order(:id)
170
+ expect(posts.map(&:histories).map(&:count)).to all(eq 3)
171
+
167
172
  posts.update_all_without_history(title: "Untracked")
168
173
  expect(posts.first.histories.count).to eq 3
169
- expect(posts.second.histories.count).to eq 2
174
+ expect(posts.second.histories.count).to eq 3
170
175
  expect(posts.third.histories.count).to eq 3
171
176
 
172
177
  thing1 = ThingWithoutHistory.create(name: "Thing 1")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: historiographer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - brettshollenberger