2017-01-31 17:13:45 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2017-05-02 15:21:13 +00:00
|
|
|
module CanUniqName
|
2017-01-31 17:13:45 +00:00
|
|
|
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)
|
2017-10-01 12:25:52 +00:00
|
|
|
(1..100).each do |counter|
|
2017-01-31 17:13:45 +00:00
|
|
|
name = "#{name}_#{counter}"
|
|
|
|
break if !exists?(name: name)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|