2018-11-11 08:43:43 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ApplicationRecord < ActiveRecord::Base
|
|
|
|
self.abstract_class = true
|
2018-11-22 09:35:28 +00:00
|
|
|
|
2019-04-08 13:33:36 +00:00
|
|
|
alias_method :reset, :reload
|
|
|
|
|
2020-05-28 15:08:02 +00:00
|
|
|
def self.without_order
|
|
|
|
reorder(nil)
|
|
|
|
end
|
|
|
|
|
2018-11-22 09:35:28 +00:00
|
|
|
def self.id_in(ids)
|
|
|
|
where(id: ids)
|
|
|
|
end
|
2019-02-04 13:39:54 +00:00
|
|
|
|
2019-03-27 16:18:28 +00:00
|
|
|
def self.id_not_in(ids)
|
|
|
|
where.not(id: ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.pluck_primary_key
|
|
|
|
where(nil).pluck(self.primary_key)
|
|
|
|
end
|
|
|
|
|
2019-04-12 04:19:45 +00:00
|
|
|
def self.safe_ensure_unique(retries: 0)
|
|
|
|
transaction(requires_new: true) do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
if retries > 0
|
|
|
|
retries -= 1
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2019-02-05 17:22:25 +00:00
|
|
|
def self.safe_find_or_create_by!(*args)
|
|
|
|
safe_find_or_create_by(*args).tap do |record|
|
|
|
|
record.validate! unless record.persisted?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-04 13:39:54 +00:00
|
|
|
def self.safe_find_or_create_by(*args)
|
2019-04-12 04:19:45 +00:00
|
|
|
safe_ensure_unique(retries: 1) do
|
2019-02-04 13:39:54 +00:00
|
|
|
find_or_create_by(*args)
|
|
|
|
end
|
|
|
|
end
|
2019-05-11 12:06:44 +00:00
|
|
|
|
|
|
|
def self.underscore
|
|
|
|
Gitlab::SafeRequestStore.fetch("model:#{self}:underscore") { self.to_s.underscore }
|
|
|
|
end
|
2018-11-11 08:43:43 +00:00
|
|
|
end
|