diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb index 40f5e32..3653f4b 100644 --- a/app/controllers/notes_controller.rb +++ b/app/controllers/notes_controller.rb @@ -2,18 +2,19 @@ after_action :verify_authorized respond_to :html - ## - # POST /notes + require "pp" + def create @note = Note.new user_id = params[:new_note][:user_id] @note.user_id = user_id - answer_id = params[:new_note][:answer_id] question_id = params[:new_note][:question_id] plan_id = params[:new_note][:plan_id] + + # create answer if we dont already have one if answer_id.present? - answer = Answer.find(@note.answer_id) + answer = Answer.find(answer_id) else answer = Answer.new answer.plan_id = plan_id @@ -23,49 +24,53 @@ end @note.answer= answer - @note.text = params["#{params[:new_note][:answer_id]}new_note_text"] + @note.text = params["#{question_id}new_note_text"] authorize @note @plan = answer.plan - @phase = answer.question.section.phase + @notice = "Save failed." + @answer = answer + @question = Question.find(question_id) if @note.save - session[:question_id_notes] = answer.question_id - redirect_to edit_plan_phase_path(@plan, @phase), status: :found, notice: _('Comment was successfully created.') + @status = true + @notice = _('Comment was successfully created.') end + notes = answer.notes.all + @num_notes = notes.count end - ## - # PUT /notes/1 + + def update @note = Note.find(params[:note][:id]) authorize @note @note.text = params["#{params[:note][:id]}_note_text"] - @plan = Plan.find(@note.plan_id) - @project = Project.find(@plan.project_id) + @answer = @note.answer + @question = @answer.question + @plan = @answer.plan if @note.update_attributes(params[:note]) - session[:question_id_notes] = @note.question_id - redirect_to edit_project_plan_path(@project, @plan), status: :found, notice: _('Comment was successfully updated.') + @notice = _('Comment was successfully saved.') end end - ## - # ARCHIVE /notes/1 + + def archive @note = Note.find(params[:note][:id]) authorize @note @note.archived = true @note.archived_by = params[:note][:archived_by] - @plan = Plan.find(@note.plan_id) - @project = Project.find(@plan.project_id) + @answer = @note.answer + @question = @answer.question + @plan = @answer.plan if @note.update_attributes(params[:note]) - session[:question_id_notes] = @note.question_id - redirect_to edit_project_plan_path(@project, @plan), status: :found, notice: _('Comment has been removed.') + @notice = _('Comment removed.') end end end diff --git a/app/policies/note_policy.rb b/app/policies/note_policy.rb index 8ffe95a..88a9e11 100644 --- a/app/policies/note_policy.rb +++ b/app/policies/note_policy.rb @@ -13,11 +13,11 @@ end def update? - Plan.find(@note.plan_id).readable_by?(@user.id) + Plan.find(@note.answer.plan_id).readable_by?(@user.id) end def archive? - Plan.find(@note.plan_id).readable_by?(@user.id) + Plan.find(@note.answer.plan_id).readable_by?(@user.id) end end diff --git a/app/views/answers/update.js.erb b/app/views/answers/update.js.erb index 781c176..57e2234 100644 --- a/app/views/answers/update.js.erb +++ b/app/views/answers/update.js.erb @@ -3,11 +3,6 @@ // On success this will be "" on error it will be the // conflicting answer -//$("#answer_notice").html("<%= raw @message %>"); - -<%if @message %> - $("#answer_notice-<%=@question.id%>").html(<%= @message %>) %>"); -<% end %> <% if @old_answer %> $("#answer_notice_<%=@question.id%>").html("<%= escape_javascript(render partial: '/phases/answer', locals: { question: @question, answer: @old_answer}) %>"); diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index e8a3467..6afcffb 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -62,6 +62,8 @@ <%= render "layouts/header" %> + +
<% if notice %>

<%= notice %>

diff --git a/app/views/phases/_add_note.html.erb b/app/views/phases/_add_note.html.erb index 190f89b..cc367af 100644 --- a/app/views/phases/_add_note.html.erb +++ b/app/views/phases/_add_note.html.erb @@ -1,25 +1,28 @@ - <% new_note = Note.new - answerid = answer.id + questionid = question.id %> -<%= form_for :new_note, - :url => {:controller => :notes, :action => :create }, - :html=>{:method=>:post, :id => "new_note_form_#{answerid}", :class => "add_note_form"} do |f| %> - <%= f.hidden_field :user_id, :value => current_user.id %> - <%= f.hidden_field :answer_id, :value => answerid %> - <%= f.hidden_field :question_id, :value => question.id %> - <%= f.hidden_field :plan_id, :value => plan_id %> +<%= form_for( :new_note, + url: notes_path, + remote: true, + method: :post, + id: "new_note_form_#{questionid}", + class: "add_note_form") do |f| %> + <%= f.hidden_field :user_id, value: current_user.id %> + <%= f.hidden_field :question_id, value: questionid %> + <%= f.hidden_field :answer_id, value: answer.id %> + <%= f.hidden_field :plan_id, value: plan_id %> - <%= text_area_tag("#{answerid}new_note_text".to_sym, "" , class: "tinymce") %> + <%= text_area_tag "#{questionid}new_note_text", nil, class: "tinymce" %> + <%= tinymce %>
- <%= f.submit _('Save'), :class => "btn btn-primary new_comment_submit_button" %> + <%= f.submit _('Save'), class: "btn btn-primary new_comment_submit_button" %>

