summaryrefslogtreecommitdiff
path: root/app/models/discussion.rb
diff options
context:
space:
mode:
authorDouwe Maan <douwe@selenight.nl>2017-03-30 18:38:21 -0600
committerLuke "Jared" Bennett <lbennett@gitlab.com>2017-04-05 17:44:14 +0100
commit2058e71e63c9ac471137f831b4d04b6626968532 (patch)
tree5c06529ab83a209f8ad7b203d1f131a97041b0bf /app/models/discussion.rb
parent874413cf701870a0fc1051f7c0a5fc4b4f884657 (diff)
downloadgitlab-ce-2058e71e63c9ac471137f831b4d04b6626968532.tar.gz
Extract commonalities between DiffDiscussion and LegacyDiffDiscussion
Diffstat (limited to 'app/models/discussion.rb')
-rw-r--r--app/models/discussion.rb81
1 files changed, 6 insertions, 75 deletions
diff --git a/app/models/discussion.rb b/app/models/discussion.rb
index 6e97a4862ed..27ed0480e6d 100644
--- a/app/models/discussion.rb
+++ b/app/models/discussion.rb
@@ -1,5 +1,9 @@
class Discussion
- MEMOIZED_VALUES = [] # rubocop:disable Style/MutableConstant
+ cattr_accessor :memoized_values, instance_accessor: false do
+ []
+ end
+
+ include ResolvableDiscussion
attr_reader :notes, :noteable
@@ -13,12 +17,6 @@ class Discussion
to: :first_note
- delegate :resolved_at,
- :resolved_by,
-
- to: :last_resolved_note,
- allow_nil: true
-
def self.build(notes, noteable = nil)
notes.first.discussion_class(noteable).new(notes, noteable)
end
@@ -98,76 +96,9 @@ class Discussion
notes.length == 1
end
- # Keep this method in sync with the `potentially_resolvable` scope on `ResolvableNote`
- def potentially_resolvable?
- for_merge_request?
- end
-
- def resolvable?
- return @resolvable if @resolvable.present?
-
- @resolvable = potentially_resolvable? && notes.any?(&:resolvable?)
- end
- MEMOIZED_VALUES << :resolvable
-
- def resolved?
- return @resolved if @resolved.present?
-
- @resolved = resolvable? && notes.none?(&:to_be_resolved?)
- end
- MEMOIZED_VALUES << :resolved
-
- def first_note
- @first_note ||= notes.first
- end
- MEMOIZED_VALUES << :first_note
-
- def first_note_to_resolve
- return unless resolvable?
-
- @first_note_to_resolve ||= notes.find(&:to_be_resolved?)
- end
- MEMOIZED_VALUES << :first_note_to_resolve
-
- def last_resolved_note
- return unless resolved?
-
- @last_resolved_note ||= resolved_notes.sort_by(&:resolved_at).last
- end
- MEMOIZED_VALUES << :last_resolved_note
-
def last_note
@last_note ||= notes.last
end
- MEMOIZED_VALUES << :last_note
-
- 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?
-
- current_user == self.noteable.author ||
- current_user.can?(:resolve_note, self.project)
- end
-
- def resolve!(current_user)
- return unless resolvable?
-
- update { |notes| notes.resolve!(current_user) }
- end
-
- def unresolve!
- return unless resolvable?
-
- update { |notes| notes.unresolve! }
- end
def collapsed?
resolved?
@@ -192,7 +123,7 @@ class Discussion
# Set the notes array to the updated notes
@notes = notes_relation.fresh.to_a
- MEMOIZED_VALUES.each do |var|
+ self.class.memoized_values.each do |var|
instance_variable_set(:"@#{var}", nil)
end
end