2018-11-11 03:43:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ApplicationRecord < ActiveRecord::Base
|
|
|
|
self.abstract_class = true
|
2018-11-22 04:35:28 -05:00
|
|
|
|
2019-04-08 09:33:36 -04:00
|
|
|
alias_method :reset, :reload
|
|
|
|
|
2020-05-28 11:08:02 -04:00
|
|
|
def self.without_order
|
|
|
|
reorder(nil)
|
|
|
|
end
|
|
|
|
|
2018-11-22 04:35:28 -05:00
|
|
|
def self.id_in(ids)
|
|
|
|
where(id: ids)
|
|
|
|
end
|
2019-02-04 08:39:54 -05:00
|
|
|
|
2020-07-10 02:09:23 -04:00
|
|
|
def self.iid_in(iids)
|
|
|
|
where(iid: iids)
|
|
|
|
end
|
|
|
|
|
2019-03-27 12:18:28 -04: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 00:19:45 -04: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
|
|
|
|
|
2020-07-16 20:09:37 -04:00
|
|
|
def self.at_most(count)
|
|
|
|
limit(count)
|
|
|
|
end
|
|
|
|
|
2020-08-05 11:09:59 -04:00
|
|
|
def self.safe_find_or_create_by!(*args, &block)
|
|
|
|
safe_find_or_create_by(*args, &block).tap do |record|
|
2019-02-05 12:22:25 -05:00
|
|
|
record.validate! unless record.persisted?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-05 11:09:59 -04:00
|
|
|
def self.safe_find_or_create_by(*args, &block)
|
2019-04-12 00:19:45 -04:00
|
|
|
safe_ensure_unique(retries: 1) do
|
2020-08-05 11:09:59 -04:00
|
|
|
find_or_create_by(*args, &block)
|
2019-02-04 08:39:54 -05:00
|
|
|
end
|
|
|
|
end
|
2019-05-11 08:06:44 -04:00
|
|
|
|
|
|
|
def self.underscore
|
|
|
|
Gitlab::SafeRequestStore.fetch("model:#{self}:underscore") { self.to_s.underscore }
|
|
|
|
end
|
2018-11-11 03:43:43 -05:00
|
|
|
end
|