activerecord-bitemporal 4.0.0 → 4.2.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: 4be4f1dd46241e22de6ea7acf687531b3313fdd83f2deab6e9a7d249cc6b5853
4
- data.tar.gz: 70b235bbe03555fa5db6ba0309af3147d2fb4f80708a13ac421944f6c4603a0b
3
+ metadata.gz: 97a21996b6ff931bf324653432cd481eac86731ee3de16445c98627987683639
4
+ data.tar.gz: 34178dcfc6e5fe4cbd1f01554499f075d1be90208f05cc2d39a0039941d03607
5
5
  SHA512:
6
- metadata.gz: 8817c135d2f0ea3ec5d8979f0360ac5772ad32ff244d5ebf65b6111954452e41ada1b9466669313775356cf3838fc522289344125392c9a4700c869c6705c0fd
7
- data.tar.gz: 28a09497a4e6d7c085a6e44b6f4262a6fa2d6ac0f79d499b1bd6d6b946bacaaf78a14ab1f2389d9d96a719fee4168c0374ddad5b4994b34e07f8738c6462ba0f
6
+ metadata.gz: da5f747f97ec3c16f14b65d592482b23cc8a0d8ba2d1c691dc98df8b5f84d5dfd8c92e8be67362f8b6be38b0bd584c2bfdd97f5169f9e4d91347a7286ad87770
7
+ data.tar.gz: 2a1b5d95ac36a751943a87adcb9231b81633f61139a6ca38ca3d34859a7e9a2f647ce204b549eda50efca9cc48245d7aff7c21499397d4ef673d1f4c02de84a4
data/.gitignore CHANGED
@@ -1,2 +1,4 @@
1
1
  .bundle/
