activerecord-bitemporal 4.1.0 → 4.2.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: '09a592f0b1e43e737bd1fdbc8a88f025af734bf3c0d42c4fa2297dbaa72a68e5'
4
- data.tar.gz: b591ee586a0d5fc3f109444787f86bc5c399f22d33c44d84d80bde6f91b141ac
3
+ metadata.gz: 97a21996b6ff931bf324653432cd481eac86731ee3de16445c98627987683639
4
+ data.tar.gz: 34178dcfc6e5fe4cbd1f01554499f075d1be90208f05cc2d39a0039941d03607
5
5
  SHA512:
6
- metadata.gz: b0d9d4f5dd752200b2e803bca72b123ed067165bd61092755fb091b10eb35fd58b82617f7957d48f738b8f9ec09c480d8338553c3b33098202056d2d20e94d09
7
- data.tar.gz: d90e6bbb55b783bc75f650c00c4bbaf226b4d5ba67c86e004bfecbe75224031c0a0ad69c95955741c0b560bdc85fc2a4f3945d07885d0b128ecbf0416f003569
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,27 @@
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
+
3
25
  ## 4.1.0
4
26
 
5
27
  ### Added
@@ -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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module Bitemporal
5
- VERSION = "4.1.0"
5
+ VERSION = "4.2.0"
6
6
  end
7
7
  end
@@ -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.1.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-28 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