Commit 48c682b5 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Switch Notes controller to use json

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 923bd2ec
...@@ -2,71 +2,54 @@ class Projects::NotesController < Projects::ApplicationController ...@@ -2,71 +2,54 @@ class Projects::NotesController < Projects::ApplicationController
# Authorize # Authorize
before_filter :authorize_read_note! before_filter :authorize_read_note!
before_filter :authorize_write_note!, only: [:create] before_filter :authorize_write_note!, only: [:create]
before_filter :authorize_admin_note!, only: [:update, :destroy]
respond_to :js
def index def index
@notes = Notes::LoadContext.new(project, current_user, params).execute @notes = Notes::LoadContext.new(project, current_user, params).execute
@target_type = params[:target_type].camelize
@target_id = params[:target_id]
if params[:target_type] == "merge_request" notes_json = { notes: [] }
@discussions = Note.discussions_from_notes(@notes)
end
respond_to do |format| @notes.each do |note|
format.html { redirect_to :back } notes_json[:notes] << {
format.json do id: note.id,
render json: { html: note_to_html(note)
html: view_to_html_string("projects/notes/_notes")
} }
end end
end
render json: notes_json
end end
def create def create
@note = Notes::CreateContext.new(project, current_user, params).execute @note = Notes::CreateContext.new(project, current_user, params).execute
@target_type = params[:target_type].camelize
@target_id = params[:target_id]
respond_to do |format| respond_to do |format|
format.html {redirect_to :back} format.json { render_note_json(@note) }
format.js format.html { redirect_to :back }
end end
end end
def destroy def update
@note = @project.notes.find(params[:id]) note.update_attributes(params[:note])
return access_denied! unless can?(current_user, :admin_note, @note) note.reset_events_cache
@note.destroy
@note.reset_events_cache
respond_to do |format| respond_to do |format|
format.js { render nothing: true } format.json { render_note_json(note) }
format.html { redirect_to :back }
end end
end end
def update def destroy
@note = @project.notes.find(params[:id]) note.destroy
return access_denied! unless can?(current_user, :admin_note, @note) note.reset_events_cache
@note.update_attributes(params[:note])
@note.reset_events_cache
respond_to do |format| respond_to do |format|
format.js do format.js { render nothing: true }
render js: { success: @note.valid?, id: @note.id, note: view_context.markdown(@note.note) }.to_json
end
format.html do
redirect_to :back
end
end end
end end
def delete_attachment def delete_attachment
@note = @project.notes.find(params[:id]) note.remove_attachment!
@note.remove_attachment! note.update_attribute(:attachment, nil)
@note.update_attribute(:attachment, nil)
respond_to do |format| respond_to do |format|
format.js { render nothing: true } format.js { render nothing: true }
...@@ -76,4 +59,41 @@ class Projects::NotesController < Projects::ApplicationController ...@@ -76,4 +59,41 @@ class Projects::NotesController < Projects::ApplicationController
def preview def preview
render text: view_context.markdown(params[:note]) render text: view_context.markdown(params[:note])
end end
private
def note
@note ||= @project.notes.find(params[:id])
end
def note_to_html(note)
render_to_string(
"projects/notes/_note",
layout: false,
formats: [:html],
locals: { note: note }
)
end
def note_to_discussion_html(note)
render_to_string(
"projects/notes/_diff_notes_with_reply",
layout: false,
formats: [:html],
locals: { notes: [note] }
)
end
def render_note_json(note)
render json: {
id: note.id,
discussion_id: note.discussion_id,
html: note_to_html(note),
discussion_html: note_to_discussion_html(note)
}
end
def authorize_admin_note!
return access_denied! unless can?(current_user, :admin_note, note)
end
end end
= 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| = 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|
= note_target_fields = note_target_fields
= f.hidden_field :commit_id = f.hidden_field :commit_id
= f.hidden_field :line_code = f.hidden_field :line_code
......
%ul#notes-list.notes %ul#notes-list.notes.main-notes-list
= render "projects/notes/notes" = render "projects/notes/notes"
.js-notes-busy .js-notes-busy
...@@ -7,4 +7,4 @@ ...@@ -7,4 +7,4 @@
= render "projects/notes/form" = render "projects/notes/form"
:javascript :javascript
NoteList.init("#{@target_id}", "#{@target_type}", "#{project_notes_path(@project)}"); new Notes("#{project_notes_path(target_id: @noteable.id, target_type: @noteable.class.name.underscore)}", #{@notes.map(&:id).to_json})
- if @note.valid?
var noteHtml = "#{escape_javascript(render @note)}";
- if note_for_main_target?(@note)
NoteList.appendNewNote(#{@note.id}, noteHtml);
- else
:plain
var firstDiscussionNoteHtml = "#{escape_javascript(render "projects/notes/diff_notes_with_reply", notes: [@note])}";
NoteList.appendNewDiscussionNote("#{@note.discussion_id}",
firstDiscussionNoteHtml,
noteHtml);
- else
var errorsHtml = "#{escape_javascript(render 'projects/notes/form_errors', note: @note)}";
- if note_for_main_target?(@note)
NoteList.errorsOnForm(errorsHtml);
- else
NoteList.errorsOnForm(errorsHtml, "#{@note.discussion_id}");
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment