diff options
author | Jacopo <beschi.jacopo@gmail.com> | 2018-04-19 11:31:01 +0200 |
---|---|---|
committer | Jacopo <beschi.jacopo@gmail.com> | 2018-04-19 13:56:37 +0200 |
commit | 9000626a60c889c76ff1dfc8c6247f15953ca993 (patch) | |
tree | 0d1c6fd369301b146464634b4360dd79002dac30 | |
parent | 6ae3098eb8f01406190942e8952866dd9af81dde (diff) | |
download | gitlab-ce-9000626a60c889c76ff1dfc8c6247f15953ca993.tar.gz |
Moves Uniquify counter in the initializer
-rw-r--r-- | app/models/concerns/uniquify.rb | 27 | ||||
-rw-r--r-- | app/models/issue.rb | 2 | ||||
-rw-r--r-- | spec/models/concerns/uniquify_spec.rb | 4 |
3 files changed, 20 insertions, 13 deletions
diff --git a/app/models/concerns/uniquify.rb b/app/models/concerns/uniquify.rb index db51ed2dbeb..549a76da20e 100644 --- a/app/models/concerns/uniquify.rb +++ b/app/models/concerns/uniquify.rb @@ -1,16 +1,21 @@ +# Uniquify +# +# Return a version of the given 'base' string that is unique +# by appending a counter to it. Uniqueness is determined by +# repeated calls to the passed block. +# +# You can pass an initial value for the counter, if not given +# counting starts from 1. +# +# If `base` is a function/proc, we expect that calling it with a +# candidate counter returns a string to test/return. class Uniquify - # Return a version of the given 'base' string that is unique - # by appending a counter to it. Uniqueness is determined by - # repeated calls to the passed block. - # - # You can pass an initial value for the counter, if not given - # counting starts from 1. - # - # If `base` is a function/proc, we expect that calling it with a - # candidate counter returns a string to test/return. - def string(base, counter = nil) - @base = base + def initialize(counter = nil) @counter = counter + end + + def string(base) + @base = base increment_counter! while yield(base_string) base_string diff --git a/app/models/issue.rb b/app/models/issue.rb index 51028a404c2..0332bfa9371 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -198,7 +198,7 @@ class Issue < ActiveRecord::Base return to_branch_name unless project.repository.branch_exists?(to_branch_name) start_counting_from = 2 - Uniquify.new.string(-> (counter) { "#{to_branch_name}-#{counter}" }, start_counting_from) do |suggested_branch_name| + Uniquify.new(start_counting_from).string(-> (counter) { "#{to_branch_name}-#{counter}" }) do |suggested_branch_name| project.repository.branch_exists?(suggested_branch_name) end end diff --git a/spec/models/concerns/uniquify_spec.rb b/spec/models/concerns/uniquify_spec.rb index 00341213bbe..6cd2de6dcce 100644 --- a/spec/models/concerns/uniquify_spec.rb +++ b/spec/models/concerns/uniquify_spec.rb @@ -24,7 +24,9 @@ describe Uniquify do it 'allows to pass an initial value for the counter' do start_counting_from = 2 - result = uniquify.string('test_string', start_counting_from) { |s| s == 'test_string' } + uniquify = described_class.new(start_counting_from) + + result = uniquify.string('test_string') { |s| s == 'test_string' } expect(result).to eq('test_string2') end |