diff options
author | Andreas Brandl <abrandl@gitlab.com> | 2018-03-12 17:38:56 +0300 |
---|---|---|
committer | Andreas Brandl <abrandl@gitlab.com> | 2018-03-16 15:35:25 +0300 |
commit | 0360b0928aada1db7635e6c6bc40f0f571b72c30 (patch) | |
tree | 8753b119a642505f450304684d36c33a8d8ddc79 /app/models/concerns/atomic_internal_id.rb | |
parent | 754272e392c0da088200a1b56156600973f63267 (diff) |
More flexible way of internal id generation.
Diffstat (limited to 'app/models/concerns/atomic_internal_id.rb')
-rw-r--r-- | app/models/concerns/atomic_internal_id.rb | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/app/models/concerns/atomic_internal_id.rb b/app/models/concerns/atomic_internal_id.rb index 3cc9ce7f03f..eef5c0bfcd1 100644 --- a/app/models/concerns/atomic_internal_id.rb +++ b/app/models/concerns/atomic_internal_id.rb @@ -2,15 +2,19 @@ module AtomicInternalId extend ActiveSupport::Concern included do - before_validation(on: :create) do - set_iid - end - - validates :iid, presence: true, numericality: true - end + class << self + def has_internal_id(on, scope:, usage: nil, init: nil) + before_validation(on: :create) do + if self.public_send(on).blank? # rubocop:disable GitlabSecurity/PublicSend + usage = (usage || self.class.name.tableize).to_sym + new_iid = InternalId.generate_next(self, scope, usage, init) + self.public_send("#{on}=", new_iid) # rubocop:disable GitlabSecurity/PublicSend + end + end - def set_iid - self.iid = InternalId.generate_next(self.project, :issues) if iid.blank? + validates on, presence: true, numericality: true + end + end end def to_param |