2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ApplicationRecord < ActiveRecord::Base
|
|
|
|
self.abstract_class = true
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
def self.id_in(ids)
|
|
|
|
where(id: ids)
|
|
|
|
end
|
|
|
|
|
2019-05-18 00:54:41 +05:30
|
|
|
def self.id_not_in(ids)
|
|
|
|
where.not(id: ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.pluck_primary_key
|
|
|
|
where(nil).pluck(self.primary_key)
|
|
|
|
end
|
|
|
|
|
2019-05-03 19:53:19 +05:30
|
|
|
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-03-02 22:35:43 +05:30
|
|
|
def self.safe_find_or_create_by!(*args)
|
|
|
|
safe_find_or_create_by(*args).tap do |record|
|
|
|
|
record.validate! unless record.persisted?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.safe_find_or_create_by(*args)
|
2019-05-03 19:53:19 +05:30
|
|
|
safe_ensure_unique(retries: 1) do
|
2019-03-02 22:35:43 +05:30
|
|
|
find_or_create_by(*args)
|
|
|
|
end
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|