Simplify HAML code using unless condition and nesting

Extract diff line data attributes to helper methods
This commit is contained in:
Paco Guzman 2016-07-06 06:59:18 +02:00
parent fa981c9794
commit 39b6e678da
7 changed files with 30 additions and 14 deletions

View File

@ -24,7 +24,6 @@ class Projects::CompareController < Projects::ApplicationController
@diff_refs = [@base_commit, @commit]
@diff_notes_disabled = true
@grouped_diff_notes = {}
@comments_target = {}
end
end

View File

@ -138,7 +138,6 @@ class Projects::MergeRequestsController < Projects::ApplicationController
@base_commit = @merge_request.diff_base_commit
@diffs = @merge_request.compare.diffs(diff_options) if @merge_request.compare
@diff_notes_disabled = true
@comments_target = {}
@pipeline = @merge_request.pipeline
@statuses = @pipeline.statuses if @pipeline

View File

@ -24,6 +24,27 @@ module NotesHelper
}.to_json
end
def note_text_file_data
return {} unless defined?(@comments_target) && @comments_target.any?
@comments_target.slice(:noteable_id, :noteable_type, :commit_id).merge(note_type: LegacyDiffNote.name)
end
def note_line_parallel_data(line_code, line_type)
data = {
line_code: line_code,
line_type: line_type,
}
unless @diff_notes_disabled
data.merge!(
discussion_id: discussion_id(line_code)
)
end
data
end
def discussion_id(line_code)
LegacyDiffNote.build_discussion_id(
@comments_target[:noteable_type],

View File

@ -30,7 +30,6 @@ module Emails
@target_url = @message.target_url
@project = Project.find(project_id)
@diff_notes_disabled = true
@comments_target = {}
add_project_headers
headers['X-GitLab-Author'] = @message.author_username

View File

@ -1,5 +1,5 @@
- type = line.type
- line_data = @comments_target.any? ? { data: { discussion_id: discussion_id(line_code) } } : {}
- line_data = @diff_notes_disabled ? {} : { data: { discussion_id: discussion_id(line_code) } }
%tr.line_holder{ line_data, id: line_code, class: type }
- case type
- when 'match'
@ -15,11 +15,11 @@
- if defined?(plain) && plain
= link_text
- else
%a{href: "##{line_code}", data: { linenumber: link_text }}= " "
%a{href: "##{line_code}", data: { linenumber: link_text }}
%td.new_line.diff-line-num{ class: type, data: { linenumber: line.new_pos } }
- link_text = type == "old" ? " " : line.new_pos
- if defined?(plain) && plain
= link_text
- else
%a{href: "##{line_code}", data: { linenumber: link_text }}= ""
%a{href: "##{line_code}", data: { linenumber: link_text }}
%td.line_content{ class: ['noteable_line', type], data: { line_code: line_code, line_type: type } }= diff_line_content(line.text, type)

View File

@ -1,6 +1,5 @@
/ Side-by-side diff view
- text_file_data = @comments_target.any? ? { data: { noteable_id: @comments_target[:noteable_id], noteable_type: @comments_target[:noteable_type], commit_id: @comments_target[:commit_id], note_type: LegacyDiffNote.name } } : {}
%div.text-file.diff-wrap-lines.code.file-content.js-syntax-highlight{ text_file_data }
%div.text-file.diff-wrap-lines.code.file-content.js-syntax-highlight{ data: note_text_file_data }
%table
- diff_file.parallel_diff_lines.each do |line|
- left = line[:left]
@ -14,9 +13,9 @@
%td.new_line.diff-line-num.empty-cell
%td.line_content.parallel.match= left[:text]
- else
%td.old_line.diff-line-num{id: left[:line_code], class: "#{left[:type]} #{'empty-cell' if !left[:number]}", data: { linenumber: left[:number] }}
%td.old_line.diff-line-num{id: left[:line_code], class: "#{left[:type]} #{'empty-cell' unless left[:number]}", data: { linenumber: left[:number] }}
%a{href: "##{left[:line_code]}" }= raw(left[:number])
%td.line_content{class: "parallel noteable_line #{left[:type]} #{'empty-cell' if left[:text].empty?}", data: { discussion_id: discussion_id(left[:line_code]), line_type: left[:type], line_code: left[:line_code] }}= diff_line_content(left[:text])
%td.line_content{class: "parallel noteable_line #{left[:type]} #{'empty-cell' if left[:text].empty?}", data: note_line_parallel_data(left[:line_code], left[:type]) }= diff_line_content(left[:text])
- if right[:type] == 'new'
- new_line_class = 'new'
@ -25,9 +24,9 @@
- new_line_class = nil
- new_line_code = left[:line_code]
%td.new_line.diff-line-num{id: new_line_code, class: "#{new_line_class} #{'empty-cell' if !right[:number]}", data: { linenumber: right[:number] } }
%td.new_line.diff-line-num{id: new_line_code, class: "#{new_line_class} #{'empty-cell' unless right[:number]}", data: { linenumber: right[:number] } }
%a{href: "##{new_line_code}" }= raw(right[:number])
%td.line_content.parallel{class: "noteable_line #{new_line_class} #{'empty-cell' if right[:text].empty?}", data: { discussion_id: discussion_id(new_line_code), line_type: new_line_class, line_code: new_line_code }}= diff_line_content(right[:text])
%td.line_content.parallel{class: "noteable_line #{new_line_class} #{'empty-cell' if right[:text].empty?}", data: note_line_parallel_data(new_line_code, new_line_class) }= diff_line_content(right[:text])
- unless @diff_notes_disabled
- notes_left, notes_right = organize_comments(left, right)

View File

@ -3,8 +3,7 @@
.suppressed-container
%a.show-suppressed-diff.js-show-suppressed-diff Changes suppressed. Click to show.
- text_file_data = @comments_target.any? ? { data: { noteable_id: @comments_target[:noteable_id], noteable_type: @comments_target[:noteable_type], commit_id: @comments_target[:commit_id], note_type: LegacyDiffNote.name } } : {}
%table.text-file.code.js-syntax-highlight{ text_file_data, class: too_big ? 'hide' : '' }
%table.text-file.code.js-syntax-highlight{ data: note_text_file_data, class: too_big ? 'hide' : '' }
- last_line = 0
- diff_file.highlighted_diff_lines.each_with_index do |line, index|