Refactor snippet routes

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