Commit 236aa0fd authored by David Kim's avatar David Kim

Add alternate_viewer for renamed viewer

We'd like to be able to change the underlying viewer when the current
viewer is renamed so that we can display the full content of the file in
line.
parent 0f58b134
...@@ -88,6 +88,7 @@ class DiffFileBaseEntity < Grape::Entity ...@@ -88,6 +88,7 @@ class DiffFileBaseEntity < Grape::Entity
expose :b_mode expose :b_mode
expose :viewer, using: DiffViewerEntity expose :viewer, using: DiffViewerEntity
expose :alternate_viewer, using: DiffViewerEntity
expose :old_size do |diff_file| expose :old_size do |diff_file|
diff_file.old_blob&.raw_size diff_file.old_blob&.raw_size
......
...@@ -314,6 +314,10 @@ module Gitlab ...@@ -314,6 +314,10 @@ module Gitlab
@rich_viewer = rich_viewer_class&.new(self) @rich_viewer = rich_viewer_class&.new(self)
end end
def alternate_viewer
alternate_viewer_class&.new(self)
end
def rendered_as_text?(ignore_errors: true) def rendered_as_text?(ignore_errors: true)
simple_viewer.is_a?(DiffViewer::Text) && (ignore_errors || simple_viewer.render_error.nil?) simple_viewer.is_a?(DiffViewer::Text) && (ignore_errors || simple_viewer.render_error.nil?)
end end
...@@ -419,6 +423,17 @@ module Gitlab ...@@ -419,6 +423,17 @@ module Gitlab
return if collapsed? return if collapsed?
return unless diffable? return unless diffable?
return unless modified_file? return unless modified_file?
find_renderable_viewer_class(classes)
end
def alternate_viewer_class
return unless viewer.class == DiffViewer::Renamed
find_renderable_viewer_class(RICH_VIEWERS) || (DiffViewer::Text if text?)
end
def find_renderable_viewer_class(classes)
return if different_type? || external_storage_error? return if different_type? || external_storage_error?
verify_binary = !stored_externally? verify_binary = !stored_externally?
......
...@@ -567,6 +567,61 @@ describe Gitlab::Diff::File do ...@@ -567,6 +567,61 @@ describe Gitlab::Diff::File do
end end
end end
describe '#alternate_viewer' do
subject { diff_file.alternate_viewer }
where(:viewer_class) do
[
DiffViewer::Image,
DiffViewer::Collapsed,
DiffViewer::NotDiffable,
DiffViewer::Text,
DiffViewer::NoPreview,
DiffViewer::Added,
DiffViewer::Deleted,
DiffViewer::ModeChanged,
DiffViewer::ModeChanged,
DiffViewer::NoPreview
]
end
with_them do
let(:viewer) { viewer_class.new(diff_file) }
before do
allow(diff_file).to receive(:viewer).and_return(viewer)
end
it { is_expected.to be_nil }
end
context 'when viewer is DiffViewer::Renamed' do
let(:viewer) { DiffViewer::Renamed.new(diff_file) }
before do
allow(diff_file).to receive(:viewer).and_return(viewer)
end
context 'when it can be rendered as text' do
it { is_expected.to be_a(DiffViewer::Text) }
end
context 'when it can be rendered as image' do
let(:commit) { project.commit('2f63565e7aac07bcdadb654e253078b727143ec4') }
let(:diff_file) { commit.diffs.diff_file_with_new_path('files/images/6049019_460s.jpg') }
it { is_expected.to be_a(DiffViewer::Image) }
end
context 'when it is something else' do
let(:commit) { project.commit('ae73cb07c9eeaf35924a10f713b364d32b2dd34f') }
let(:diff_file) { commit.diffs.diff_file_with_new_path('Gemfile.zip') }
it { is_expected.to be_nil }
end
end
end
describe '#rendered_as_text?' do describe '#rendered_as_text?' do
context 'when the simple viewer is text-based' do context 'when the simple viewer is text-based' do
let(:commit) { project.commit('570e7b2abdd848b95f2f578043fc23bd6f6fd24d') } let(:commit) { project.commit('570e7b2abdd848b95f2f578043fc23bd6f6fd24d') }
......
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