summaryrefslogtreecommitdiff
path: root/app/models/concerns/resolvable_discussion.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/concerns/resolvable_discussion.rb')
-rw-r--r--app/models/concerns/resolvable_discussion.rb25
1 files changed, 25 insertions, 0 deletions
diff --git a/app/models/concerns/resolvable_discussion.rb b/app/models/concerns/resolvable_discussion.rb
index 22cd9bb7fd4..5cb51196a94 100644
--- a/app/models/concerns/resolvable_discussion.rb
+++ b/app/models/concerns/resolvable_discussion.rb
@@ -2,6 +2,15 @@ module ResolvableDiscussion
extend ActiveSupport::Concern
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,
@@ -78,4 +87,20 @@ module ResolvableDiscussion
update { |notes| notes.unresolve! }
end
+
+ 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
+ @notes = notes_relation.fresh.to_a
+
+ self.class.memoized_values.each do |var|
+ instance_variable_set(:"@#{var}", nil)
+ end
+ end
end