summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorMark Chao <mchao@gitlab.com>2019-03-07 12:29:02 +0800
committerMark Chao <mchao@gitlab.com>2019-03-07 16:12:36 +0800
commitcea59dbe030bfde83247ef27c49ffd5267b194ea (patch)
treea71514f4a8670415a62997aa30a3bc5edec9c1fc /app
parentb14de8e1f519b9b874033f783051814129af176c (diff)
downloadgitlab-ce-cea59dbe030bfde83247ef27c49ffd5267b194ea.tar.gz
Move diff_line preparation into presenter
Update spec
Diffstat (limited to 'app')
-rw-r--r--app/controllers/projects/blob_controller.rb39
-rw-r--r--app/presenters/blobs/unfold_presenter.rb31
2 files changed, 35 insertions, 35 deletions
diff --git a/app/controllers/projects/blob_controller.rb b/app/controllers/projects/blob_controller.rb
index 99fda0bf137..0a33856a8d3 100644
--- a/app/controllers/projects/blob_controller.rb
+++ b/app/controllers/projects/blob_controller.rb
@@ -91,51 +91,20 @@ class Projects::BlobController < Projects::ApplicationController
apply_diff_view_cookie!
@form = Blobs::UnfoldPresenter.new(blob, params.to_unsafe_h)
- @lines = @form.lines
- @match_line = @form.match_line_text
- # We can keep only 'render_diff_lines' from this conditional when
+ # keep only json rendering when
# https://gitlab.com/gitlab-org/gitlab-ce/issues/44988 is done
if rendered_for_merge_request?
- render_diff_lines
+ render json: DiffLineSerializer.new.represent(@form.diff_lines)
else
+ @lines = @form.lines
+ @match_line = @form.match_line_text
render layout: false
end
end
private
- # Converts a String array to Gitlab::Diff::Line array
- def render_diff_lines
- @lines.map! do |line|
- diff_line = Gitlab::Diff::Line.new(line, nil, nil, nil, nil)
- diff_line.rich_text = line
- diff_line
- end
-
- add_match_line
-
- render json: DiffLineSerializer.new.represent(@lines)
- end
-
- def add_match_line
- return unless @form.unfold?
-
- if @form.bottom? && @form.to < @blob.lines.size
- old_pos = @form.to - @form.offset
- new_pos = @form.to
- elsif @form.since != 1
- old_pos = new_pos = @form.since
- end
-
- # Match line is not needed when it reaches the top limit or bottom limit of the file.
- return unless new_pos
-
- @match_line = Gitlab::Diff::Line.new(@match_line, 'match', nil, old_pos, new_pos)
-
- @form.bottom? ? @lines.push(@match_line) : @lines.unshift(@match_line)
- end
-
def blob
@blob ||= @repository.blob_at(@commit.id, @path)
diff --git a/app/presenters/blobs/unfold_presenter.rb b/app/presenters/blobs/unfold_presenter.rb
index 908b2f97f89..7b13db3bb74 100644
--- a/app/presenters/blobs/unfold_presenter.rb
+++ b/app/presenters/blobs/unfold_presenter.rb
@@ -25,6 +25,17 @@ module Blobs
end
end
+ # Converts a String array to Gitlab::Diff::Line array, with match line added
+ def diff_lines
+ diff_lines = lines.map do |line|
+ Gitlab::Diff::Line.new(line, nil, nil, nil, nil, rich_text: line)
+ end
+
+ add_match_line(diff_lines)
+
+ diff_lines
+ end
+
def lines
strong_memoize(:lines) do
lines = @all_lines
@@ -40,5 +51,25 @@ module Blobs
line = [since, lines_length].join(',')
"@@ -#{line}+#{line} @@"
end
+
+ private
+
+ def add_match_line(diff_lines)
+ return unless unfold?
+
+ if bottom? && to < @all_lines.size
+ old_pos = to - offset
+ new_pos = to
+ elsif since != 1
+ old_pos = new_pos = since
+ end
+
+ # Match line is not needed when it reaches the top limit or bottom limit of the file.
+ return unless new_pos
+
+ match_line = Gitlab::Diff::Line.new(match_line_text, 'match', nil, old_pos, new_pos)
+
+ bottom? ? diff_lines.push(match_line) : diff_lines.unshift(match_line)
+ end
end
end