diff options
Diffstat (limited to 'app/models/concerns/uniquify.rb')
-rw-r--r-- | app/models/concerns/uniquify.rb | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/app/models/concerns/uniquify.rb b/app/models/concerns/uniquify.rb index 1485ab6ae99..6734472bc6d 100644 --- a/app/models/concerns/uniquify.rb +++ b/app/models/concerns/uniquify.rb @@ -6,19 +6,23 @@ class Uniquify # If `base` is a function/proc, we expect that calling it with a # candidate counter returns a string to test/return. def string(base, exists_fn) + @base = base @counter = nil - if base.respond_to?(:call) - increment_counter! while exists_fn[base.call(@counter)] - base.call(@counter) - else - increment_counter! while exists_fn["#{base}#{@counter}"] - "#{base}#{@counter}" - end + increment_counter! while exists_fn[base_string] + base_string end private + def base_string + if @base.respond_to?(:call) + @base.call(@counter) + else + "#{@base}#{@counter}" + end + end + def increment_counter! @counter = @counter ? @counter.next : 1 end |