Commit c6c9963e authored by Igor Drozdov's avatar Igor Drozdov

Move collapsed partial into a diff viewer

We have a concept of diff viewers, which renders diff
file differently depending on the type of this diff file

Following this concept, we can move rendering of a
collapsed diff file into a diff viewer and avoid
unnecessary calculation of a diff file type
parent 6b3a9dcb
# frozen_string_literal: true
module DiffViewer
class Collapsed < Base
include Simple
include Static
self.partial_name = 'collapsed'
end
end
...@@ -3,8 +3,6 @@ ...@@ -3,8 +3,6 @@
.diff-viewer{ data: { type: viewer.type }, class: ('hidden' if hidden) } .diff-viewer{ data: { type: viewer.type }, class: ('hidden' if hidden) }
- if viewer.render_error - if viewer.render_error
= render 'projects/diffs/render_error', viewer: viewer = render 'projects/diffs/render_error', viewer: viewer
- elsif viewer.collapsed?
= render 'projects/diffs/collapsed', viewer: viewer
- else - else
- viewer.prepare! - viewer.prepare!
......
...@@ -358,6 +358,10 @@ module Gitlab ...@@ -358,6 +358,10 @@ module Gitlab
end end
end end
def modified_file?
new_file? || deleted_file? || content_changed?
end
# We can't use Object#try because Blob doesn't inherit from Object, but # We can't use Object#try because Blob doesn't inherit from Object, but
# from BasicObject (via SimpleDelegator). # from BasicObject (via SimpleDelegator).
def try_blobs(meth) def try_blobs(meth)
...@@ -393,42 +397,26 @@ module Gitlab ...@@ -393,42 +397,26 @@ module Gitlab
end end
def simple_viewer_class def simple_viewer_class
return DiffViewer::Collapsed if collapsed?
return DiffViewer::NotDiffable unless diffable? return DiffViewer::NotDiffable unless diffable?
return DiffViewer::Text if modified_file? && text?
return DiffViewer::NoPreview if content_changed?
return DiffViewer::Added if new_file?
return DiffViewer::Deleted if deleted_file?
return DiffViewer::Renamed if renamed_file?
return DiffViewer::ModeChanged if mode_changed?
if content_changed?
if text?
DiffViewer::Text
else
DiffViewer::NoPreview DiffViewer::NoPreview
end end
elsif new_file?
if text?
DiffViewer::Text
else
DiffViewer::Added
end
elsif deleted_file?
if text?
DiffViewer::Text
else
DiffViewer::Deleted
end
elsif renamed_file?
DiffViewer::Renamed
elsif mode_changed?
DiffViewer::ModeChanged
else
DiffViewer::NoPreview
end
end
def rich_viewer_class def rich_viewer_class
viewer_class_from(RICH_VIEWERS) viewer_class_from(RICH_VIEWERS)
end end
def viewer_class_from(classes) def viewer_class_from(classes)
return if collapsed?
return unless diffable? return unless diffable?
return unless new_file? || deleted_file? || content_changed? return unless modified_file?
return if different_type? || external_storage_error? return if different_type? || external_storage_error?
verify_binary = !stored_externally? verify_binary = !stored_externally?
......
...@@ -347,6 +347,16 @@ describe Gitlab::Diff::File do ...@@ -347,6 +347,16 @@ describe Gitlab::Diff::File do
end end
describe '#simple_viewer' do describe '#simple_viewer' do
context 'when the file is collapsed' do
before do
allow(diff_file).to receive(:collapsed?).and_return(true)
end
it 'returns a Collapsed viewer' do
expect(diff_file.simple_viewer).to be_a(DiffViewer::Collapsed)
end
end
context 'when the file is not diffable' do context 'when the file is not diffable' do
before do before do
allow(diff_file).to receive(:diffable?).and_return(false) allow(diff_file).to receive(:diffable?).and_return(false)
......
...@@ -9,15 +9,7 @@ describe 'projects/diffs/_viewer.html.haml' do ...@@ -9,15 +9,7 @@ describe 'projects/diffs/_viewer.html.haml' do
let(:commit) { project.commit('570e7b2abdd848b95f2f578043fc23bd6f6fd24d') } let(:commit) { project.commit('570e7b2abdd848b95f2f578043fc23bd6f6fd24d') }
let(:diff_file) { commit.diffs.diff_file_with_new_path('files/ruby/popen.rb') } let(:diff_file) { commit.diffs.diff_file_with_new_path('files/ruby/popen.rb') }
let(:viewer_class) do let(:viewer) { diff_file.simple_viewer }
Class.new(DiffViewer::Base) do
include DiffViewer::Rich
self.partial_name = 'text'
end
end
let(:viewer) { viewer_class.new(diff_file) }
before do before do
assign(:project, project) assign(:project, project)
...@@ -53,7 +45,7 @@ describe 'projects/diffs/_viewer.html.haml' do ...@@ -53,7 +45,7 @@ describe 'projects/diffs/_viewer.html.haml' do
it 'renders the collapsed view' do it 'renders the collapsed view' do
render_view render_view
expect(view).to render_template('projects/diffs/_collapsed') expect(view).to render_template('projects/diffs/viewers/_collapsed')
end 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