Commit 6ae3098e authored by Jacopo's avatar Jacopo

Uses Uniquify to calculate Issue#suggested_branch_name

parent 4f2e4947
......@@ -3,11 +3,14 @@ class Uniquify
# 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)
def string(base, counter = nil)
@base = base
@counter = nil
@counter = counter
increment_counter! while yield(base_string)
base_string
......
......@@ -197,10 +197,10 @@ class Issue < ActiveRecord::Base
def suggested_branch_name
return to_branch_name unless project.repository.branch_exists?(to_branch_name)
index = 2
index += 1 while project.repository.branch_exists?("#{to_branch_name}-#{index}")
"#{to_branch_name}-#{index}"
start_counting_from = 2
Uniquify.new.string(-> (counter) { "#{to_branch_name}-#{counter}" }, start_counting_from) do |suggested_branch_name|
project.repository.branch_exists?(suggested_branch_name)
end
end
# Returns boolean if a related branch exists for the current issue
......
......@@ -22,6 +22,13 @@ describe Uniquify do
expect(result).to eq('test_string2')
end
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' }
expect(result).to eq('test_string2')
end
it 'allows passing in a base function that defines the location of the counter' do
result = uniquify.string(-> (counter) { "test_#{counter}_string" }) do |s|
s == 'test__string'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment