1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
# frozen_string_literal: true
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?`
scope :potentially_resolvable, -> { where(type: resolvable_types).where(noteable_type: Noteable.resolvable_types) }
# 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
class_methods do
# This method must be kept in sync with `#resolve!`
def resolve!(current_user)
now = Time.current
unresolved.update_all(updated_at: now, resolved_at: now, resolved_by_id: current_user.id)
end
# This method must be kept in sync with `#unresolve!`
def unresolve!
resolved.update_all(updated_at: Time.current, resolved_at: nil, resolved_by_id: nil)
end
# overridden on EE
def resolvable_types
RESOLVABLE_TYPES
end
end
# Keep this method in sync with the `potentially_resolvable` scope
def potentially_resolvable?
self.class.resolvable_types.include?(self.class.name) && noteable&.supports_resolvable_notes?
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!`
def resolve_without_save(current_user, resolved_by_push: false)
return false unless resolvable?
return false if resolved?
now = Time.current
self.updated_at = now
self.resolved_at = now
self.resolved_by = current_user
self.resolved_by_push = resolved_by_push
true
end
# If you update this method remember to also update `.unresolve!`
def unresolve_without_save
return false unless resolvable?
return false unless resolved?
self.updated_at = Time.current
self.resolved_at = nil
self.resolved_by = nil
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!
end
end
ResolvableNote::ClassMethods.prepend_mod_with('ResolvableNote::ClassMethods')
|