summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorRobert Speicher <robert@gitlab.com>2016-07-21 17:56:23 +0000
committerRobert Speicher <robert@gitlab.com>2016-07-21 17:56:23 +0000
commit89292551295418cf4b5b90ce904a6b41f19a8be3 (patch)
tree838aba8af56513d28760ae15516479a7eaf34e15 /app/models
parent0606b92c8532a0268a919bd3d0c5a083bad5ecf9 (diff)
parent79214be727aaa0704a1be5b50aa6dd3011629bc2 (diff)
downloadgitlab-ce-89292551295418cf4b5b90ce904a6b41f19a8be3.tar.gz
Merge branch 'discussion-model' into 'master'
Add Discussion model to represent MR/diff discussion Related to https://gitlab.com/gitlab-org/gitlab-ce/issues/10325. See merge request !5376
Diffstat (limited to 'app/models')
-rw-r--r--app/models/concerns/note_on_diff.rb25
-rw-r--r--app/models/discussion.rb91
-rw-r--r--app/models/note.rb7
3 files changed, 95 insertions, 28 deletions
diff --git a/app/models/concerns/note_on_diff.rb b/app/models/concerns/note_on_diff.rb
index 2785fbb21c9..4be6a2f621b 100644
--- a/app/models/concerns/note_on_diff.rb
+++ b/app/models/concerns/note_on_diff.rb
@@ -1,12 +1,6 @@
module NoteOnDiff
extend ActiveSupport::Concern
- NUMBER_OF_TRUNCATED_DIFF_LINES = 16
-
- included do
- delegate :blob, :highlighted_diff_lines, to: :diff_file, allow_nil: true
- end
-
def diff_note?
true
end
@@ -30,23 +24,4 @@ module NoteOnDiff
def can_be_award_emoji?
false
end
-
- # Returns an array of at most 16 highlighted lines above a diff note
- def truncated_diff_lines
- prev_lines = []
-
- highlighted_diff_lines.each do |line|
- if line.meta?
- prev_lines.clear
- else
- prev_lines << line
-
- break if for_line?(line)
-
- prev_lines.shift if prev_lines.length >= NUMBER_OF_TRUNCATED_DIFF_LINES
- end
- end
-
- prev_lines
- end
end
diff --git a/app/models/discussion.rb b/app/models/discussion.rb
new file mode 100644
index 00000000000..74facfd1c9c
--- /dev/null
+++ b/app/models/discussion.rb
@@ -0,0 +1,91 @@
+class Discussion
+ NUMBER_OF_TRUNCATED_DIFF_LINES = 16
+
+ attr_reader :first_note, :notes
+
+ delegate :created_at,
+ :project,
+ :author,
+
+ :noteable,
+ :for_commit?,
+ :for_merge_request?,
+
+ :line_code,
+ :diff_file,
+ :for_line?,
+ :active?,
+
+ to: :first_note
+
+ delegate :blob, :highlighted_diff_lines, to: :diff_file, allow_nil: true
+
+ def self.for_notes(notes)
+ notes.group_by(&:discussion_id).values.map { |notes| new(notes) }
+ end
+
+ def self.for_diff_notes(notes)
+ notes.group_by(&:line_code).values.map { |notes| new(notes) }
+ end
+
+ def initialize(notes)
+ @first_note = notes.first
+ @notes = notes
+ end
+
+ def id
+ first_note.discussion_id
+ end
+
+ def diff_discussion?
+ first_note.diff_note?
+ end
+
+ def legacy_diff_discussion?
+ notes.any?(&:legacy_diff_note?)
+ end
+
+ def for_target?(target)
+ self.noteable == target && !diff_discussion?
+ end
+
+ def expanded?
+ !diff_discussion? || active?
+ end
+
+ def reply_attributes
+ data = {
+ noteable_type: first_note.noteable_type,
+ noteable_id: first_note.noteable_id,
+ commit_id: first_note.commit_id,
+ discussion_id: self.id,
+ }
+
+ if diff_discussion?
+ data[:note_type] = first_note.type
+
+ data.merge!(first_note.diff_attributes)
+ end
+
+ data
+ end
+
+ # Returns an array of at most 16 highlighted lines above a diff note
+ def truncated_diff_lines
+ prev_lines = []
+
+ highlighted_diff_lines.each do |line|
+ if line.meta?
+ prev_lines.clear
+ else
+ prev_lines << line
+
+ break if for_line?(line)
+
+ prev_lines.shift if prev_lines.length >= NUMBER_OF_TRUNCATED_DIFF_LINES
+ end
+ end
+
+ prev_lines
+ end
+end
diff --git a/app/models/note.rb b/app/models/note.rb
index 0ce10c77de9..9b0a7211b4e 100644
--- a/app/models/note.rb
+++ b/app/models/note.rb
@@ -82,11 +82,12 @@ class Note < ActiveRecord::Base
end
def discussions
- all.group_by(&:discussion_id).values
+ Discussion.for_notes(all)
end
- def grouped_diff_notes
- diff_notes.select(&:active?).sort_by(&:created_at).group_by(&:line_code)
+ def grouped_diff_discussions
+ notes = diff_notes.fresh.select(&:active?)
+ Discussion.for_diff_notes(notes).map { |d| [d.line_code, d] }.to_h
end
# Searches for notes matching the given query.