diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-08-08 12:22:09 +0300 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-08-08 12:22:09 +0300 |
commit | 4537623d12fb7372262267be5d6ec3b41f3f476c (patch) | |
tree | ccd0494b1112c388114e032f994505a2344123be /app/models/note.rb | |
parent | c7e490ebd5ab69fe043cd39145bd6ef3850a6921 (diff) | |
parent | 1a83fea711961844adc7ddb21ce007143fefc144 (diff) | |
download | gitlab-ce-4537623d12fb7372262267be5d6ec3b41f3f476c.tar.gz |
Merge branch 'master' into karlhungus-mr-on-fork
Conflicts:
app/contexts/filter_context.rb
app/contexts/search_context.rb
app/models/merge_request.rb
app/models/note.rb
app/views/shared/_merge_requests.html.haml
spec/controllers/commit_controller_spec.rb
spec/services/notification_service_spec.rb
Diffstat (limited to 'app/models/note.rb')
-rw-r--r-- | app/models/note.rb | 52 |
1 files changed, 44 insertions, 8 deletions
diff --git a/app/models/note.rb b/app/models/note.rb index fecee950713..c0bf79237c1 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.line_code.present? } + def self.create_status_change_note(noteable, project, author, status) create({ noteable: noteable, @@ -67,28 +70,61 @@ 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 && 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 = Note.where(noteable_id: self.noteable_id, noteable_type: self.noteable_type, 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 active? + # TODO: determine if discussion is outdated + # according to recent MR diff or not + true + 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_old_line + line_code.split('_')[1].to_i end def diff_new_line line_code.split('_')[2].to_i end + def diff_line + return @diff_line if @diff_line + + if diff + Gitlab::DiffParser.new(diff).each do |full_line, type, line_code, line_new, line_old| + @diff_line = full_line if line_code == self.line_code + end + end + + @diff_line + end + def discussion_id @discussion_id ||= [:discussion, noteable_type.try(:underscore), noteable_id || commit_id, line_code].join("-").to_sym end |