diff options
author | Douwe Maan <douwe@gitlab.com> | 2016-07-15 19:21:02 +0000 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2016-07-15 19:21:02 +0000 |
commit | 3b9e58953f5e846280d7f14a87a9816c24c1cc8d (patch) | |
tree | 78ccdc3e7343c8a3ef375f57ecdf27a2e3a32ce5 | |
parent | 263f005e905088dc7b84276f0a14be567fd158b0 (diff) | |
parent | efe18f0507e08c7fd0f90a99e0c241c3c4ce107a (diff) | |
download | gitlab-ce-3b9e58953f5e846280d7f14a87a9816c24c1cc8d.tar.gz |
Merge branch 'fix-mentioned-users-on-diff-notes' into 'master'
Fix mentioned users on diff notes
## Summary
`DiffNote`, and `LegacyDiffNote` returns empty array for `mentionable_attrs`, because `mentionable_attrs` is not inheritable by subclasses. The problem can be illustrated with this small sample:
```ruby
module Mentionable
extend ActiveSupport::Concern
module ClassMethods
def attr_mentionable(attr)
mentionable_attrs << [attr.to_s]
end
def mentionable_attrs
@mentionable_attrs ||= []
end
end
end
class A
include Mentionable
attr_mentionable :foo
end
class B < A
end
A.mentionable_attrs
=> [["foo", {}]]
B.mentionable_attrs
=> []
```
Possible solution using `cattr_accessor`:
```ruby
module Mentionable
extend ActiveSupport::Concern
module ClassMethods
def attr_mentionable(attr)
mentionable_attrs << [attr.to_s]
end
end
included do
cattr_accessor :mentionable_attrs, instance_accessor: false do
[]
end
end
end
class A
include Mentionable
attr_mentionable :foo
end
class B < A
end
A.mentionable_attrs
=> [["foo"]]
B.mentionable_attrs
=> [["foo"]]
B.mentionable_attrs < [:bar]
=> [["foo"], ["bar"]]
A.mentionable_attrs
=> [["foo"], ["bar"]]
```
`mentionable_attrs` is inheritable by subclasses. If a subclass changes the value then that would also change the value for parent class. Similarly if parent class changes the value then that would change the value of subclasses too.
## What are the relevant issue numbers?
Fixes #19807
Fixes #18022
/cc @stanhu @DouweM @rspeicher
See merge request !5243
-rw-r--r-- | CHANGELOG | 1 | ||||
-rw-r--r-- | app/models/concerns/mentionable.rb | 8 | ||||
-rw-r--r-- | app/models/concerns/participable.rb | 7 | ||||
-rw-r--r-- | spec/services/todo_service_spec.rb | 18 |
4 files changed, 28 insertions, 6 deletions
diff --git a/CHANGELOG b/CHANGELOG index 457943c6b4b..1c31a254eb7 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -57,6 +57,7 @@ v 8.10.0 (unreleased) - Throttle the update of `project.pushes_since_gc` to 1 minute. - Allow expanding and collapsing files in diff view (!4990) - Collapse large diffs by default (!4990) + - Fix mentioned users list on diff notes - Check for conflicts with existing Project's wiki path when creating a new project. - Show last push widget in upstream after push to fork - Cache todos pending/done dashboard query counts. diff --git a/app/models/concerns/mentionable.rb b/app/models/concerns/mentionable.rb index 8cac47246db..ec9e0f1b1d0 100644 --- a/app/models/concerns/mentionable.rb +++ b/app/models/concerns/mentionable.rb @@ -14,14 +14,14 @@ module Mentionable attr = attr.to_s mentionable_attrs << [attr, options] end + end + included do # Accessor for attributes marked mentionable. - def mentionable_attrs - @mentionable_attrs ||= [] + cattr_accessor :mentionable_attrs, instance_accessor: false do + [] end - end - included do if self < Participable participant -> (user, ext) { all_references(user, extractor: ext) } end diff --git a/app/models/concerns/participable.rb b/app/models/concerns/participable.rb index 9822844357d..70740c76e43 100644 --- a/app/models/concerns/participable.rb +++ b/app/models/concerns/participable.rb @@ -41,9 +41,12 @@ module Participable def participant(attr) participant_attrs << attr end + end - def participant_attrs - @participant_attrs ||= [] + included do + # Accessor for participant attributes. + cattr_accessor :participant_attrs, instance_accessor: false do + [] end end diff --git a/spec/services/todo_service_spec.rb b/spec/services/todo_service_spec.rb index b4522536724..83c18a35472 100644 --- a/spec/services/todo_service_spec.rb +++ b/spec/services/todo_service_spec.rb @@ -435,6 +435,24 @@ describe TodoService, services: true do should_create_todo(user: author, target: mr_unassigned, action: Todo::MARKED) end end + + describe '#new_note' do + let(:mention) { john_doe.to_reference } + let(:diff_note_on_merge_request) { create(:diff_note_on_merge_request, project: project, noteable: mr_unassigned, author: author, note: "Hey #{mention}") } + let(:legacy_diff_note_on_merge_request) { create(:legacy_diff_note_on_merge_request, project: project, noteable: mr_unassigned, author: author, note: "Hey #{mention}") } + + it 'creates a todo for mentioned user on new diff note' do + service.new_note(diff_note_on_merge_request, author) + + should_create_todo(user: john_doe, target: mr_unassigned, author: author, action: Todo::MENTIONED, note: diff_note_on_merge_request) + end + + it 'creates a todo for mentioned user on legacy diff note' do + service.new_note(legacy_diff_note_on_merge_request, author) + + should_create_todo(user: john_doe, target: mr_unassigned, author: author, action: Todo::MENTIONED, note: legacy_diff_note_on_merge_request) + end + end end it 'updates cached counts when a todo is created' do |