Skip to content

Commit

Permalink
Merge pull request #195 from chaintope/feature/add_next_id_to_timestamp
Browse files Browse the repository at this point in the history
Add next_id field to timestamp record
  • Loading branch information
rantan authored Nov 21, 2023
2 parents 53d0b7c + c08f453 commit 1155955
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 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
Expand Down

0 comments on commit 1155955

Please sign in to comment.