Commit c9029b83 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'vij-snippet-blob-raw-presenter' into 'master'

Change `raw_path` in SnippetBlobPresenter

See merge request gitlab-org/gitlab!35843
parents a4f96953 4f6651ec
......@@ -285,6 +285,22 @@ module GitlabRoutingHelper
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
end
def gitlab_snippet_notes_path(snippet, *args)
new_args = snippet_query_params(snippet, *args)
snippet_notes_path(snippet, *new_args)
......
# frozen_string_literal: true
class SnippetBlobPresenter < BlobPresenter
include GitlabRoutingHelper
def rich_data
return if blob.binary?
return unless blob.rich_viewer
......@@ -15,15 +17,17 @@ class SnippetBlobPresenter < BlobPresenter
end
def raw_path
if snippet.is_a?(ProjectSnippet)
raw_project_snippet_path(snippet.project, snippet)
else
raw_snippet_path(snippet)
end
return gitlab_raw_snippet_blob_path(blob) if snippet_multiple_files?
gitlab_raw_snippet_path(snippet)
end
private
def snippet_multiple_files?
blob.container.repository_exists? && Feature.enabled?(:snippet_multiple_files, current_user)
end
def snippet
blob.container
end
......
......@@ -181,6 +181,23 @@ RSpec.describe GitlabRoutingHelper do
end
end
describe '#gitlab_raw_snippet_blob_path' do
let(:ref) { 'test-ref' }
it_behaves_like 'snippet blob raw path' do
subject { gitlab_raw_snippet_blob_path(blob, ref) }
end
context 'without a ref' do
let(:blob) { personal_snippet.blobs.first }
let(: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}")
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")
......
......@@ -109,22 +109,38 @@ RSpec.describe SnippetBlobPresenter do
end
describe '#raw_path' do
subject { described_class.new(snippet.blob).raw_path }
let_it_be(:project) { create(:project) }
let_it_be(:user) { create(:user) }
let_it_be(:personal_snippet) { create(:personal_snippet, :repository, author: user) }
let_it_be(:project_snippet) { create(:project_snippet, :repository, project: project, author: user) }
context 'with ProjectSnippet' do
let!(:project) { create(:project) }
let(:snippet) { create(:project_snippet, project: project) }
before do
project.add_developer(user)
end
subject { described_class.new(snippet.blobs.first, current_user: user).raw_path }
it 'returns the raw path' do
expect(subject).to eq "/#{snippet.project.full_path}/snippets/#{snippet.id}/raw"
it_behaves_like 'snippet blob raw path'
context 'with snippet_multiple_files feature disabled' do
before do
stub_feature_flags(snippet_multiple_files: false)
end
end
context 'with PersonalSnippet' do
let(:snippet) { create(:personal_snippet) }
context 'with ProjectSnippet' do
let(:snippet) { project_snippet }
it 'returns the raw path' do
expect(subject).to eq "/snippets/#{snippet.id}/raw"
it 'returns the raw path' do
expect(subject).to eq "/#{snippet.project.full_path}/snippets/#{snippet.id}/raw"
end
end
context 'with PersonalSnippet' do
let(:snippet) { personal_snippet }
it 'returns the raw path' do
expect(subject).to eq "/snippets/#{snippet.id}/raw"
end
end
end
end
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.shared_examples 'snippet blob raw path' do
let(:blob) { snippet.blobs.first }
let(:ref) { blob.repository.root_ref }
context 'for PersonalSnippets' do
let(:snippet) { personal_snippet }
it 'returns the raw personal snippet blob path' do
expect(subject).to eq("/-/snippets/#{snippet.id}/raw/#{ref}/#{blob.path}")
end
end
context 'for ProjectSnippets' do
let(:snippet) { project_snippet }
it 'returns the raw project snippet blob path' do
expect(subject).to eq("/#{snippet.project.full_path}/-/snippets/#{snippet.id}/raw/#{ref}/#{blob.path}")
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