Commit f7e7dc7ebbdac2a45d528d1f9c19055febd0bede

Authored by Dmitriy Zaporozhets
1 parent 73af33e4

Make note anchors actually work

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
app/assets/javascripts/notes.js
... ... @@ -13,9 +13,6 @@ var NoteList = {
13 13  
14 14 NoteList.setupMainTargetNoteForm();
15 15  
16   - // get initial set of notes
17   - NoteList.getContent();
18   -
19 16 // Unbind events to prevent firing twice
20 17 $(document).off("click", ".js-add-diff-note-button");
21 18 $(document).off("click", ".js-discussion-reply-button");
... ...
app/controllers/projects/commit_controller.rb
... ... @@ -24,7 +24,8 @@ class Projects::CommitController &lt; Projects::ApplicationController
24 24 @line_notes = result[:line_notes]
25 25 @branches = result[:branches]
26 26 @notes_count = result[:notes_count]
27   - @target_type = :commit
  27 + @target_type = "Commit"
  28 + @notes = project.notes.for_commit_id(@commit.id).not_inline.fresh
28 29 @target_id = @commit.id
29 30  
30 31 @comments_allowed = @reply_allowed = true
... ...
app/controllers/projects/issues_controller.rb
... ... @@ -49,7 +49,8 @@ class Projects::IssuesController &lt; Projects::ApplicationController
49 49  
50 50 def show
51 51 @note = @project.notes.new(noteable: @issue)
52   - @target_type = :issue
  52 + @notes = @issue.notes.inc_author.fresh
  53 + @target_type = 'Issue'
53 54 @target_id = @issue.id
54 55  
55 56 respond_with(@issue)
... ...
app/controllers/projects/merge_requests_controller.rb
... ... @@ -198,6 +198,10 @@ class Projects::MergeRequestsController &lt; Projects::ApplicationController
198 198 def define_show_vars
199 199 # Build a note object for comment form
200 200 @note = @project.notes.new(noteable: @merge_request)
  201 + @notes = @merge_request.mr_and_commit_notes.inc_author.fresh
  202 + @discussions = Note.discussions_from_notes(@notes)
  203 + @target_type = 'MergeRequest'
  204 + @target_id = @merge_request.id
201 205  
202 206 # Get commits from repository
203 207 # or from cache if already merged
... ... @@ -205,9 +209,6 @@ class Projects::MergeRequestsController &lt; Projects::ApplicationController
205 209  
206 210 @allowed_to_merge = allowed_to_merge?
207 211 @show_merge_controls = @merge_request.opened? && @commits.any? && @allowed_to_merge
208   -
209   - @target_type = :merge_request
210   - @target_id = @merge_request.id
211 212 end
212 213  
213 214 def allowed_to_merge?
... ...
app/controllers/projects/notes_controller.rb
... ... @@ -11,7 +11,7 @@ class Projects::NotesController &lt; Projects::ApplicationController
11 11 @target_id = params[:target_id]
12 12  
13 13 if params[:target_type] == "merge_request"
14   - @discussions = discussions_from_notes
  14 + @discussions = Note.discussions_from_notes(@notes)
15 15 end
16 16  
17 17 respond_to do |format|
... ... @@ -76,36 +76,4 @@ class Projects::NotesController &lt; Projects::ApplicationController
76 76 def preview
77 77 render text: view_context.markdown(params[:note])
78 78 end
79   -
80   - protected
81   -
82   - def discussion_notes_for(note)
83   - @notes.select do |other_note|
84   - note.discussion_id == other_note.discussion_id
85   - end
86   - end
87   -
88   - def discussions_from_notes
89   - discussion_ids = []
90   - discussions = []
91   -
92   - @notes.each do |note|
93   - next if discussion_ids.include?(note.discussion_id)
94   -
95   - # don't group notes for the main target
96   - if note_for_main_target?(note)
97   - discussions << [note]
98   - else
99   - discussions << discussion_notes_for(note)
100   - discussion_ids << note.discussion_id
101   - end
102   - end
103   -
104   - discussions
105   - end
106   -
107   - # Helps to distinguish e.g. commit notes in mr notes list
108   - def note_for_main_target?(note)
109   - (@target_type.camelize == note.noteable_type && !note.for_diff_line?)
110   - end
111 79 end
... ...
app/controllers/projects/snippets_controller.rb
... ... @@ -48,7 +48,8 @@ class Projects::SnippetsController &lt; Projects::ApplicationController
48 48  
49 49 def show
50 50 @note = @project.notes.new(noteable: @snippet)
51   - @target_type = :snippet
  51 + @notes = @snippet.notes.fresh
  52 + @target_type = 'Snippet'
52 53 @target_id = @snippet.id
53 54 end
54 55  
... ...
app/models/note.rb
... ... @@ -56,29 +56,52 @@ class Note &lt; ActiveRecord::Base
56 56 serialize :st_diff
57 57 before_create :set_diff, if: ->(n) { n.line_code.present? }
58 58  
59   - def self.create_status_change_note(noteable, project, author, status, source)
60   - body = "_Status changed to #{status}#{' by ' + source.gfm_reference if source}_"
61   -
62   - create({
63   - noteable: noteable,
64   - project: project,
65   - author: author,
66   - note: body,
67   - system: true
68   - }, without_protection: true)
69   - end
  59 + class << self
  60 + def create_status_change_note(noteable, project, author, status, source)
  61 + body = "_Status changed to #{status}#{' by ' + source.gfm_reference if source}_"
  62 +
  63 + create({
  64 + noteable: noteable,
  65 + project: project,
  66 + author: author,
  67 + note: body,
  68 + system: true
  69 + }, without_protection: true)
  70 + end
  71 +
  72 + # +noteable+ was referenced from +mentioner+, by including GFM in either +mentioner+'s description or an associated Note.
  73 + # Create a system Note associated with +noteable+ with a GFM back-reference to +mentioner+.
  74 + def create_cross_reference_note(noteable, mentioner, author, project)
  75 + create({
  76 + noteable: noteable,
  77 + commit_id: (noteable.sha if noteable.respond_to? :sha),
  78 + project: project,
  79 + author: author,
  80 + note: "_mentioned in #{mentioner.gfm_reference}_",
  81 + system: true
  82 + }, without_protection: true)
  83 + end
70 84  
71   - # +noteable+ was referenced from +mentioner+, by including GFM in either +mentioner+'s description or an associated Note.
72   - # Create a system Note associated with +noteable+ with a GFM back-reference to +mentioner+.
73   - def self.create_cross_reference_note(noteable, mentioner, author, project)
74   - create({
75   - noteable: noteable,
76   - commit_id: (noteable.sha if noteable.respond_to? :sha),
77   - project: project,
78   - author: author,
79   - note: "_mentioned in #{mentioner.gfm_reference}_",
80   - system: true
81   - }, without_protection: true)
  85 + def discussions_from_notes(notes)
  86 + discussion_ids = []
  87 + discussions = []
  88 +
  89 + notes.each do |note|
  90 + next if discussion_ids.include?(note.discussion_id)
  91 +
  92 + # don't group notes for the main target
  93 + if !note.for_diff_line? && note.noteable_type == "MergeRequest"
  94 + discussions << [note]
  95 + else
  96 + discussions << notes.select do |other_note|
  97 + note.discussion_id == other_note.discussion_id
  98 + end
  99 + discussion_ids << note.discussion_id
  100 + end
  101 + end
  102 +
  103 + discussions
  104 + end
82 105 end
83 106  
84 107 # Determine whether or not a cross-reference note already exists.
... ... @@ -89,7 +112,7 @@ class Note &lt; ActiveRecord::Base
89 112 def commit_author
90 113 @commit_author ||=
91 114 project.users.find_by_email(noteable.author_email) ||
92   - project.users.find_by_name(noteable.author_name)
  115 + project.users.find_by_name(noteable.author_name)
93 116 rescue
94 117 nil
95 118 end
... ...
app/views/projects/notes/_notes.html.haml
... ... @@ -4,7 +4,7 @@
4 4 - if note_for_main_target?(note)
5 5 = render discussion_notes
6 6 - else
7   - = render 'discussion', discussion_notes: discussion_notes
  7 + = render 'projects/notes/discussion', discussion_notes: discussion_notes
8 8 - else
9 9 - @notes.each do |note|
10 10 - next unless note.author
... ...
app/views/projects/notes/_notes_with_form.html.haml
1 1 %ul#notes-list.notes
  2 + = render "projects/notes/notes"
2 3 .js-notes-busy
3 4  
4 5 .js-main-target-form
... ...