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