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

122 lines
2.8 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 ResolvableDiscussion
extend ActiveSupport::Concern
2018-03-17 18:26:18 +05:30
include ::Gitlab::Utils::StrongMemoize
2017-08-17 22:00:37 +05:30
included do
# A number of properties of this `Discussion`, like `first_note` and `resolvable?`, are memoized.
# When this discussion is resolved or unresolved, the values of these properties potentially change.
# To make sure all memoized values are reset when this happens, `update` resets all instance variables with names in
# `memoized_variables`. If you add a memoized method in `ResolvableDiscussion` or any `Discussion` subclass,
# please make sure the instance variable name is added to `memoized_values`, like below.
cattr_accessor :memoized_values, instance_accessor: false do
[]
end
memoized_values.push(
:resolvable,
:resolved,
:first_note,
:first_note_to_resolve,
:last_resolved_note,
:last_note
)
2020-06-23 00:09:42 +05:30
delegate :potentially_resolvable?,
:noteable_id,
:noteable_type,
to: :first_note
2017-08-17 22:00:37 +05:30
delegate :resolved_at,
:resolved_by,
to: :last_resolved_note,
allow_nil: true
end
2022-01-26 12:08:38 +05:30
def resolved_by_push?
!!last_resolved_note&.resolved_by_push?
end
2017-08-17 22:00:37 +05:30
def resolvable?
2018-03-17 18:26:18 +05:30
strong_memoize(:resolvable) do
potentially_resolvable? && notes.any?(&:resolvable?)
end
2017-08-17 22:00:37 +05:30
end
def resolved?
2018-03-17 18:26:18 +05:30
strong_memoize(:resolved) do
resolvable? && notes.none?(&:to_be_resolved?)
end
2017-08-17 22:00:37 +05:30
end
def first_note
2018-03-17 18:26:18 +05:30
strong_memoize(:first_note) do
notes.first
end
2017-08-17 22:00:37 +05:30
end
def first_note_to_resolve
return unless resolvable?
2018-03-17 18:26:18 +05:30
strong_memoize(:first_note_to_resolve) do
notes.find(&:to_be_resolved?)
end
2017-08-17 22:00:37 +05:30
end
def last_resolved_note
return unless resolved?
2018-03-17 18:26:18 +05:30
strong_memoize(:last_resolved_note) do
2020-03-13 15:44:24 +05:30
resolved_notes.max_by(&:resolved_at)
2018-03-17 18:26:18 +05:30
end
2017-08-17 22:00:37 +05:30
end
def resolved_notes
notes.select(&:resolved?)
end
def to_be_resolved?
resolvable? && !resolved?
end
def can_resolve?(current_user)
return false unless current_user
return false unless resolvable?
2021-10-29 20:43:33 +05:30
current_user.can?(:resolve_note, self.noteable)
2017-08-17 22:00:37 +05:30
end
def resolve!(current_user)
return unless resolvable?
update { |notes| notes.resolve!(current_user) }
end
def unresolve!
return unless resolvable?
2023-03-17 16:20:25 +05:30
update(&:unresolve!)
2017-08-17 22:00:37 +05:30
end
2022-04-04 11:22:00 +05:30
def clear_memoized_values
self.class.memoized_values.each do |name|
clear_memoization(name)
end
end
2017-08-17 22:00:37 +05:30
private
def update
# Do not select `Note.resolvable`, so that system notes remain in the collection
notes_relation = Note.where(id: notes.map(&:id))
yield(notes_relation)
# Set the notes array to the updated notes
2018-03-17 18:26:18 +05:30
@notes = notes_relation.fresh.to_a # rubocop:disable Gitlab/ModuleWithInstanceVariables
2017-08-17 22:00:37 +05:30
2022-04-04 11:22:00 +05:30
clear_memoized_values
2017-08-17 22:00:37 +05:30
end
end