diff options
author | Douwe Maan <douwe@selenight.nl> | 2017-03-09 19:29:11 -0600 |
---|---|---|
committer | Luke "Jared" Bennett <lbennett@gitlab.com> | 2017-04-05 17:44:14 +0100 |
commit | 08bbb9fce66cb46d3262e6cd4c4379b59f065be0 (patch) | |
tree | 159eeb7ca43419f29926d6e77637db18bddd20a9 /app/models/diff_note.rb | |
parent | 8bdfee8ba5fb0a8f48501e63274c8f9ce5708007 (diff) | |
download | gitlab-ce-08bbb9fce66cb46d3262e6cd4c4379b59f065be0.tar.gz |
Add option to start a new discussion on an MR
Diffstat (limited to 'app/models/diff_note.rb')
-rw-r--r-- | app/models/diff_note.rb | 115 |
1 files changed, 22 insertions, 93 deletions
diff --git a/app/models/diff_note.rb b/app/models/diff_note.rb index 895a91139c9..e5f437f8647 100644 --- a/app/models/diff_note.rb +++ b/app/models/diff_note.rb @@ -9,58 +9,44 @@ class DiffNote < Note validates :diff_line, presence: true validates :line_code, presence: true, line_code: true validates :noteable_type, inclusion: { in: %w(Commit MergeRequest) } - validates :resolved_by, presence: true, if: :resolved? validate :positions_complete validate :verify_supported - # Keep this scope in sync with the logic in `#resolvable?` - scope :resolvable, -> { user.where(noteable_type: 'MergeRequest') } - scope :resolved, -> { resolvable.where.not(resolved_at: nil) } - scope :unresolved, -> { resolvable.where(resolved_at: nil) } - - after_initialize :ensure_original_discussion_id before_validation :set_original_position, :update_position, on: :create - before_validation :set_line_code, :set_original_discussion_id + before_validation :set_line_code # We need to do this again, because it's already in `Note`, but is affected by # `update_position` and needs to run after that. - before_validation :set_discussion_id + before_validation :set_discussion_id, if: :position_changed? after_save :keep_around_commits - class << self - def build_discussion_id(noteable_type, noteable_id, position) - [super(noteable_type, noteable_id), *position.key].join("-") - end - - # This method must be kept in sync with `#resolve!` - def resolve!(current_user) - unresolved.update_all(resolved_at: Time.now, resolved_by_id: current_user.id) - end - - # This method must be kept in sync with `#unresolve!` - def unresolve! - resolved.update_all(resolved_at: nil, resolved_by_id: nil) - end - end - def new_diff_note? true end + def discussion_class(*) + DiffDiscussion + end + def diff_attributes - { position: position.to_json } + { + original_position: original_position.to_json, + position: position.to_json, + } end - def position=(new_position) - if new_position.is_a?(String) - new_position = JSON.parse(new_position) rescue nil - end + %i(original_position= position=).each do |meth| + define_method meth do |new_position| + if new_position.is_a?(String) + new_position = JSON.parse(new_position) rescue nil + end - if new_position.is_a?(Hash) - new_position = new_position.with_indifferent_access - new_position = Gitlab::Diff::Position.new(new_position) - end + if new_position.is_a?(Hash) + new_position = new_position.with_indifferent_access + new_position = Gitlab::Diff::Position.new(new_position) + end - super(new_position) + super(new_position) + end end def diff_file @@ -88,43 +74,6 @@ class DiffNote < Note self.position.diff_refs == diff_refs end - # If you update this method remember to also update the scope `resolvable` - def resolvable? - !system? && for_merge_request? - end - - def resolved? - return false unless resolvable? - - self.resolved_at.present? - end - - # If you update this method remember to also update `.resolve!` - def resolve!(current_user) - return unless resolvable? - return if resolved? - - self.resolved_at = Time.now - self.resolved_by = current_user - save! - end - - # If you update this method remember to also update `.unresolve!` - def unresolve! - return unless resolvable? - return unless resolved? - - self.resolved_at = nil - self.resolved_by = nil - save! - end - - def discussion - return unless resolvable? - - self.noteable.find_diff_discussion(self.discussion_id) - end - private def supported? @@ -140,33 +89,13 @@ class DiffNote < Note end def set_original_position - self.original_position = self.position.dup + self.original_position = self.position.dup unless self.original_position&.complete? end def set_line_code self.line_code = self.position.line_code(self.project.repository) end - def ensure_original_discussion_id - return unless self.persisted? - return if self.original_discussion_id - - set_original_discussion_id - update_column(:original_discussion_id, self.original_discussion_id) - end - - def set_original_discussion_id - self.original_discussion_id = Digest::SHA1.hexdigest(build_original_discussion_id) - end - - def build_discussion_id - self.class.build_discussion_id(noteable_type, noteable_id || commit_id, position) - end - - def build_original_discussion_id - self.class.build_discussion_id(noteable_type, noteable_id || commit_id, original_position) - end - def update_position return unless supported? return if for_commit? |