2015-04-17 20:05:46 +02:00
|
|
|
module MarkAsDeletedWithName
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
def mark_as_deleted
|
|
|
|
# get maximum length of name
|
2023-05-12 13:01:12 +02:00
|
|
|
max_length = 100_000
|
2015-04-17 20:05:46 +02:00
|
|
|
if lenval = self.class.validators_on(:name).detect { |v| v.is_a?(ActiveModel::Validations::LengthValidator) }
|
|
|
|
max_length = lenval.options[:maximum]
|
|
|
|
end
|
|
|
|
# find unique deleted name
|
|
|
|
# (would have been nice to use retry, but there is no general duplicate-entry exception)
|
|
|
|
n = ''
|
|
|
|
begin
|
2020-07-30 22:05:19 +02:00
|
|
|
append = " \u2020" + n
|
2021-03-01 15:27:26 +01:00
|
|
|
deleted_name = name.truncate(max_length - append.length, omission: '') + append
|
2015-04-17 20:05:46 +02:00
|
|
|
if n.blank?
|
|
|
|
n = 'A'
|
|
|
|
else
|
|
|
|
n.succ!
|
|
|
|
end
|
|
|
|
end while self.class.where(name: deleted_name).exists?
|
|
|
|
# mark as deleted
|
|
|
|
update_columns deleted_at: Time.now, name: deleted_name
|
|
|
|
end
|
|
|
|
end
|