2018-08-03 17:22:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-12 15:51:38 +00:00
|
|
|
# Include atomic internal id generation scheme for a model
|
|
|
|
#
|
2018-03-16 12:34:08 +00:00
|
|
|
# This allows us to atomically generate internal ids that are
|
2018-03-12 15:51:38 +00:00
|
|
|
# unique within a given scope.
|
|
|
|
#
|
|
|
|
# For example, let's generate internal ids for Issue per Project:
|
|
|
|
# ```
|
2019-03-28 13:17:42 +00:00
|
|
|
# class Issue < ApplicationRecord
|
2018-03-12 15:51:38 +00:00
|
|
|
# has_internal_id :iid, scope: :project, init: ->(s) { s.project.issues.maximum(:iid) }
|
|
|
|
# end
|
|
|
|
# ```
|
|
|
|
#
|
|
|
|
# This generates unique internal ids per project for newly created issues.
|
|
|
|
# The generated internal id is saved in the `iid` attribute of `Issue`.
|
|
|
|
#
|
|
|
|
# This concern uses InternalId records to facilitate atomicity.
|
|
|
|
# In the absence of a record for the given scope, one will be created automatically.
|
|
|
|
# In this situation, the `init` block is called to calculate the initial value.
|
|
|
|
# In the example above, we calculate the maximum `iid` of all issues
|
|
|
|
# within the given project.
|
|
|
|
#
|
|
|
|
# Note that a model may have more than one internal id associated with possibly
|
|
|
|
# different scopes.
|
2018-03-06 19:09:01 +00:00
|
|
|
module AtomicInternalId
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2018-08-27 12:35:31 +00:00
|
|
|
class_methods do
|
2019-10-04 12:06:14 +00:00
|
|
|
def has_internal_id(column, scope:, init:, ensure_if: nil, presence: true) # rubocop:disable Naming/PredicateName
|
2018-07-27 12:39:48 +00:00
|
|
|
# We require init here to retain the ability to recalculate in the absence of a
|
|
|
|
# InternaLId record (we may delete records in `internal_ids` for example).
|
|
|
|
raise "has_internal_id requires a init block, none given." unless init
|
2019-10-04 12:06:14 +00:00
|
|
|
raise "has_internal_id needs to be defined on association." unless self.reflect_on_association(scope)
|
2018-07-27 12:39:48 +00:00
|
|
|
|
2019-10-04 12:06:14 +00:00
|
|
|
before_validation :"track_#{scope}_#{column}!", on: :create
|
|
|
|
before_validation :"ensure_#{scope}_#{column}!", on: :create, if: ensure_if
|
2018-05-11 06:34:36 +00:00
|
|
|
validates column, presence: presence
|
2018-05-03 08:48:23 +00:00
|
|
|
|
2018-05-10 07:22:43 +00:00
|
|
|
define_method("ensure_#{scope}_#{column}!") do
|
2019-10-04 12:06:14 +00:00
|
|
|
scope_value = internal_id_read_scope(scope)
|
2018-08-01 09:03:14 +00:00
|
|
|
value = read_attribute(column)
|
|
|
|
return value unless scope_value
|
2018-03-14 13:36:07 +00:00
|
|
|
|
2019-10-04 12:06:14 +00:00
|
|
|
if value.nil?
|
2019-09-30 18:06:34 +00:00
|
|
|
# We don't have a value yet and use a InternalId record to generate
|
|
|
|
# the next value.
|
2019-10-04 12:06:14 +00:00
|
|
|
value = InternalId.generate_next(
|
|
|
|
self,
|
|
|
|
internal_id_scope_attrs(scope),
|
|
|
|
internal_id_scope_usage,
|
|
|
|
init)
|
2018-08-01 09:03:14 +00:00
|
|
|
write_attribute(column, value)
|
2018-03-14 12:42:03 +00:00
|
|
|
end
|
2018-05-03 08:48:23 +00:00
|
|
|
|
2018-08-01 09:03:14 +00:00
|
|
|
value
|
2018-03-12 14:38:56 +00:00
|
|
|
end
|
2019-03-22 15:51:15 +00:00
|
|
|
|
2019-10-04 12:06:14 +00:00
|
|
|
define_method("track_#{scope}_#{column}!") do
|
2019-10-15 21:06:25 +00:00
|
|
|
return unless @internal_id_needs_tracking
|
2019-10-04 12:06:14 +00:00
|
|
|
|
|
|
|
scope_value = internal_id_read_scope(scope)
|
|
|
|
return unless scope_value
|
|
|
|
|
2019-10-11 18:06:15 +00:00
|
|
|
value = read_attribute(column)
|
|
|
|
|
2019-10-04 12:06:14 +00:00
|
|
|
if value.present?
|
|
|
|
# The value was set externally, e.g. by the user
|
|
|
|
# We update the InternalId record to keep track of the greatest value.
|
|
|
|
InternalId.track_greatest(
|
|
|
|
self,
|
|
|
|
internal_id_scope_attrs(scope),
|
|
|
|
internal_id_scope_usage,
|
|
|
|
value,
|
|
|
|
init)
|
2019-10-11 18:06:15 +00:00
|
|
|
|
|
|
|
@internal_id_needs_tracking = false
|
2019-10-04 12:06:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-30 18:06:34 +00:00
|
|
|
define_method("#{column}=") do |value|
|
|
|
|
super(value).tap do |v|
|
|
|
|
# Indicate the iid was set from externally
|
2019-10-04 12:06:14 +00:00
|
|
|
@internal_id_needs_tracking = true
|
2019-09-30 18:06:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-22 15:51:15 +00:00
|
|
|
define_method("reset_#{scope}_#{column}") do
|
|
|
|
if value = read_attribute(column)
|
2019-10-04 12:06:14 +00:00
|
|
|
did_reset = InternalId.reset(
|
|
|
|
self,
|
|
|
|
internal_id_scope_attrs(scope),
|
|
|
|
internal_id_scope_usage,
|
|
|
|
value)
|
2019-03-22 15:51:15 +00:00
|
|
|
|
2019-10-04 12:06:14 +00:00
|
|
|
if did_reset
|
2019-03-22 15:51:15 +00:00
|
|
|
write_attribute(column, nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
read_attribute(column)
|
|
|
|
end
|
2018-03-12 14:38:56 +00:00
|
|
|
end
|
2018-03-06 19:09:01 +00:00
|
|
|
end
|
2019-10-04 12:06:14 +00:00
|
|
|
|
|
|
|
def internal_id_scope_attrs(scope)
|
|
|
|
scope_value = internal_id_read_scope(scope)
|
|
|
|
|
|
|
|
{ scope_value.class.table_name.singularize.to_sym => scope_value } if scope_value
|
|
|
|
end
|
|
|
|
|
|
|
|
def internal_id_scope_usage
|
|
|
|
self.class.table_name.to_sym
|
|
|
|
end
|
|
|
|
|
|
|
|
def internal_id_read_scope(scope)
|
|
|
|
association(scope).reader
|
|
|
|
end
|
2018-03-06 19:09:01 +00:00
|
|
|
end
|