Commit acd877c7 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add cross project references support for label model

parent 65ba4da9
......@@ -70,16 +70,20 @@ class Label < ActiveRecord::Base
#
# Label.first.to_reference # => "~1"
# Label.first.to_reference(format: :name) # => "~\"bug\""
# Label.first.to_reference(project) # => "gitlab-org/gitlab-ce~1"
#
# Returns a String
def to_reference(_from_project = nil, format: :id)
if format == :name && !name.include?('"')
%(#{self.class.reference_prefix}"#{name}")
def to_reference(from_project = nil, format: :id)
reference = label_format_reference(format)
if cross_project_reference?(from_project)
project.to_reference + reference
else
"#{self.class.reference_prefix}#{id}"
reference
end
end
def open_issues_count
issues.opened.count
end
......@@ -95,4 +99,16 @@ class Label < ActiveRecord::Base
def template?
template
end
private
def label_format_reference(format = :id)
raise StandardError, 'Unknown format' unless [:id, :name].include?(format)
if format == :name && !name.include?('"')
%(#{self.class.reference_prefix}"#{name}")
else
"#{self.class.reference_prefix}#{id}"
end
end
end
......@@ -59,7 +59,6 @@ describe Label, models: true do
context 'using id' do
it 'returns a String reference to the object' do
expect(label.to_reference).to eq "~#{label.id}"
expect(label.to_reference(double('project'))).to eq "~#{label.id}"
end
end
......@@ -73,5 +72,30 @@ describe Label, models: true do
expect(label.to_reference(format: :name)).to eq "~#{label.id}"
end
end
context 'using invalid format' do
it 'raises error' do
expect { label.to_reference(format: :invalid) }
.to raise_error StandardError, /Unknown format/
end
end
context 'cross project reference' do
let(:project) { create(:project) }
context 'using name' do
it 'returns cross reference with label name' do
expect(label.to_reference(project, format: :name))
.to eq %Q(#{label.project.to_reference}~"#{label.name}")
end
end
context 'using id' do
it 'returns cross reference with label id' do
expect(label.to_reference(project, format: :id))
.to eq %Q(#{label.project.to_reference}~#{label.id})
end
end
end
end
end
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