Commit 48c682b52f61d96ef14ca9168b5f2678385dd626
1 parent
923bd2ec
Exists in
spb-stable
and in
3 other branches
Switch Notes controller to use json
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing
4 changed files
with
62 additions
and
61 deletions
Show diff stats
app/controllers/projects/notes_controller.rb
| ... | ... | @@ -2,71 +2,54 @@ class Projects::NotesController < Projects::ApplicationController |
| 2 | 2 | # Authorize |
| 3 | 3 | before_filter :authorize_read_note! |
| 4 | 4 | before_filter :authorize_write_note!, only: [:create] |
| 5 | - | |
| 6 | - respond_to :js | |
| 5 | + before_filter :authorize_admin_note!, only: [:update, :destroy] | |
| 7 | 6 | |
| 8 | 7 | def index |
| 9 | 8 | @notes = Notes::LoadContext.new(project, current_user, params).execute |
| 10 | - @target_type = params[:target_type].camelize | |
| 11 | - @target_id = params[:target_id] | |
| 12 | 9 | |
| 13 | - if params[:target_type] == "merge_request" | |
| 14 | - @discussions = Note.discussions_from_notes(@notes) | |
| 15 | - end | |
| 10 | + notes_json = { notes: [] } | |
| 16 | 11 | |
| 17 | - respond_to do |format| | |
| 18 | - format.html { redirect_to :back } | |
| 19 | - format.json do | |
| 20 | - render json: { | |
| 21 | - html: view_to_html_string("projects/notes/_notes") | |
| 22 | - } | |
| 23 | - end | |
| 12 | + @notes.each do |note| | |
| 13 | + notes_json[:notes] << { | |
| 14 | + id: note.id, | |
| 15 | + html: note_to_html(note) | |
| 16 | + } | |
| 24 | 17 | end |
| 18 | + | |
| 19 | + render json: notes_json | |
| 25 | 20 | end |
| 26 | 21 | |
| 27 | 22 | def create |
| 28 | 23 | @note = Notes::CreateContext.new(project, current_user, params).execute |
| 29 | - @target_type = params[:target_type].camelize | |
| 30 | - @target_id = params[:target_id] | |
| 31 | 24 | |
| 32 | 25 | respond_to do |format| |
| 33 | - format.html {redirect_to :back} | |
| 34 | - format.js | |
| 26 | + format.json { render_note_json(@note) } | |
| 27 | + format.html { redirect_to :back } | |
| 35 | 28 | end |
| 36 | 29 | end |
| 37 | 30 | |
| 38 | - def destroy | |
| 39 | - @note = @project.notes.find(params[:id]) | |
| 40 | - return access_denied! unless can?(current_user, :admin_note, @note) | |
| 41 | - @note.destroy | |
| 42 | - @note.reset_events_cache | |
| 31 | + def update | |
| 32 | + note.update_attributes(params[:note]) | |
| 33 | + note.reset_events_cache | |
| 43 | 34 | |
| 44 | 35 | respond_to do |format| |
| 45 | - format.js { render nothing: true } | |
| 36 | + format.json { render_note_json(note) } | |
| 37 | + format.html { redirect_to :back } | |
| 46 | 38 | end |
| 47 | 39 | end |
| 48 | 40 | |
| 49 | - def update | |
| 50 | - @note = @project.notes.find(params[:id]) | |
| 51 | - return access_denied! unless can?(current_user, :admin_note, @note) | |
| 52 | - | |
| 53 | - @note.update_attributes(params[:note]) | |
| 54 | - @note.reset_events_cache | |
| 41 | + def destroy | |
| 42 | + note.destroy | |
| 43 | + note.reset_events_cache | |
| 55 | 44 | |
| 56 | 45 | respond_to do |format| |
| 57 | - format.js do | |
| 58 | - render js: { success: @note.valid?, id: @note.id, note: view_context.markdown(@note.note) }.to_json | |
| 59 | - end | |
| 60 | - format.html do | |
| 61 | - redirect_to :back | |
| 62 | - end | |
| 46 | + format.js { render nothing: true } | |
| 63 | 47 | end |
| 64 | 48 | end |
| 65 | 49 | |
| 66 | 50 | def delete_attachment |
| 67 | - @note = @project.notes.find(params[:id]) | |
| 68 | - @note.remove_attachment! | |
| 69 | - @note.update_attribute(:attachment, nil) | |
| 51 | + note.remove_attachment! | |
| 52 | + note.update_attribute(:attachment, nil) | |
| 70 | 53 | |
| 71 | 54 | respond_to do |format| |
| 72 | 55 | format.js { render nothing: true } |
| ... | ... | @@ -76,4 +59,41 @@ class Projects::NotesController < Projects::ApplicationController |
| 76 | 59 | def preview |
| 77 | 60 | render text: view_context.markdown(params[:note]) |
| 78 | 61 | end |
| 62 | + | |
| 63 | + private | |
| 64 | + | |
| 65 | + def note | |
| 66 | + @note ||= @project.notes.find(params[:id]) | |
| 67 | + end | |
| 68 | + | |
| 69 | + def note_to_html(note) | |
| 70 | + render_to_string( | |
| 71 | + "projects/notes/_note", | |
| 72 | + layout: false, | |
| 73 | + formats: [:html], | |
| 74 | + locals: { note: note } | |
| 75 | + ) | |
| 76 | + end | |
| 77 | + | |
| 78 | + def note_to_discussion_html(note) | |
| 79 | + render_to_string( | |
| 80 | + "projects/notes/_diff_notes_with_reply", | |
| 81 | + layout: false, | |
| 82 | + formats: [:html], | |
| 83 | + locals: { notes: [note] } | |
| 84 | + ) | |
| 85 | + end | |
| 86 | + | |
| 87 | + def render_note_json(note) | |
| 88 | + render json: { | |
| 89 | + id: note.id, | |
| 90 | + discussion_id: note.discussion_id, | |
| 91 | + html: note_to_html(note), | |
| 92 | + discussion_html: note_to_discussion_html(note) | |
| 93 | + } | |
| 94 | + end | |
| 95 | + | |
| 96 | + def authorize_admin_note! | |
| 97 | + return access_denied! unless can?(current_user, :admin_note, note) | |
| 98 | + end | |
| 79 | 99 | end | ... | ... |
app/views/projects/notes/_form.html.haml
| 1 | -= form_for [@project, @note], remote: true, html: { multipart: true, id: nil, class: "new_note js-new-note-form common-note-form" }, authenticity_token: true do |f| | |
| 2 | - | |
| 1 | += form_for [@project, @note], remote: true, html: { :'data-type' => 'json', multipart: true, id: nil, class: "new_note js-new-note-form common-note-form" }, authenticity_token: true do |f| | |
| 3 | 2 | = note_target_fields |
| 4 | 3 | = f.hidden_field :commit_id |
| 5 | 4 | = f.hidden_field :line_code | ... | ... |
app/views/projects/notes/_notes_with_form.html.haml
| 1 | -%ul#notes-list.notes | |
| 1 | +%ul#notes-list.notes.main-notes-list | |
| 2 | 2 | = render "projects/notes/notes" |
| 3 | 3 | .js-notes-busy |
| 4 | 4 | |
| ... | ... | @@ -7,4 +7,4 @@ |
| 7 | 7 | = render "projects/notes/form" |
| 8 | 8 | |
| 9 | 9 | :javascript |
| 10 | - NoteList.init("#{@target_id}", "#{@target_type}", "#{project_notes_path(@project)}"); | |
| 10 | + new Notes("#{project_notes_path(target_id: @noteable.id, target_type: @noteable.class.name.underscore)}", #{@notes.map(&:id).to_json}) | ... | ... |
app/views/projects/notes/create.js.haml
| ... | ... | @@ -1,18 +0,0 @@ |
| 1 | -- if @note.valid? | |
| 2 | - var noteHtml = "#{escape_javascript(render @note)}"; | |
| 3 | - | |
| 4 | - - if note_for_main_target?(@note) | |
| 5 | - NoteList.appendNewNote(#{@note.id}, noteHtml); | |
| 6 | - - else | |
| 7 | - :plain | |
| 8 | - var firstDiscussionNoteHtml = "#{escape_javascript(render "projects/notes/diff_notes_with_reply", notes: [@note])}"; | |
| 9 | - NoteList.appendNewDiscussionNote("#{@note.discussion_id}", | |
| 10 | - firstDiscussionNoteHtml, | |
| 11 | - noteHtml); | |
| 12 | - | |
| 13 | -- else | |
| 14 | - var errorsHtml = "#{escape_javascript(render 'projects/notes/form_errors', note: @note)}"; | |
| 15 | - - if note_for_main_target?(@note) | |
| 16 | - NoteList.errorsOnForm(errorsHtml); | |
| 17 | - - else | |
| 18 | - NoteList.errorsOnForm(errorsHtml, "#{@note.discussion_id}"); |