summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2015-04-17 12:34:51 +0200
committerRobert Speicher <rspeicher@gmail.com>2015-04-20 13:01:47 -0400
commitb5802d144b4141b2259247f83903f3431ed6199c (patch)
tree5ebe0c9c82529c2dda1efcdbaca0354b1f6b5096
parentb595249462346d48817330baf8c253b6f08690a1 (diff)
downloadgitlab-ce-b5802d144b4141b2259247f83903f3431ed6199c.tar.gz
project_from_ref returns nil when reference doesn't exist.
-rw-r--r--lib/gitlab/markdown/cross_project_reference.rb15
-rw-r--r--spec/lib/gitlab/markdown/cross_project_reference_spec.rb10
2 files changed, 14 insertions, 11 deletions
diff --git a/lib/gitlab/markdown/cross_project_reference.rb b/lib/gitlab/markdown/cross_project_reference.rb
index de05102751f..887c205cdc9 100644
--- a/lib/gitlab/markdown/cross_project_reference.rb
+++ b/lib/gitlab/markdown/cross_project_reference.rb
@@ -16,15 +16,12 @@ module Gitlab
#
# Returns a Project, or nil if the reference can't be accessed
def project_from_ref(ref)
- if ref && other = Project.find_with_namespace(ref)
- if user_can_reference_project?(other)
- other
- else
- nil
- end
- else
- context[:project]
- end
+ return context[:project] unless ref
+
+ other = Project.find_with_namespace(ref)
+ return nil unless other && user_can_reference_project?(other)
+
+ other
end
def user_can_reference_project?(project, user = context[:current_user])
diff --git a/spec/lib/gitlab/markdown/cross_project_reference_spec.rb b/spec/lib/gitlab/markdown/cross_project_reference_spec.rb
index d79b2c1af92..4698d6138c2 100644
--- a/spec/lib/gitlab/markdown/cross_project_reference_spec.rb
+++ b/spec/lib/gitlab/markdown/cross_project_reference_spec.rb
@@ -13,9 +13,15 @@ module Gitlab::Markdown
include described_class
describe '#project_from_ref' do
- context 'when referenced project does not exist' do
+ context 'when no project was referenced' do
it 'returns the project from context' do
- expect(project_from_ref('invalid/reference')).to eq context[:project]
+ expect(project_from_ref(nil)).to eq context[:project]
+ end
+ end
+
+ context 'when referenced project does not exist' do
+ it 'returns nil' do
+ expect(project_from_ref('invalid/reference')).to be_nil
end
end