Refactor snippet routes

parent 8214da07
......@@ -271,7 +271,7 @@ module GitlabRoutingHelper
end
end
def gitlab_raw_snippet_blob_url(snippet, path, ref = nil)
def gitlab_raw_snippet_blob_url(snippet, path, ref = nil, **options)
params = {
snippet_id: snippet,
ref: ref || snippet.repository.root_ref,
......@@ -279,26 +279,14 @@ module GitlabRoutingHelper
}
if snippet.is_a?(ProjectSnippet)
project_snippet_blob_raw_url(snippet.project, params)
project_snippet_blob_raw_url(snippet.project, **params, **options)
else
snippet_blob_raw_url(params)
snippet_blob_raw_url(**params, **options)
end
end
def gitlab_raw_snippet_blob_path(blob, ref = nil)
snippet = blob.container
params = {
snippet_id: snippet,
ref: ref || blob.repository.root_ref,
path: blob.path
}
if snippet.is_a?(ProjectSnippet)
project_snippet_blob_raw_path(snippet.project, params)
else
snippet_blob_raw_path(params)
end
def gitlab_raw_snippet_blob_path(snippet, path, ref = nil, **options)
gitlab_raw_snippet_blob_url(snippet, path, ref, only_path: true, **options)
end
def gitlab_snippet_notes_path(snippet, *args)
......
......@@ -17,7 +17,7 @@ class SnippetBlobPresenter < BlobPresenter
end
def raw_path
return gitlab_raw_snippet_blob_path(blob) if snippet_multiple_files?
return gitlab_raw_snippet_blob_path(snippet, blob.path) if snippet_multiple_files?
gitlab_raw_snippet_path(snippet)
end
......
......@@ -182,58 +182,79 @@ RSpec.describe GitlabRoutingHelper do
end
describe '#gitlab_raw_snippet_blob_path' do
let(:snippet) { personal_snippet }
let(:blob) { snippet.blobs.first }
let(:ref) { 'test-ref' }
let(:args) { {} }
subject { gitlab_raw_snippet_blob_path(snippet, blob.path, ref, args) }
it_behaves_like 'snippet blob raw path'
context 'when an argument is set' do
let(:args) { { inline: true } }
it_behaves_like 'snippet blob raw path' do
subject { gitlab_raw_snippet_blob_path(blob, ref) }
it { expect(subject).to eq("/-/snippets/#{personal_snippet.id}/raw/#{ref}/#{blob.path}?inline=true") }
end
context 'without a ref' do
let(:blob) { personal_snippet.blobs.first }
let(:ref) { blob.repository.root_ref }
let(:ref) { nil }
let(:expected_ref) { blob.repository.root_ref }
it 'uses the root ref' do
expect(gitlab_raw_snippet_blob_path(blob)).to eq("/-/snippets/#{personal_snippet.id}/raw/#{ref}/#{blob.path}")
expect(subject).to eq("/-/snippets/#{personal_snippet.id}/raw/#{expected_ref}/#{blob.path}")
end
end
end
describe '#gitlab_raw_snippet_url' do
it 'returns the raw personal snippet url' do
expect(gitlab_raw_snippet_url(personal_snippet)).to eq("http://test.host/snippets/#{personal_snippet.id}/raw")
end
it 'returns the raw project snippet url' do
expect(gitlab_raw_snippet_url(project_snippet)).to eq("http://test.host/#{project_snippet.project.full_path}/snippets/#{project_snippet.id}/raw")
end
end
describe '#gitlab_raw_snippet_blob_url' do
let(:blob) { snippet.blobs.first }
let(:ref) { 'snippet-test-ref' }
let(:args) { {} }
subject { gitlab_raw_snippet_blob_url(snippet, blob.path, ref, args) }
context 'for a PersonalSnippet' do
let(:snippet) { personal_snippet }
it { expect(gitlab_raw_snippet_blob_url(snippet, blob.path, ref)).to eq("http://test.host/-/snippets/#{snippet.id}/raw/#{ref}/#{blob.path}") }
it { expect(subject).to eq("http://test.host/-/snippets/#{snippet.id}/raw/#{ref}/#{blob.path}") }
end
context 'for a ProjectSnippet' do
let(:snippet) { project_snippet }
it { expect(gitlab_raw_snippet_blob_url(snippet, blob.path, ref)).to eq("http://test.host/#{snippet.project.full_path}/-/snippets/#{snippet.id}/raw/#{ref}/#{blob.path}") }
it { expect(subject).to eq("http://test.host/#{snippet.project.full_path}/-/snippets/#{snippet.id}/raw/#{ref}/#{blob.path}") }
end
context 'when an argument is set' do
let(:args) { { inline: true } }
let(:snippet) { personal_snippet }
it { expect(subject).to eq("http://test.host/-/snippets/#{snippet.id}/raw/#{ref}/#{blob.path}?inline=true") }
end
context 'without a ref' do
let(:snippet) { personal_snippet }
let(:ref) { snippet.repository.root_ref }
let(:ref) { nil }
let(:expected_ref) { snippet.repository.root_ref }
it 'uses the root ref' do
expect(gitlab_raw_snippet_blob_url(snippet, blob.path)).to eq("http://test.host/-/snippets/#{snippet.id}/raw/#{ref}/#{blob.path}")
expect(subject).to eq("http://test.host/-/snippets/#{snippet.id}/raw/#{expected_ref}/#{blob.path}")
end
end
end
describe '#gitlab_raw_snippet_url' do
it 'returns the raw personal snippet url' do
expect(gitlab_raw_snippet_url(personal_snippet)).to eq("http://test.host/snippets/#{personal_snippet.id}/raw")
end
it 'returns the raw project snippet url' do
expect(gitlab_raw_snippet_url(project_snippet)).to eq("http://test.host/#{project_snippet.project.full_path}/snippets/#{project_snippet.id}/raw")
end
end
describe '#gitlab_snippet_notes_path' do
it 'returns the notes path for the personal snippet' do
expect(gitlab_snippet_notes_path(personal_snippet)).to eq("/snippets/#{personal_snippet.id}/notes")
......
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