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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
6114aedf
Commit
6114aedf
authored
Jul 14, 2015
by
Dmitriy Zaporozhets
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of gitlab.com:gitlab-org/gitlab-ce
parents
ee53ba9f
90e47dd9
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
81 additions
and
18 deletions
+81
-18
app/models/concerns/mentionable.rb
app/models/concerns/mentionable.rb
+28
-14
app/models/note.rb
app/models/note.rb
+1
-1
app/services/issues/update_service.rb
app/services/issues/update_service.rb
+1
-1
app/services/merge_requests/update_service.rb
app/services/merge_requests/update_service.rb
+1
-1
spec/models/concerns/mentionable_spec.rb
spec/models/concerns/mentionable_spec.rb
+49
-0
spec/support/mentionable_shared_examples.rb
spec/support/mentionable_shared_examples.rb
+1
-1
No files found.
app/models/concerns/mentionable.rb
View file @
6114aedf
...
@@ -79,22 +79,36 @@ module Mentionable
...
@@ -79,22 +79,36 @@ module Mentionable
end
end
end
end
# If the mentionable_text field is about to change, locate any *added* references and create cross references for
# When a mentionable field is changed, creates cross-reference notes that
# them. Invoke from an observer's #before_save implementation.
# don't already exist
def
notice_added_references
(
p
=
project
,
a
=
author
)
def
create_new_cross_references!
(
p
=
project
,
a
=
author
)
ch
=
changed_attributes
changes
=
detect_mentionable_changes
original
,
mentionable_changed
=
""
,
false
self
.
class
.
mentionable_attrs
.
each
do
|
attr
|
return
if
changes
.
empty?
if
ch
[
attr
]
original
<<
ch
[
attr
]
mentionable_changed
=
true
end
end
# Only proceed if the saved changes actually include a chance to an attr_mentionable field.
original_text
=
changes
.
collect
{
|
_
,
vals
|
vals
.
first
}.
join
(
' '
)
return
unless
mentionable_changed
preexisting
=
references
(
p
,
self
.
author
,
original
)
preexisting
=
references
(
p
,
self
.
author
,
original
_text
)
create_cross_references!
(
p
,
a
,
preexisting
)
create_cross_references!
(
p
,
a
,
preexisting
)
end
end
private
# Returns a Hash of changed mentionable fields
#
# Preference is given to the `changes` Hash, but falls back to
# `previous_changes` if it's empty (i.e., the changes have already been
# persisted).
#
# See ActiveModel::Dirty.
#
# Returns a Hash.
def
detect_mentionable_changes
source
=
(
changes
.
present?
?
changes
:
previous_changes
).
dup
mentionable
=
self
.
class
.
mentionable_attrs
# Only include changed fields that are mentionable
source
.
select
{
|
key
,
val
|
mentionable
.
include?
(
key
)
}
end
end
end
app/models/note.rb
View file @
6114aedf
...
@@ -356,7 +356,7 @@ class Note < ActiveRecord::Base
...
@@ -356,7 +356,7 @@ class Note < ActiveRecord::Base
end
end
def
set_references
def
set_references
notice_added_references
(
project
,
author
)
create_new_cross_references!
(
project
,
author
)
end
end
def
editable?
def
editable?
...
...
app/services/issues/update_service.rb
View file @
6114aedf
...
@@ -35,7 +35,7 @@ module Issues
...
@@ -35,7 +35,7 @@ module Issues
create_title_change_note
(
issue
,
issue
.
previous_changes
[
'title'
].
first
)
create_title_change_note
(
issue
,
issue
.
previous_changes
[
'title'
].
first
)
end
end
issue
.
notice_added_references
(
issue
.
project
,
current_user
)
issue
.
create_new_cross_references!
(
issue
.
project
,
current_user
)
execute_hooks
(
issue
,
'update'
)
execute_hooks
(
issue
,
'update'
)
end
end
...
...
app/services/merge_requests/update_service.rb
View file @
6114aedf
...
@@ -59,7 +59,7 @@ module MergeRequests
...
@@ -59,7 +59,7 @@ module MergeRequests
merge_request
.
mark_as_unchecked
merge_request
.
mark_as_unchecked
end
end
merge_request
.
notice_added_references
(
merge_request
.
project
,
current_user
)
merge_request
.
create_new_cross_references!
(
merge_request
.
project
,
current_user
)
execute_hooks
(
merge_request
,
'update'
)
execute_hooks
(
merge_request
,
'update'
)
end
end
...
...
spec/models/concerns/mentionable_spec.rb
View file @
6114aedf
...
@@ -28,4 +28,53 @@ describe Issue, "Mentionable" do
...
@@ -28,4 +28,53 @@ describe Issue, "Mentionable" do
issue
.
create_cross_references!
(
project
,
author
,
[
commit2
])
issue
.
create_cross_references!
(
project
,
author
,
[
commit2
])
end
end
end
end
describe
'#create_new_cross_references!'
do
let
(
:project
)
{
create
(
:project
)
}
let
(
:issues
)
{
create_list
(
:issue
,
2
,
project:
project
)
}
context
'before changes are persisted'
do
it
'ignores pre-existing references'
do
issue
=
create_issue
(
description:
issues
[
0
].
to_reference
)
expect
(
SystemNoteService
).
not_to
receive
(
:cross_reference
)
issue
.
description
=
'New description'
issue
.
create_new_cross_references!
end
it
'notifies new references'
do
issue
=
create_issue
(
description:
issues
[
0
].
to_reference
)
expect
(
SystemNoteService
).
to
receive
(
:cross_reference
).
with
(
issues
[
1
],
any_args
)
issue
.
description
=
issues
[
1
].
to_reference
issue
.
create_new_cross_references!
end
end
context
'after changes are persisted'
do
it
'ignores pre-existing references'
do
issue
=
create_issue
(
description:
issues
[
0
].
to_reference
)
expect
(
SystemNoteService
).
not_to
receive
(
:cross_reference
)
issue
.
update_attributes
(
description:
'New description'
)
issue
.
create_new_cross_references!
end
it
'notifies new references'
do
issue
=
create_issue
(
description:
issues
[
0
].
to_reference
)
expect
(
SystemNoteService
).
to
receive
(
:cross_reference
).
with
(
issues
[
1
],
any_args
)
issue
.
update_attributes
(
description:
issues
[
1
].
to_reference
)
issue
.
create_new_cross_references!
end
end
def
create_issue
(
description
:)
create
(
:issue
,
project:
project
,
description:
description
)
end
end
end
end
spec/support/mentionable_shared_examples.rb
View file @
6114aedf
...
@@ -143,6 +143,6 @@ shared_examples 'an editable mentionable' do
...
@@ -143,6 +143,6 @@ shared_examples 'an editable mentionable' do
end
end
set_mentionable_text
.
call
(
new_text
)
set_mentionable_text
.
call
(
new_text
)
subject
.
notice_added_references
(
project
,
author
)
subject
.
create_new_cross_references!
(
project
,
author
)
end
end
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