File tree Expand file tree Collapse file tree 2 files changed +29
-11
lines changed
lib/activerecord-bitemporal
spec/activerecord-bitemporal Expand file tree Collapse file tree 2 files changed +29
-11
lines changed Original file line number Diff line number Diff line change @@ -346,17 +346,20 @@ def destroy(force_delete: false, operated_at: Time.current)
346346 _run_destroy_callbacks {
347347 @destroyed = update_transaction_to ( operated_at )
348348
349- # 削除時の状態を履歴レコードとして保存する
350- duplicated_instance . valid_to = target_datetime
351- duplicated_instance . transaction_from = operated_at
352- duplicated_instance . save_without_bitemporal_callbacks! ( validate : false )
353- if @destroyed
354- @_swapped_id_previously_was = swapped_id
355- @_swapped_id = duplicated_instance . swapped_id
356- self . valid_from = duplicated_instance . valid_from
357- self . valid_to = duplicated_instance . valid_to
358- self . transaction_from = duplicated_instance . transaction_from
359- self . transaction_to = duplicated_instance . transaction_to
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
360363 end
361364 }
362365 raise ActiveRecord ::RecordInvalid unless @destroyed
Original file line number Diff line number Diff line change @@ -1204,6 +1204,21 @@ class EmployeeWithUniquness < Employee
12041204 it { expect { subject } . to change ( employee , :transaction_from ) . from ( updated_time ) . to ( destroyed_time ) }
12051205 it { expect { subject } . not_to change ( employee , :transaction_to ) }
12061206 end
1207+
1208+ context "with `#force_update`" do
1209+ subject { Timecop . freeze ( destroyed_time ) { employee . force_update { employee . destroy } } }
1210+
1211+ it { expect { subject } . to change ( Employee , :count ) . by ( -1 ) }
1212+ it { expect { subject } . to change ( employee , :destroyed? ) . from ( false ) . to ( true ) }
1213+ it { expect { subject } . not_to change ( employee , :valid_from ) }
1214+ it { expect { subject } . not_to change ( employee , :valid_to ) }
1215+ it { expect { subject } . not_to change ( employee , :transaction_from ) }
1216+ it { expect { subject } . to change ( employee , :transaction_to ) . from ( ActiveRecord ::Bitemporal ::DEFAULT_TRANSACTION_TO ) . to ( destroyed_time ) }
1217+ it { expect { subject } . not_to change { Employee . ignore_valid_datetime . within_deleted . count } }
1218+ it { expect { subject } . not_to change ( employee , :swapped_id ) }
1219+ it { expect { subject } . not_to change ( employee , :swapped_id_previously_was ) }
1220+ it { expect ( subject ) . to eq employee }
1221+ end
12071222 end
12081223
12091224 describe "#touch" do
You can’t perform that action at this time.
0 commit comments