Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
2058e71e
Commit
2058e71e
authored
Mar 30, 2017
by
Douwe Maan
Committed by
Luke "Jared" Bennett
Apr 05, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract commonalities between DiffDiscussion and LegacyDiffDiscussion
parent
874413cf
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
162 additions
and
164 deletions
+162
-164
app/models/concerns/discussion_on_diff.rb
app/models/concerns/discussion_on_diff.rb
+56
-0
app/models/concerns/resolvable_discussion.rb
app/models/concerns/resolvable_discussion.rb
+79
-0
app/models/diff_discussion.rb
app/models/diff_discussion.rb
+8
-52
app/models/diff_note.rb
app/models/diff_note.rb
+2
-13
app/models/discussion.rb
app/models/discussion.rb
+6
-75
app/models/legacy_diff_discussion.rb
app/models/legacy_diff_discussion.rb
+8
-6
app/models/legacy_diff_note.rb
app/models/legacy_diff_note.rb
+0
-8
app/models/merge_request.rb
app/models/merge_request.rb
+2
-2
app/models/note.rb
app/models/note.rb
+1
-8
No files found.
app/models/concerns/discussion_on_diff.rb
0 → 100644
View file @
2058e71e
module
DiscussionOnDiff
extend
ActiveSupport
::
Concern
included
do
NUMBER_OF_TRUNCATED_DIFF_LINES
=
16
memoized_values
<<
:active
delegate
:line_code
,
:original_line_code
,
:diff_file
,
:diff_line
,
:for_line?
,
:active?
,
to: :first_note
delegate
:file_path
,
:blob
,
:highlighted_diff_lines
,
:diff_lines
,
to: :diff_file
,
allow_nil:
true
end
def
diff_discussion?
true
end
def
active?
return
@active
if
@active
.
present?
@active
=
first_note
.
active?
end
# Returns an array of at most 16 highlighted lines above a diff note
def
truncated_diff_lines
(
highlight:
true
)
lines
=
highlight
?
highlighted_diff_lines
:
diff_lines
prev_lines
=
[]
lines
.
each
do
|
line
|
if
line
.
meta?
prev_lines
.
clear
else
prev_lines
<<
line
break
if
for_line?
(
line
)
prev_lines
.
shift
if
prev_lines
.
length
>=
NUMBER_OF_TRUNCATED_DIFF_LINES
end
end
prev_lines
end
end
app/models/concerns/resolvable_discussion.rb
0 → 100644
View file @
2058e71e
module
ResolvableDiscussion
extend
ActiveSupport
::
Concern
included
do
memoized_values
<<
:resolvable
memoized_values
<<
:resolved
memoized_values
<<
:first_note
memoized_values
<<
:first_note_to_resolve
memoized_values
<<
:last_resolved_note
memoized_values
<<
:last_note
delegate
:resolved_at
,
:resolved_by
,
to: :last_resolved_note
,
allow_nil:
true
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
def
resolved?
return
@resolved
if
@resolved
.
present?
@resolved
=
resolvable?
&&
notes
.
none?
(
&
:to_be_resolved?
)
end
def
first_note
@first_note
||=
notes
.
first
end
def
first_note_to_resolve
return
unless
resolvable?
@first_note_to_resolve
||=
notes
.
find
(
&
:to_be_resolved?
)
end
def
last_resolved_note
return
unless
resolved?
@last_resolved_note
||=
resolved_notes
.
sort_by
(
&
:resolved_at
).
last
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?
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
end
app/models/diff_discussion.rb
View file @
2058e71e
class
DiffDiscussion
<
Discussion
class
DiffDiscussion
<
Discussion
NUMBER_OF_TRUNCATED_DIFF_LINES
=
16
include
DiscussionOnDiff
delegate
:line_code
,
delegate
:position
,
:original_line_code
,
:original_position
,
:diff_file
,
:diff_line
,
:for_line?
,
:active?
,
to: :first_note
to: :first_note
delegate
:file_path
,
:blob
,
:highlighted_diff_lines
,
:diff_lines
,
to: :diff_file
,
allow_nil:
true
def
self
.
build_discussion_id
(
note
)
def
self
.
build_discussion_id
(
note
)
[
*
super
(
note
),
*
unique_position_identifier
(
note
)
]
[
*
super
(
note
),
*
note
.
position
.
key
]
end
end
def
self
.
build_original_discussion_id
(
note
)
def
self
.
build_original_discussion_id
(
note
)
[
*
Discussion
.
build_discussion_id
(
note
),
*
note
.
original_position
.
key
]
[
*
Discussion
.
build_discussion_id
(
note
),
*
note
.
original_position
.
key
]
end
end
def
self
.
unique_position_identifier
(
note
)
note
.
position
.
key
end
def
diff_discussion?
true
end
def
legacy_diff_discussion?
def
legacy_diff_discussion?
false
false
end
end
def
active?
return
@active
if
@active
.
present?
@active
=
first_note
.
active?
end
MEMOIZED_VALUES
<<
:active
def
reply_attributes
def
reply_attributes
super
.
merge
(
first_note
.
diff_attributes
)
super
.
merge
(
end
original_position:
original_position
.
to_json
,
position:
position
.
to_json
,
# Returns an array of at most 16 highlighted lines above a diff note
)
def
truncated_diff_lines
(
highlight:
true
)
lines
=
highlight
?
highlighted_diff_lines
:
diff_lines
prev_lines
=
[]
lines
.
each
do
|
line
|
if
line
.
meta?
prev_lines
.
clear
else
prev_lines
<<
line
break
if
for_line?
(
line
)
prev_lines
.
shift
if
prev_lines
.
length
>=
NUMBER_OF_TRUNCATED_DIFF_LINES
end
end
prev_lines
end
end
end
end
app/models/diff_note.rb
View file @
2058e71e
...
@@ -21,23 +21,12 @@ class DiffNote < Note
...
@@ -21,23 +21,12 @@ class DiffNote < Note
before_validation
:set_discussion_id
,
if: :position_changed?
before_validation
:set_discussion_id
,
if: :position_changed?
after_save
:keep_around_commits
after_save
:keep_around_commits
def
new_diff_note?
true
end
def
discussion_class
(
*
)
def
discussion_class
(
*
)
DiffDiscussion
DiffDiscussion
end
end
def
diff_attributes
%i(original_position position)
.
each
do
|
meth
|
{
define_method
"
#{
meth
}
="
do
|
new_position
|
original_position:
original_position
.
to_json
,
position:
position
.
to_json
,
}
end
%i(original_position= position=)
.
each
do
|
meth
|
define_method
meth
do
|
new_position
|
if
new_position
.
is_a?
(
String
)
if
new_position
.
is_a?
(
String
)
new_position
=
JSON
.
parse
(
new_position
)
rescue
nil
new_position
=
JSON
.
parse
(
new_position
)
rescue
nil
end
end
...
...
app/models/discussion.rb
View file @
2058e71e
class
Discussion
class
Discussion
MEMOIZED_VALUES
=
[]
# rubocop:disable Style/MutableConstant
cattr_accessor
:memoized_values
,
instance_accessor:
false
do
[]
end
include
ResolvableDiscussion
attr_reader
:notes
,
:noteable
attr_reader
:notes
,
:noteable
...
@@ -13,12 +17,6 @@ class Discussion
...
@@ -13,12 +17,6 @@ class Discussion
to: :first_note
to: :first_note
delegate
:resolved_at
,
:resolved_by
,
to: :last_resolved_note
,
allow_nil:
true
def
self
.
build
(
notes
,
noteable
=
nil
)
def
self
.
build
(
notes
,
noteable
=
nil
)
notes
.
first
.
discussion_class
(
noteable
).
new
(
notes
,
noteable
)
notes
.
first
.
discussion_class
(
noteable
).
new
(
notes
,
noteable
)
end
end
...
@@ -98,76 +96,9 @@ class Discussion
...
@@ -98,76 +96,9 @@ class Discussion
notes
.
length
==
1
notes
.
length
==
1
end
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
def
last_note
@last_note
||=
notes
.
last
@last_note
||=
notes
.
last
end
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?
def
collapsed?
resolved?
resolved?
...
@@ -192,7 +123,7 @@ class Discussion
...
@@ -192,7 +123,7 @@ class Discussion
# Set the notes array to the updated notes
# Set the notes array to the updated notes
@notes
=
notes_relation
.
fresh
.
to_a
@notes
=
notes_relation
.
fresh
.
to_a
MEMOIZED_VALUES
.
each
do
|
var
|
self
.
class
.
memoized_values
.
each
do
|
var
|
instance_variable_set
(
:"@
#{
var
}
"
,
nil
)
instance_variable_set
(
:"@
#{
var
}
"
,
nil
)
end
end
end
end
...
...
app/models/legacy_diff_discussion.rb
View file @
2058e71e
class
LegacyDiffDiscussion
<
DiffDiscussion
class
LegacyDiffDiscussion
<
Discussion
def
self
.
unique_position_identifier
(
note
)
include
DiscussionOnDiff
note
.
line_code
end
def
self
.
build_
original_
discussion_id
(
note
)
def
self
.
build_discussion_id
(
note
)
Discussion
.
build_original_discussion_id
(
note
)
[
*
super
(
note
),
note
.
line_code
]
end
end
def
legacy_diff_discussion?
def
legacy_diff_discussion?
...
@@ -19,4 +17,8 @@ class LegacyDiffDiscussion < DiffDiscussion
...
@@ -19,4 +17,8 @@ class LegacyDiffDiscussion < DiffDiscussion
def
collapsed?
def
collapsed?
!
active?
!
active?
end
end
def
reply_attributes
super
.
merge
(
line_code:
line_code
)
end
end
end
app/models/legacy_diff_note.rb
View file @
2058e71e
...
@@ -11,14 +11,6 @@ class LegacyDiffNote < Note
...
@@ -11,14 +11,6 @@ class LegacyDiffNote < Note
LegacyDiffDiscussion
LegacyDiffDiscussion
end
end
def
legacy_diff_note?
true
end
def
diff_attributes
{
line_code:
line_code
}
end
def
project_repository
def
project_repository
if
RequestStore
.
active?
if
RequestStore
.
active?
RequestStore
.
fetch
(
"project:
#{
project_id
}
:repository"
)
{
self
.
project
.
repository
}
RequestStore
.
fetch
(
"project:
#{
project_id
}
:repository"
)
{
self
.
project
.
repository
}
...
...
app/models/merge_request.rb
View file @
2058e71e
...
@@ -846,8 +846,8 @@ class MergeRequest < ActiveRecord::Base
...
@@ -846,8 +846,8 @@ class MergeRequest < ActiveRecord::Base
return
unless
has_complete_diff_refs?
return
unless
has_complete_diff_refs?
return
if
new_diff_refs
==
old_diff_refs
return
if
new_diff_refs
==
old_diff_refs
active_diff_notes
=
self
.
notes
.
diff_notes
.
select
do
|
note
|
active_diff_notes
=
self
.
notes
.
new_
diff_notes
.
select
do
|
note
|
note
.
new_diff_note?
&&
note
.
active?
(
old_diff_refs
)
note
.
active?
(
old_diff_refs
)
end
end
return
if
active_diff_notes
.
empty?
return
if
active_diff_notes
.
empty?
...
...
app/models/note.rb
View file @
2058e71e
...
@@ -75,6 +75,7 @@ class Note < ActiveRecord::Base
...
@@ -75,6 +75,7 @@ class Note < ActiveRecord::Base
end
end
scope
:diff_notes
,
->
{
where
(
type:
%w(LegacyDiffNote DiffNote)
)
}
scope
:diff_notes
,
->
{
where
(
type:
%w(LegacyDiffNote DiffNote)
)
}
scope
:new_diff_notes
,
->
{
where
(
type:
'DiffNote'
)
}
scope
:non_diff_notes
,
->
{
where
(
type:
[
'Note'
,
'DiscussionNote'
,
nil
])
}
scope
:non_diff_notes
,
->
{
where
(
type:
[
'Note'
,
'DiscussionNote'
,
nil
])
}
scope
:with_associations
,
->
do
scope
:with_associations
,
->
do
...
@@ -136,14 +137,6 @@ class Note < ActiveRecord::Base
...
@@ -136,14 +137,6 @@ class Note < ActiveRecord::Base
false
false
end
end
def
legacy_diff_note?
false
end
def
new_diff_note?
false
end
def
active?
def
active?
true
true
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment