debian-mirror-gitlab/app/models/concerns/resolvable_note.rb

87 lines
2.3 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module ResolvableNote
extend ActiveSupport::Concern
# Names of all subclasses of `Note` that can be resolvable.
RESOLVABLE_TYPES = %w(DiffNote DiscussionNote).freeze
included do
belongs_to :resolved_by, class_name: "User"
validates :resolved_by, presence: true, if: :resolved?
# Keep this scope in sync with `#potentially_resolvable?`
2019-09-04 21:01:54 +05:30
scope :potentially_resolvable, -> { where(type: RESOLVABLE_TYPES).where(noteable_type: Noteable.resolvable_types) }
2017-08-17 22:00:37 +05:30
# Keep this scope in sync with `#resolvable?`
scope :resolvable, -> { potentially_resolvable.user }
scope :resolved, -> { resolvable.where.not(resolved_at: nil) }
scope :unresolved, -> { resolvable.where(resolved_at: nil) }
end
2018-11-20 20:47:30 +05:30
class_methods do
2017-08-17 22:00:37 +05:30
# 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
# Keep this method in sync with the `potentially_resolvable` scope
def potentially_resolvable?
2018-11-08 19:23:39 +05:30
RESOLVABLE_TYPES.include?(self.class.name) && noteable&.supports_resolvable_notes?
2017-08-17 22:00:37 +05:30
end
# Keep this method in sync with the `resolvable` scope
def resolvable?
potentially_resolvable? && !system?
end
def resolved?
return false unless resolvable?
self.resolved_at.present?
end
def to_be_resolved?
resolvable? && !resolved?
end
# If you update this method remember to also update `.resolve!`
2018-03-17 18:26:18 +05:30
def resolve_without_save(current_user, resolved_by_push: false)
return false unless resolvable?
return false if resolved?
2017-08-17 22:00:37 +05:30
self.resolved_at = Time.now
self.resolved_by = current_user
2018-03-17 18:26:18 +05:30
self.resolved_by_push = resolved_by_push
true
2017-08-17 22:00:37 +05:30
end
# If you update this method remember to also update `.unresolve!`
2018-03-17 18:26:18 +05:30
def unresolve_without_save
return false unless resolvable?
return false unless resolved?
2017-08-17 22:00:37 +05:30
self.resolved_at = nil
self.resolved_by = nil
2018-03-17 18:26:18 +05:30
true
end
def resolve!(current_user, resolved_by_push: false)
resolve_without_save(current_user, resolved_by_push: resolved_by_push) &&
save!
end
def unresolve!
unresolve_without_save && save!
2017-08-17 22:00:37 +05:30
end
end