diff --git a/lib/glueby/contract/active_record/timestamp.rb b/lib/glueby/contract/active_record/timestamp.rb index d255dd1..9074bac 100644 --- a/lib/glueby/contract/active_record/timestamp.rb +++ b/lib/glueby/contract/active_record/timestamp.rb @@ -9,7 +9,12 @@ class Timestamp < ::ActiveRecord::Base attr_reader :tx - belongs_to :prev, class_name: 'Glueby::Contract::AR::Timestamp', optional: true + belongs_to :prev, class_name: 'Glueby::Contract::AR::Timestamp', inverse_of: :next, optional: true + has_one :next, class_name: 'Glueby::Contract::AR::Timestamp', foreign_key: 'prev_id' + + def next_id + self.next&.id + end validate :validate_prev @@ -114,6 +119,7 @@ def save_with_broadcast!(fee_estimator: Glueby::Contract::FeeEstimator::Fixed.ne if update_trackable? prev.latest = false + prev.next = self prev.save! end end diff --git a/spec/glueby/contract/timestamp/active_record/timestamp_spec.rb b/spec/glueby/contract/timestamp/active_record/timestamp_spec.rb index 50628ca..8d6dc91 100644 --- a/spec/glueby/contract/timestamp/active_record/timestamp_spec.rb +++ b/spec/glueby/contract/timestamp/active_record/timestamp_spec.rb @@ -519,11 +519,14 @@ expect(timestamp.p2c_address).not_to be_nil expect(timestamp.payment_base).not_to be_nil expect(timestamp.latest).to be_truthy + expect(timestamp.next_id).to be_nil end - it 'update previous timestamp\'s latest falg to false' do + it 'update previous timestamp\'s latest flag and next_id' do subject - expect(prev.reload.latest).to be_falsey + prev.reload + expect(prev.latest).to be_falsey + expect(prev.next_id).to eq timestamp.id end context 'previous timestamp type is not trackable' do