Skip to content

Commit

Permalink
deleted_at value assign to transaction_to.
Browse files Browse the repository at this point in the history
  • Loading branch information
osyo-manga committed Aug 31, 2020
1 parent 6a5225b commit 8bd896b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/activerecord-bitemporal/bitemporal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,9 @@ def _create_record(attribute_names = self.attribute_names)
self.transaction_from = self.created_at if changes.key?("created_at")
self.created_at = self.transaction_from
end
if has_column?(:deleted_at) && self.transaction_to != ActiveRecord::Bitemporal::DEFAULT_TRANSACTION_TO
self.deleted_at = self.transaction_to
if has_column?(:deleted_at)
self.transaction_to = self.deleted_at if changes.key?("deleted_at")
self.deleted_at = self.transaction_to == ActiveRecord::Bitemporal::DEFAULT_TRANSACTION_TO ? nil : self.transaction_to
end

# アソシエーションの子に対して `valid_from` を設定
Expand Down
51 changes: 51 additions & 0 deletions spec/activerecord-bitemporal/transaction_at_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,57 @@
end
end

describe "#create" do
let(:_01_01) { "2020/01/01".to_time }
let(:_04_01) { "2020/04/01".to_time }
let(:_08_01) { "2020/08/01".to_time }
let(:_12_01) { "2020/12/01".to_time }
subject { Company.create(params) }

context "params is empty" do
let(:params) { {} }
it { expect(subject.created_at.iso8601(6)).to eq subject.transaction_from.iso8601(6) }
it { expect(subject.deleted_at).to eq nil }
it { expect(subject.transaction_to).to eq ActiveRecord::Bitemporal::DEFAULT_TRANSACTION_TO }
end

context "set `created_at`" do
let(:params) { { created_at: _01_01 } }
it { is_expected.to have_attributes(created_at: _01_01, transaction_from: _01_01) }
end
context "set `transaction_from`" do
let(:params) { { transaction_from: _01_01 } }
it { is_expected.to have_attributes(created_at: _01_01, transaction_from: _01_01) }
end
context "set `created_at` and `transaction_from`" do
let(:params) { { created_at: _01_01, transaction_from: _04_01 } }
it { is_expected.to have_attributes(created_at: _01_01, transaction_from: _01_01) }
end

context "set `deleted_at`" do
let(:params) { { deleted_at: _01_01 } }
it { is_expected.to have_attributes(deleted_at: _01_01, transaction_to: _01_01) }

context "to `nil`" do
let(:params) { { deleted_at: nil } }
it { is_expected.to have_attributes(deleted_at: nil, transaction_to: ActiveRecord::Bitemporal::DEFAULT_TRANSACTION_TO) }
end
end
context "set `transaction_to`" do
let(:params) { { transaction_to: _01_01 } }
it { is_expected.to have_attributes(deleted_at: _01_01, transaction_to: _01_01) }
end
context "set `deleted_at` and `transaction_to`" do
let(:params) { { deleted_at: _01_01, transaction_to: _04_01 } }
it { is_expected.to have_attributes(deleted_at: _01_01, transaction_to: _01_01) }

context "`deleted_at` to `nil`" do
let(:params) { { deleted_at: nil, transaction_to: _04_01 } }
it { is_expected.to have_attributes(deleted_at: _04_01, transaction_to: _04_01) }
end
end
end

describe "#update" do
let(:company) { Company.create(name: "Company1") }
define_method(:company_all) { Company.ignore_valid_datetime.within_deleted.bitemporal_for(company.id).order(:transaction_from) }
Expand Down

0 comments on commit 8bd896b

Please sign in to comment.