diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-08-04 19:01:57 +0300 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-08-04 19:01:57 +0300 |
commit | 2d75b454ef87a87bff942dc31837687919113468 (patch) | |
tree | 26640730044bad8eb3e55e78b5ff6930b56e0e1d /app/models | |
parent | e06352979de1a3344d0ab34c5f0ecf7ed73da66f (diff) | |
download | gitlab-ce-2d75b454ef87a87bff942dc31837687919113468.tar.gz |
Store diff with line note. It makes possible to see inline notes with proper diff even if MR diff changed
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/note.rb | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/app/models/note.rb b/app/models/note.rb index c23aab03bcc..b0875a0761b 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -50,6 +50,9 @@ class Note < ActiveRecord::Base scope :inc_author_project, ->{ includes(:project, :author) } scope :inc_author, ->{ includes(:author) } + serialize :st_diff + before_create :set_diff, if: ->(n) { n.noteable_type == 'MergeRequest' && n.line_code.present? } + def self.create_status_change_note(noteable, author, status) create({ noteable: noteable, @@ -67,22 +70,33 @@ class Note < ActiveRecord::Base nil end - def diff - if noteable.diffs.present? - noteable.diffs.select do |d| - if d.new_path - Digest::SHA1.hexdigest(d.new_path) == diff_file_index - end - end.first + def find_diff + return nil unless noteable.diffs.present? + + @diff ||= noteable.diffs.find do |d| + Digest::SHA1.hexdigest(d.new_path) == diff_file_index if d.new_path end end + def set_diff + # First lets find notes with same diff + # before iterating over all mr diffs + diff = self.noteable.notes.where(line_code: self.line_code).last.try(:diff) + diff ||= find_diff + + self.st_diff = diff.to_hash if diff + end + + def diff + @diff ||= Gitlab::Git::Diff.new(st_diff) if st_diff.respond_to?(:map) + end + def diff_file_index line_code.split('_')[0] end def diff_file_name - diff.new_path + diff.new_path if diff end def diff_new_line |