summaryrefslogtreecommitdiff
path: root/app/models/concerns/internal_id.rb
blob: 01079fb8bd6aceb6db26b83c557207639d2f58e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module InternalId
  extend ActiveSupport::Concern

  included do
    validate :set_iid, on: :create
    validates :iid, presence: true, numericality: true
  end

  def set_iid
    if iid.blank?
      parent = project || group
      records = parent.public_send(self.class.name.tableize) # rubocop:disable GitlabSecurity/PublicSend
      max_iid = records.maximum(:iid)

      self.iid = max_iid.to_i + 1
    end
  end

  def to_param
    iid.to_s
  end
end