trabajo-afectivo/app/models/concerns/can_uniq_name.rb

34 lines
671 B
Ruby
Raw Permalink Normal View History

2022-01-01 13:38:12 +00:00
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
module CanUniqName
extend ActiveSupport::Concern
# methods defined here are going to extend the class, not the instance of it
class_methods do
=begin
generate uniq name (will check name of model and generates _1 sequenze)
Used as before_update callback, no own use needed
name = Model.generate_uniq_name('some name')
returns
result = 'some name_X'
=end
def generate_uniq_name(name)
return name if !exists?(name: name)
(1..100).each do |counter|
name = "#{name}_#{counter}"
break if !exists?(name: name)
end
name
end
end
end