-<%end%> +<% end %> diff --git a/app/views/phases/_answer_form.html.erb b/app/views/phases/_answer_form.html.erb index 90880c5..6ee222a 100644 --- a/app/views/phases/_answer_form.html.erb +++ b/app/views/phases/_answer_form.html.erb @@ -130,9 +130,9 @@
  • <% if comments.count > 0 then%> <% comments_label_with_count = "#{_('Notes')} (#{comments.count})"%> - <%= link_to comments_label_with_count , "#", :class => "comments_accordion_button" %> + <%= link_to comments_label_with_count , "#", id: "notes_number_#{question_id}", class: "comments_accordion_button" %> <%else%> - <%= link_to _('Share note'), "#", :class => "comments_accordion_button" %> + <%= link_to _('Share note'), "#", id: "notes_number_#{question_id}", class: "comments_accordion_button" %> <%end%>
  • <%else%> @@ -142,9 +142,9 @@
  • <% if comments.count > 0 then%> <% comments_label_with_count = "#{_('Notes')} (#{comments.count})"%> -

    <%= comments_label_with_count %>

    +

    <%= comments_label_with_count %>

    <%else%> -

    <%= _('Share note') %>

    +

    <%= _('Share note') %>

    <%end%>
  • <%end%> @@ -202,7 +202,7 @@
    - <%= render :partial => "note", locals: {question: question, answer: answer, plan: plan }%> + <%= render :partial => "note", locals: {question: question, answer: answer, plan: plan, suffix: "" }%>
    diff --git a/app/views/phases/_archive_note.html.erb b/app/views/phases/_archive_note.html.erb index 26e87e3..0e7b244 100644 --- a/app/views/phases/_archive_note.html.erb +++ b/app/views/phases/_archive_note.html.erb @@ -1,23 +1,21 @@ -<%= form_for(Note.new, :url => {:controller => :notes, :action => :archive } , :html => { :method => :put, :class => "archive_note_form", :id => "archive_note_form_#{note["id"]}"}) do |f| %> - <%= f.hidden_field :id, :value => note["id"] %> +<%= form_for(note, + url: archive_note_path(note), + remote: true, + class: "archive_note_form", + id: "archive_note_form_#{note.id}") do |f| %> + + <%= f.hidden_field :id, :value => note.id %> <%= f.hidden_field :archived_by, :value => current_user.id %> - <%= render :partial => "view_note", locals: {note: note} %> + <%= render :partial => "/phases/view_note", locals: {note: note} %> - <% if current_user.id == note["user_id"] then %> - <%= _('helpers.notes.archive_own_note_question')%> - <% button_label = _('helpers.notes.archive_own_comment_button_label') %> - <% else%> - <%= _('helpers.notes.archive_note_question')%> - <% button_label = _('helpers.notes.archive_comment_button_label') %> - <%end%> +

    <%= _('Are you sure you want to remove this note?')%>

    - -
    - <%= hidden_field_tag :note_id, note["id"], :class => "comment_id" %> - <%= f.submit button_label, :class => "btn btn-primary archive_comment_submit_button" %> - <%= link_to _('Cancel'), "#", :class => "cancel_archive_comment btn cancel" %> -
    + +
    + <%= f.submit _('Remove'), onclick: "archive_note(#{note.id}, #{question_id})", :class => "btn btn-primary archive_comment_submit_button" %> + <%= link_to _('Cancel'), "#", onclick: "cancel_archive_note(#{note.id})", :class => "cancel_archive_comment btn cancel" %> +

    -<%end%> +<% end %> diff --git a/app/views/phases/_edit_note.html.erb b/app/views/phases/_edit_note.html.erb index cc482cc..b0a7bdf 100644 --- a/app/views/phases/_edit_note.html.erb +++ b/app/views/phases/_edit_note.html.erb @@ -1,15 +1,19 @@ -<%= form_for(Note.new, :url => {:controller => :notes, :action => :update } , :html => { :method => :put, :class => "edit_note_form", :id=> "edit_note_form_#{note["id"]}"}) do |f| %> - <%= f.hidden_field :id, :value => note["id"] %> +<%= form_for(note, + url: note_path(note), + remote: true, + method: :put, + class: "edit_note_form", + id: "edit_note_form_#{note.id}") do |f| %> - <%= text_area_tag("#{note["id"]}_note_text".to_sym, note["text"] , class: "tinymce") %> + <%= f.hidden_field :id, :value => note.id %> + + <%= text_area_tag("#{note.id}_note_text".to_sym, note.text , class: "tinymce") %>
    - <%= hidden_field_tag :answer, note["answer_id"], :class => "answer_id" %> - <%= hidden_field_tag :note_id, note["id"], :class => "note_id" %> <%= f.submit _('Save'), :class => "btn btn-primary edit_note_submit_button" %>

    diff --git a/app/views/phases/_list_notes.html.erb b/app/views/phases/_list_notes.html.erb index bd2ae70..56a8b96 100644 --- a/app/views/phases/_list_notes.html.erb +++ b/app/views/phases/_list_notes.html.erb @@ -31,18 +31,16 @@ <%else%> - <%= link_to _('View'),"#", :class => "dmp_table_link view_comment_button" %> - <%= hidden_field_tag :note_id, note.id, :class => "comment_id" %> + <%= link_to _('View'),"#question-form-#{question_id}", onclick: "view_note_button(#{note.id}, #{question_id})", :class => "dmp_table_link view_comment_button" %> <% if current_user.id == note.user_id %> - <%= link_to _('Edit'),"#", :class => "dmp_table_link edit_comment_button" %> - <%= hidden_field_tag :note_id, note.id, :class => "comment_id" %> - <%= link_to _('Remove'),"#", :class => "dmp_table_link archive_comment_button" %> + <%= link_to _('Edit'),"#question-form-#{question_id}", onclick: "edit_note(#{note.id}, #{question_id})", :class => "dmp_table_link edit_comment_button" %> + <%= link_to _('Remove'),"#question-form-#{question_id}", onclick: "archive_note(#{note.id}, #{question_id})", :class => "dmp_table_link archive_comment_button" %> <% end%> <% if plan.administerable_by?(current_user.id) && current_user.id != note.user_id %> <%= hidden_field_tag :note_id, note.id, :class => "comment_id" %> - <%= link_to _('Remove'),"#", :class => "dmp_table_link archive_comment_button" %> + <%= link_to _('Remove'),"#question-form-#{question_id}", onclick: "archive_note(#{note.id}, #{question_id})", :class => "dmp_table_link archive_comment_button" %> <% end%> <%end%> @@ -57,30 +55,30 @@ <% notes_not_archived = notes.select { |n| n.archived.nil? } %> -<% latest_note = notes_not_archived.sort { |x,y| y.updated_at <=> x.updated_at }.first %> +<% latest_note = notes_not_archived.sort { |x,y| y.updated_at <=> x.updated_at }.first %> <% if !latest_note.nil? then%>
    - <%= render :partial => "view_note", locals: {note: latest_note} %> + <%= render :partial => "/phases/view_note", locals: {note: latest_note} %>
    <%end%> -<%notes.each do |com|%> +<%notes.each do |note|%> -
    " class ="view_comment_class" style="display: none"> - <%= render :partial => "view_note", locals: {note: com} %> + -
    " class ="edit_comment_class" style="display: none"> - <%= render :partial => "edit_note", locals: {note: com} %> + -
    " class ="archive_comment_class" style="display: none"> - <%= render :partial => "archive_note", locals: {note: com} %> + diff --git a/app/views/phases/_note.html.erb b/app/views/phases/_note.html.erb index ce15d4c..95d38ec 100644 --- a/app/views/phases/_note.html.erb +++ b/app/views/phases/_note.html.erb @@ -2,35 +2,32 @@ else display display add note form --> <% if answer.present? && answer.notes.any? %> - <% notes = answer.notes.all.to_a.sort! {|x,y| y["updated_at"] <=> x["updated_at"] } %> - <% answerid = answer.id %> - <%= hidden_field_tag :answer_id, answer.id %> + <% notes = answer.notes.all.to_a.sort! {|x,y| y.updated_at <=> x.updated_at } %> + <% questionid = question.id %> + <%= hidden_field_tag :question_id, questionid, class: "question_id" %> -
    - <%= link_to _('Add note'),'#', :class => "btn btn-primary add_comment_button" %> +
    + <%= link_to _('Add note'), + "#question-form-#{questionid}", + class: "btn btn-primary add_comment_button", + onclick: "add_note_button(#{questionid})" + %>
    - <%= render :partial => "list_notes", locals: {question_id: question.id, notes: notes, plan: plan} %> + <%= render :partial => "/phases/list_notes", locals: {question_id: question.id, notes: notes, plan: plan} %> -
    - - -
    - <%= link_to _('Add note'),'#', :class => "btn btn-primary add_comment_button" %> -
    -