Skip to content

Commit

Permalink
Merge pull request kufu#64 from osyo-manga/add-error_info-when-updated
Browse files Browse the repository at this point in the history
Add error info when updated.
  • Loading branch information
osyo-manga authored Nov 5, 2020
2 parents 061ac59 + 067d79d commit d2825e5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
8 changes: 6 additions & 2 deletions lib/activerecord-bitemporal/bitemporal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -445,21 +445,25 @@ def _update_row(attribute_names, attempted_action = 'update')

# 以前の履歴データは valid_to を詰めて保存
before_instance.valid_to = target_datetime
raise ActiveRecord::Rollback if before_instance.valid_from_cannot_be_greater_equal_than_valid_to
raise ActiveRecord::RecordInvalid.new(before_instance) if before_instance.valid_from_cannot_be_greater_equal_than_valid_to
before_instance.transaction_from = current_time
before_instance.save!(validate: false)

# 以降の履歴データは valid_from と valid_to を調整して保存する
after_instance.valid_from = target_datetime
after_instance.valid_to = current_valid_record.valid_to
raise ActiveRecord::Rollback if after_instance.valid_from_cannot_be_greater_equal_than_valid_to
raise ActiveRecord::RecordInvalid.new(after_instance) if after_instance.valid_from_cannot_be_greater_equal_than_valid_to
after_instance.transaction_from = current_time
after_instance.save!(validate: false)

# 有効なレコードがない場合
else
# 一番近い未来にある Instance を取ってきて、その valid_from を valid_to に入れる
nearest_instance = self.class.where(bitemporal_id: bitemporal_id).bitemporal_where_bind("valid_from", :gt, target_datetime).ignore_valid_datetime.order(valid_from: :asc).first
if nearest_instance.nil?
message = "Update failed: Couldn't find #{self.class} with 'bitemporal_id'=#{self.bitemporal_id} and 'valid_from' < #{target_datetime}"
raise ActiveRecord::RecordNotFound.new(message, self.class, "bitemporal_id", self.bitemporal_id)
end

# valid_from と valid_to を調整して保存する
after_instance.valid_from = target_datetime
Expand Down
25 changes: 19 additions & 6 deletions spec/activerecord-bitemporal/bitemporal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -784,23 +784,36 @@ def employee.wrapped_name
end

context "failure" do
let(:company) { Company.create!(valid_from: "2019/2/1") }
let(:company_count) { -> { Company.ignore_valid_datetime.bitemporal_for(company.id).count } }
let(:company_deleted_at) { -> { Company.ignore_valid_datetime.within_deleted.bitemporal_for(company.id).first.deleted_at } }
subject { -> { company.valid_at(valid_datetime) { |c| c.update(name: "Company") } } }

context "`valid_datetime` is `company.valid_from`" do
let(:company) { Company.create!(valid_from: "2019/2/1") }
let(:company_count) { -> { Company.ignore_valid_datetime.bitemporal_for(company.id).count } }
let(:company_deleted_at) { -> { Company.ignore_valid_datetime.within_deleted.bitemporal_for(company.id).first.deleted_at } }
let(:valid_datetime) { company.valid_from }
subject { -> { company.valid_at(valid_datetime) { |c| c.update(name: "Company") } } }

it { expect(subject.call).to be_falsey }
it { is_expected.not_to change(&company_count) }
it { is_expected.not_to change(&company_deleted_at) }

context "call `update!`" do
subject { -> { company.valid_at(valid_datetime) { |c| c.update!(name: "Company") } } }
it { is_expected.to raise_error(ActiveRecord::RecordNotSaved) }
it { is_expected.to raise_error(ActiveRecord::RecordInvalid) }
it { is_expected.to raise_error { |e|
expect(e.message).to eq "Validation failed: Valid from can't be greater equal than valid_to"
} }
end
end

context "update for deleted record" do
let(:datetime) { "2020/01/01".in_time_zone }
let(:company) { Company.create!(valid_from: "2019/02/01", valid_to: "2019/04/01") }
subject { -> { Timecop.freeze(datetime) { company.update!(name: "Company2") } } }
before { company.destroy }
it { is_expected.to raise_error(ActiveRecord::RecordNotFound) }
it { is_expected.to raise_error { |e|
expect(e.message).to eq "Update failed: Couldn't find Company with 'bitemporal_id'=#{company.bitemporal_id} and 'valid_from' < #{datetime}"
} }
end
end
end

Expand Down

0 comments on commit d2825e5

Please sign in to comment.