Skip to content

Commit

Permalink
Add next_id field to timestamp record
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamaguchi committed Oct 31, 2023
1 parent 53d0b7c commit 0f1822d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/glueby/contract/active_record/timestamp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 falg 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
Expand Down

0 comments on commit 0f1822d

Please sign in to comment.