diff options
| author | Robert Speicher <rspeicher@gmail.com> | 2016-03-08 20:04:13 -0500 |
|---|---|---|
| committer | Robert Speicher <rspeicher@gmail.com> | 2016-03-11 18:16:34 -0500 |
| commit | a63eba9a2bebd6e33c3d1051a0d2fd08e024f546 (patch) | |
| tree | c9c2ad7650f50b6baa3df85adc96be09dceff62b /app/models/note.rb | |
| parent | 491d0ae1c306f60919f8048844a130d89e41f565 (diff) | |
| download | gitlab-ce-a63eba9a2bebd6e33c3d1051a0d2fd08e024f546.tar.gz | |
Add unit specs for `Note#active?`rs-note-active-spec
Diffstat (limited to 'app/models/note.rb')
| -rw-r--r-- | app/models/note.rb | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/app/models/note.rb b/app/models/note.rb index 8b0610ff77e..0ea61b24955 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -172,26 +172,29 @@ class Note < ActiveRecord::Base Note.where(noteable_id: noteable_id, noteable_type: noteable_type, line_code: line_code).last.try(:diff) end - # Check if such line of code exists in merge request diff - # If exists - its active discussion - # If not - its outdated diff + # Check if this note is part of an "active" discussion + # + # This will always return true for anything except MergeRequest noteables, + # which have special logic. + # + # If the note's current diff cannot be matched in the MergeRequest's current + # diff, it's considered inactive. def active? return true unless self.diff return false unless noteable return @active if defined?(@active) - diffs = noteable.diffs(Commit.max_diff_options) - notable_diff = diffs.find { |d| d.new_path == self.diff.new_path } + noteable_diff = find_noteable_diff - return @active = false if notable_diff.nil? + if noteable_diff + parsed_lines = Gitlab::Diff::Parser.new.parse(noteable_diff.diff.each_line) - parsed_lines = Gitlab::Diff::Parser.new.parse(notable_diff.diff.each_line) - # We cannot use ||= because @active may be false - @active = parsed_lines.any? { |line_obj| line_obj.text == diff_line } - end + @active = parsed_lines.any? { |line_obj| line_obj.text == diff_line } + else + @active = false + end - def outdated? - !active? + @active end def diff_file_index @@ -375,6 +378,12 @@ class Note < ActiveRecord::Base private + # Find the diff on noteable that matches our own + def find_noteable_diff + diffs = noteable.diffs(Commit.max_diff_options) + diffs.find { |d| d.new_path == self.diff.new_path } + end + def awards_supported? (for_issue? || for_merge_request?) && !for_diff_line? end |
