434a9671cc
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.
27 lines
580 B
Ruby
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
|