Skip to content

Commit

Permalink
Merge pull request #136 from wata727/raise_valid_datetime_range_error
Browse files Browse the repository at this point in the history
Raise ValidDatetimeRangeError instead of RecordInvalid in _update_row
  • Loading branch information
wata727 authored Jul 21, 2023
2 parents ccd2d80 + 805a74d commit e52a640
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/activerecord-bitemporal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "active_support/core_ext/time/calculations"
require "activerecord-bitemporal/bitemporal"
require "activerecord-bitemporal/scope"
require "activerecord-bitemporal/errors"
require "activerecord-bitemporal/patches"
require "activerecord-bitemporal/version"
require "activerecord-bitemporal/visualizer"
Expand Down
8 changes: 6 additions & 2 deletions lib/activerecord-bitemporal/bitemporal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -501,13 +501,17 @@ def bitemporal_build_update_records(valid_datetime:, current_time: Time.current,

# 以前の履歴データは valid_to を詰めて保存
before_instance.valid_to = target_datetime
raise ActiveRecord::RecordInvalid.new(before_instance) if before_instance.valid_from_cannot_be_greater_equal_than_valid_to
if before_instance.valid_from_cannot_be_greater_equal_than_valid_to
raise ValidDatetimeRangeError.new("valid_from #{before_instance.valid_from} can't be greater equal than valid_to #{before_instance.valid_to}")
end
before_instance.transaction_from = current_time

# 以降の履歴データは valid_from と valid_to を調整して保存する
after_instance.valid_from = target_datetime
after_instance.valid_to = current_valid_record.valid_to
raise ActiveRecord::RecordInvalid.new(after_instance) if after_instance.valid_from_cannot_be_greater_equal_than_valid_to
if after_instance.valid_from_cannot_be_greater_equal_than_valid_to
raise ValidDatetimeRangeError.new("valid_from #{after_instance.valid_from} can't be greater equal than valid_to #{after_instance.valid_to}")
end
after_instance.transaction_from = current_time

# 有効なレコードがない場合
Expand Down
7 changes: 7 additions & 0 deletions lib/activerecord-bitemporal/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module ActiveRecord::Bitemporal
class BitemporalError < StandardError; end

class ValidDatetimeRangeError < BitemporalError; end
end
6 changes: 3 additions & 3 deletions spec/activerecord-bitemporal/bitemporal_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1204,9 +1204,9 @@ class Article < ActiveRecord::Base
end

describe "through" do
let(:blog) { Blog.create!(name: "tabelog").tap { |it| it.update(name: "sushilog") } }
let(:user) { User.create!(name: "Jane").tap { |it| it.update(name: "Tom") } }
let(:article) { user.articles.create!(title: "yakiniku", blog: blog).tap { |it| it.update(title: "sushi") } }
let(:blog) { Blog.create!(name: "tabelog") }
let(:user) { User.create!(name: "Jane") }
let(:article) { user.articles.create!(title: "yakiniku", blog: blog) }

context ".ignore_valid_datetime" do
let(:relation) { blog.users.ignore_valid_datetime }
Expand Down
8 changes: 3 additions & 5 deletions spec/activerecord-bitemporal/bitemporal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -858,15 +858,13 @@ def employee.wrapped_name
let(:valid_datetime) { company.valid_from }
subject { company.valid_at(valid_datetime) { |c| c.update(name: "Company") } }

it { expect(subject).to be_falsey }
it { expect { subject }.not_to change(&company_count) }
it { expect { subject }.not_to change(&company_transaction_to) }
it { expect { subject }.to raise_error(ActiveRecord::Bitemporal::ValidDatetimeRangeError) }

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

0 comments on commit e52a640

Please sign in to comment.