From acd877c73e302438acabeaccfcd0fc5dfcd7a894 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Thu, 25 Feb 2016 10:14:12 +0100 Subject: [PATCH] Add cross project references support for label model --- app/models/label.rb | 24 ++++++++++++++++++++---- spec/models/label_spec.rb | 26 +++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/app/models/label.rb b/app/models/label.rb index 02683a08dd6..445f22ee1e0 100644 --- a/app/models/label.rb +++ b/app/models/label.rb @@ -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 diff --git a/spec/models/label_spec.rb b/spec/models/label_spec.rb index fd133e1ca1b..0614ca1e7c9 100644 --- a/spec/models/label_spec.rb +++ b/spec/models/label_spec.rb @@ -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