BigW Consortium Gitlab

resolvable_note.rb 1.96 KB
Newer Older
1 2 3
module ResolvableNote
  extend ActiveSupport::Concern

4
  # Names of all subclasses of `Note` that can be resolvable.
Douwe Maan committed
5 6
  RESOLVABLE_TYPES = %w(DiffNote DiscussionNote).freeze

7 8 9 10 11
  included do
    belongs_to :resolved_by, class_name: "User"

    validates :resolved_by, presence: true, if: :resolved?

12 13
    # Keep this scope in sync with `#potentially_resolvable?`
    scope :potentially_resolvable, -> { where(type: RESOLVABLE_TYPES).where(noteable_type: Noteable::RESOLVABLE_TYPES) }
Douwe Maan committed
14 15 16
    # Keep this scope in sync with `#resolvable?`
    scope :resolvable, -> { potentially_resolvable.user }

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
    scope :resolved, -> { resolvable.where.not(resolved_at: nil) }
    scope :unresolved, -> { resolvable.where(resolved_at: nil) }
  end

  module ClassMethods
    # 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

33 34 35 36
  # Keep this method in sync with the `potentially_resolvable` scope
  def potentially_resolvable?
    RESOLVABLE_TYPES.include?(self.class.name) && noteable.supports_resolvable_notes?
  end
Douwe Maan committed
37 38

  # Keep this method in sync with the `resolvable` scope
39
  def resolvable?
Douwe Maan committed
40
    potentially_resolvable? && !system?
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
  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!(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
end