diff options
author | Douwe Maan <douwe@selenight.nl> | 2016-08-17 12:14:44 -0500 |
---|---|---|
committer | Douwe Maan <douwe@selenight.nl> | 2016-08-17 12:16:46 -0500 |
commit | 4a13aa9f34ab4114bc485e1ca8fa0db8daa0394b (patch) | |
tree | 17554a901009603f52be08914636495b06db2e68 /app/models/note.rb | |
parent | f3acf9fd248a16665a114bf6cce761e9277c2d5b (diff) | |
download | gitlab-ce-4a13aa9f34ab4114bc485e1ca8fa0db8daa0394b.tar.gz |
Store discussion_id on Note for faster discussion lookup.
Diffstat (limited to 'app/models/note.rb')
-rw-r--r-- | app/models/note.rb | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/app/models/note.rb b/app/models/note.rb index 732b2ff9bf7..fb0d0ebc396 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -70,7 +70,9 @@ class Note < ActiveRecord::Base project: [:project_members, { group: [:group_members] }]) end + after_initialize :ensure_discussion_id before_validation :nullify_blank_type, :nullify_blank_line_code + before_validation :set_discussion_id after_save :keep_around_commit class << self @@ -82,6 +84,10 @@ class Note < ActiveRecord::Base [:discussion, noteable_type.try(:underscore), noteable_id].join("-") end + def self.discussion_id(*args) + Digest::SHA1.hexdigest(build_discussion_id(*args)) + end + def discussions Discussion.for_notes(all) end @@ -142,15 +148,6 @@ class Note < ActiveRecord::Base resolvable? && !resolved? end - def discussion_id - @discussion_id ||= - if for_merge_request? - Digest::SHA1.hexdigest([:discussion, :note, id].join("-")) - else - Digest::SHA1.hexdigest(self.class.build_discussion_id(noteable_type, noteable_id || commit_id)) - end - end - def max_attachment_size current_application_settings.max_attachment_size.megabytes.to_i end @@ -256,4 +253,24 @@ class Note < ActiveRecord::Base def nullify_blank_line_code self.line_code = nil if self.line_code.blank? end + + def ensure_discussion_id + return unless self.persisted? + return if self.discussion_id + + set_discussion_id + update_column(:discussion_id, self.discussion_id) + end + + def set_discussion_id + self.discussion_id = Digest::SHA1.hexdigest(build_discussion_id) + end + + def build_discussion_id + if for_merge_request? + [:discussion, :note, id].join("-") + else + self.class.build_discussion_id(noteable_type, noteable_id || commit_id) + end + end end |