Files
fizzy/app/models/account/external_id_sequence.rb
T
Mike Dalessio 434a9671cc Ensure Account::ExternalIdSequenceTest.value is never nil
If no record exists, create it. This fixes a test in fizzy-saas in
test/models/signup_test.rb that was not actually testing with a valid
external account id.
2025-11-28 14:27:14 -05:00

27 lines
580 B
Ruby

# Provides sequential IDs for +external_account_id+ when creating accounts without one.
class Account::ExternalIdSequence < ApplicationRecord
class << self
def next
with_lock do |sequence|
sequence.increment!(:value).value
end
end
def value
first&.value || self.next
end
private
def with_lock
transaction do
sequence = lock.first_or_create!(value: initial_value)
yield sequence
end
end
def initial_value
Account.maximum(:external_account_id) || 0
end
end
end