# Generate next internal id for a given project and usage.
#
...
...
@@ -45,12 +34,21 @@ class InternalId < ActiveRecord::Base
# 3) The generated sequence is gapless.
# 4) In the absence of a record in the internal_ids table, one will be created
# and last_value will be calculated on the fly.
defgenerate_next(project,usage)
raise'project not set - this is required'unlessproject
defgenerate_next(subject,scope,usage,init)
scope=[scope].flatten.compact
raise'scope is not well-defined, need at least one column for scope (given: 0)'ifscope.empty?
raise"usage #{usage} is unknown. Supported values are InternalId.usages = #{InternalId.usages.keys.to_s}"unlessInternalId.usages.include?(usage.to_sym)
init||=->(s){0}
scope_attrs=scope.inject({})do|h,e|
h[e]=subject.public_send(e)
h
end
project.transactiondo
transactiondo
# Create a record in internal_ids if one does not yet exist