2
+ .rspec_status
3
+ Gemfile.lock
2
4
  gemfiles/*.gemfile.lock
data/CHANGELOG.md CHANGED
@@ -1,5 +1,41 @@
1
1
  # Changelog
2
2
 
3
+ ## 4.2.0
4
+
5
+ ### Added
6
+
7
+ - [Allow passing `operated_at` to destroy #138](https://github.com/kufu/activerecord-bitemporal/pull/138)
8
+
9
+ ### Changed
10
+
11
+ - [Change not to create history when destroying with `force_update` #135](https://github.com/kufu/activerecord-bitemporal/pull/135)
12
+ - [Raise `ValidDatetimeRangeError` instead of `RecordInvalid` in `_update_row` #136](https://github.com/kufu/activerecord-bitemporal/pull/136)
13
+
14
+ ### Deprecated
15
+
16
+ ### Removed
17
+
18
+ ### Fixed
19
+
20
+ ### Chores
21
+
22
+ - [Fix for RSpec deprecated warning #132](https://github.com/kufu/activerecord-bitemporal/pull/132)
23
+ - [Add some files to .gitignore #134](https://github.com/kufu/activerecord-bitemporal/pull/134)
24
+
25
+ ## 4.1.0
26
+
27
+ ### Added
28
+ - [add label option #127](https://github.com/kufu/activerecord-bitemporal/pull/127)
29
+
30
+ ### Changed
31
+ - [Support for inverse_of of Rails 6.1 or higher #130](https://github.com/kufu/activerecord-bitemporal/pull/130)
32
+
33
+ ### Deprecated
34
+
35
+ ### Removed
36
+
37
+ ### Fixed
38
+
3
39
  ## 4.0.0
4
40
 
5
41
  ### Breaking Changed
@@ -334,30 +334,32 @@ module ActiveRecord
334
334
  end || false
335
335
  end
336
336
 
337
- def destroy(force_delete: false)
337
+ def destroy(force_delete: false, operated_at: Time.current)
338
338
  return super() if force_delete
339
339
 
340
- current_time = Time.current
341
- target_datetime = valid_datetime || current_time
340
+ target_datetime = valid_datetime || operated_at
342
341
 
343
342
  duplicated_instance = self.class.find_at_time(target_datetime, self.id).dup
344
343
 
345
344
  ActiveRecord::Base.transaction(requires_new: true, joinable: false) do
346
345
  @destroyed = false
347
346
  _run_destroy_callbacks {
348
- @destroyed = update_transaction_to(current_time)
349
-
350
- # 削除時の状態を履歴レコードとして保存する
351
- duplicated_instance.valid_to = target_datetime
352
- duplicated_instance.transaction_from = current_time
353
- duplicated_instance.save_without_bitemporal_callbacks!(validate: false)
354
- if @destroyed
355
- @_swapped_id_previously_was = swapped_id
356
- @_swapped_id = duplicated_instance.swapped_id
357
- self.valid_from = duplicated_instance.valid_from
358
- self.valid_to = duplicated_instance.valid_to
359
- self.transaction_from = duplicated_instance.transaction_from
360
- self.transaction_to = duplicated_instance.transaction_to
347
+ @destroyed = update_transaction_to(operated_at)
348
+
349
+ # force_update の場合は削除時の状態の履歴を残さない
350
+ unless force_update?
351
+ # 削除時の状態を履歴レコードとして保存する
352
+ duplicated_instance.valid_to = target_datetime
353
+ duplicated_instance.transaction_from = operated_at
354
+ duplicated_instance.save_without_bitemporal_callbacks!(validate: false)
355
+ if @destroyed
356
+ @_swapped_id_previously_was = swapped_id
357
+ @_swapped_id = duplicated_instance.swapped_id
358
+ self.valid_from = duplicated_instance.valid_from
359
+ self.valid_to = duplicated_instance.valid_to
360
+ self.transaction_from = duplicated_instance.transaction_from
361
+ self.transaction_to = duplicated_instance.transaction_to
362
+ end
361
363
  end
362
364
  }
363
365
  raise ActiveRecord::RecordInvalid unless @destroyed
@@ -499,13 +501,17 @@ module ActiveRecord
499
501
 
500
502
  # 以前の履歴データは valid_to を詰めて保存
501
503
  before_instance.valid_to = target_datetime
502
- raise ActiveRecord::RecordInvalid.new(before_instance) if before_instance.valid_from_cannot_be_greater_equal_than_valid_to
504
+ if before_instance.valid_from_cannot_be_greater_equal_than_valid_to
505
+ raise ValidDatetimeRangeError.new("valid_from #{before_instance.valid_from} can't be greater equal than valid_to #{before_instance.valid_to}")
506
+ end
503
507
  before_instance.transaction_from = current_time
504
508
 
505
509
  # 以降の履歴データは valid_from と valid_to を調整して保存する
506
510
  after_instance.valid_from = target_datetime
507
511
  after_instance.valid_to = current_valid_record.valid_to
508
- raise ActiveRecord::RecordInvalid.new(after_instance) if after_instance.valid_from_cannot_be_greater_equal_than_valid_to
512
+ if after_instance.valid_from_cannot_be_greater_equal_than_valid_to
513
+ raise ValidDatetimeRangeError.new("valid_from #{after_instance.valid_from} can't be greater equal than valid_to #{after_instance.valid_to}")
514
+ end
509
515
  after_instance.transaction_from = current_time
510
516
 
511
517
  # 有効なレコードがない場合
@@ -10,7 +10,7 @@ module ActiveRecord::Bitemporal
10
10
  define_model_callbacks :bitemporal_destroy
11
11
  end
12
12
 
13
- def destroy
13
+ def destroy(...)
14
14
  perform_bitemporal_callbacks? ? run_callbacks(:bitemporal_destroy) { super } : super
15
15
  end
16
16
 
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord::Bitemporal
4
+ class BitemporalError < StandardError; end
5
+
6
+ class ValidDatetimeRangeError < BitemporalError; end
7
+ end
@@ -79,6 +79,18 @@ module ActiveRecord::Bitemporal
79
79
  def bi_temporal_model?
80
80
  owner.class.bi_temporal_model? && klass&.bi_temporal_model?
81
81
  end
82
+
83
+ def matches_foreign_key?(record)
84
+ return super unless owner.class.bi_temporal_model?
85
+
86
+ begin
87
+ original_owner_id = owner.id
88
+ owner.id = owner.read_attribute(owner.class.bitemporal_id_key)
89
+ super
90
+ ensure
91
+ owner.id = original_owner_id
92
+ end
93
+ end
82
94
  end
83
95
 
84
96
  module ThroughAssociation
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module Bitemporal
5
- VERSION = "4.0.0"
5
+ VERSION = "4.2.0"
6
6
  end
7
7
  end
@@ -91,8 +91,17 @@ module ActiveRecord::Bitemporal
91
91
  end
92
92
  end
93
93
  end
94
-
95
- "#{headers.to_s}\n#{body.to_s}"
94
+
95
+ transaction_label = 'transaction_datetime'
96
+ right_margin = time_length + 1 - transaction_label.size
97
+
98
+ label = if right_margin >= 0
99
+ "#{transaction_label + ' ' * right_margin}| valid_datetime"
100
+ else
101
+ "#{transaction_label[0...right_margin]}| valid_datetime"
102
+ end
103
+
104
+ "#{label}\n#{headers.to_s}\n#{body.to_s}"
96
105
  end
97
106
 
98
107
  # Compute a dictionary of where each time should be plotted.
@@ -4,6 +4,7 @@ require "active_record"
4
4
  require "active_support/core_ext/time/calculations"
5
5
  require "activerecord-bitemporal/bitemporal"
6
6
  require "activerecord-bitemporal/scope"
7
+ require "activerecord-bitemporal/errors"
7
8
  require "activerecord-bitemporal/patches"
8
9
  require "activerecord-bitemporal/version"
9
10
  require "activerecord-bitemporal/visualizer"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-bitemporal
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - SmartHR
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-03-01 00:00:00.000000000 Z
11
+ date: 2023-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -166,6 +166,7 @@ files:
166
166
  - lib/activerecord-bitemporal.rb
167
167
  - lib/activerecord-bitemporal/bitemporal.rb
168
168
  - lib/activerecord-bitemporal/callbacks.rb
169
+ - lib/activerecord-bitemporal/errors.rb
169
170
  - lib/activerecord-bitemporal/patches.rb
170
171
  - lib/activerecord-bitemporal/scope.rb
171
172
  - lib/activerecord-bitemporal/version.rb
@@ -189,7 +190,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
189
190
  - !ruby/object:Gem::Version
190
191
  version: '0'
191
192
  requirements: []
192
- rubygems_version: 3.1.6
193
+ rubygems_version: 3.3.26
193
194
  signing_key:
194
195
  specification_version: 4
195
196
  summary: BiTemporal Data Model for ActiveRecord