mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-09-10 15:14:33 +08:00
Add edit/delete result comment functionality
Lessons learned during this fix: Don't use dependant: :destroy on both ends of association between 2 ActiveRecords, or you will run into stack overflow.
This commit is contained in:
parent
bac0455435
commit
24dd25d9f5
7 changed files with 127 additions and 73 deletions
|
@ -1,42 +1,4 @@
|
||||||
function results_comment_edit(id) {
|
//= require comments
|
||||||
document.getElementById('edit_comment_'+id).type='text';
|
|
||||||
$('#span_comment_'+id).hide();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function results_update_comment(id) {
|
|
||||||
if (document.getElementById('edit_comment_'+id).type=='text') {
|
|
||||||
var txt = document.getElementById('edit_comment_'+id).value;
|
|
||||||
$.ajax({
|
|
||||||
type: "POST",
|
|
||||||
url: '/projects/update_comment_results',
|
|
||||||
dataType: 'json',
|
|
||||||
data: {id: id, msg: txt},
|
|
||||||
success: function (data) {
|
|
||||||
document.getElementById('edit_comment_'+id).type='hidden';
|
|
||||||
var txt = document.getElementById('edit_comment_'+id).value;
|
|
||||||
$('#span_comment_'+id).text(txt);
|
|
||||||
$('#span_comment_'+id).show();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function results_comment_delete(id) {
|
|
||||||
if (confirm('Are you sure you want to delete this comment?')) {
|
|
||||||
$.ajax({
|
|
||||||
type: "POST",
|
|
||||||
url: '/projects/delete_comment_results',
|
|
||||||
dataType: 'json',
|
|
||||||
data: {id: id},
|
|
||||||
success: function (data) {
|
|
||||||
$('.content-comments').find('#'+id).remove();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function initHandsOnTables(root) {
|
function initHandsOnTables(root) {
|
||||||
root.find("div.hot-table").each(function() {
|
root.find("div.hot-table").each(function() {
|
||||||
|
@ -81,15 +43,13 @@ function initResultCommentForm($el) {
|
||||||
.on("ajax:success", function (e, data) {
|
.on("ajax:success", function (e, data) {
|
||||||
if (data.html) {
|
if (data.html) {
|
||||||
var list = $form.parents("ul");
|
var list = $form.parents("ul");
|
||||||
var s1 = data.html
|
|
||||||
var id = s1.substring(s1.lastIndexOf("delete(")+7,s1.lastIndexOf(")'"))
|
|
||||||
|
|
||||||
// Remove potential "no comments" element
|
// Remove potential "no comments" element
|
||||||
list.parent().find(".content-comments")
|
list.parent().find(".content-comments")
|
||||||
.find("li.no-comments").remove();
|
.find("li.no-comments").remove();
|
||||||
|
|
||||||
list.parent().find(".content-comments")
|
list.parent().find(".content-comments")
|
||||||
.prepend("<li class='comment' id='"+id+"'>" + data.html + "</li>")
|
.prepend("<li class='comment'>" + data.html + "</li>")
|
||||||
.scrollTop(0);
|
.scrollTop(0);
|
||||||
list.parents("ul").find("> li.comment:gt(8)").remove();
|
list.parents("ul").find("> li.comment:gt(8)").remove();
|
||||||
$("#comment_message", $form).val("");
|
$("#comment_message", $form).val("");
|
||||||
|
@ -98,6 +58,9 @@ function initResultCommentForm($el) {
|
||||||
$(".help-block", $form)
|
$(".help-block", $form)
|
||||||
.html("")
|
.html("")
|
||||||
.addClass("hide");
|
.addClass("hide");
|
||||||
|
scrollCommentOptions(
|
||||||
|
list.parent().find(".content-comments .dropdown-comment")
|
||||||
|
);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.on("ajax:error", function (ev, xhr) {
|
.on("ajax:error", function (ev, xhr) {
|
||||||
|
@ -131,10 +94,13 @@ function initResultCommentsLink($el) {
|
||||||
var listItem = moreBtn.parents('li');
|
var listItem = moreBtn.parents('li');
|
||||||
$(data.html).insertBefore(listItem);
|
$(data.html).insertBefore(listItem);
|
||||||
if (data.results_number < data.per_page) {
|
if (data.results_number < data.per_page) {
|
||||||
moreBtn.remove();
|
moreBtn.remove();
|
||||||
} else {
|
} else {
|
||||||
moreBtn.attr("href", data.more_url);
|
moreBtn.attr("href", data.more_url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reposition dropdown comment options
|
||||||
|
scrollCommentOptions(listItem.closest(".content-comments").find(".dropdown-comment"));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -228,6 +194,10 @@ expandAllResults();
|
||||||
initTutorial();
|
initTutorial();
|
||||||
applyCollapseLinkCallBack();
|
applyCollapseLinkCallBack();
|
||||||
|
|
||||||
|
initCommentOptions("ul.content-comments");
|
||||||
|
initEditComments(".panel .tab-content");
|
||||||
|
initDeleteComments(".panel .tab-content");
|
||||||
|
|
||||||
$(function () {
|
$(function () {
|
||||||
$("#results-collapse-btn").click(function () {
|
$("#results-collapse-btn").click(function () {
|
||||||
$('.result .panel-collapse').collapse('hide');
|
$('.result .panel-collapse').collapse('hide');
|
||||||
|
|
|
@ -3,6 +3,8 @@ class ResultCommentsController < ApplicationController
|
||||||
|
|
||||||
before_action :check_view_permissions, only: [ :index ]
|
before_action :check_view_permissions, only: [ :index ]
|
||||||
before_action :check_add_permissions, only: [ :new, :create ]
|
before_action :check_add_permissions, only: [ :new, :create ]
|
||||||
|
before_action :check_edit_permissions, only: [:edit, :update]
|
||||||
|
before_action :check_destroy_permissions, only: [:destroy]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@comments = @result.last_comments(@last_comment_id, @per_page)
|
@comments = @result.last_comments(@last_comment_id, @per_page)
|
||||||
|
@ -54,7 +56,7 @@ class ResultCommentsController < ApplicationController
|
||||||
Activity.create(
|
Activity.create(
|
||||||
type_of: :add_comment_to_result,
|
type_of: :add_comment_to_result,
|
||||||
user: current_user,
|
user: current_user,
|
||||||
project: @result.my_module.project,
|
project: @result.my_module.experiment.project,
|
||||||
my_module: @result.my_module,
|
my_module: @result.my_module,
|
||||||
message: t(
|
message: t(
|
||||||
"activities.add_comment_to_result",
|
"activities.add_comment_to_result",
|
||||||
|
@ -91,6 +93,47 @@ class ResultCommentsController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def edit
|
||||||
|
@update_url =
|
||||||
|
result_result_comment_path(@result, @comment, format: :json)
|
||||||
|
respond_to do |format|
|
||||||
|
format.json do
|
||||||
|
render json: {
|
||||||
|
html: render_to_string(
|
||||||
|
partial: '/comments/edit.html.erb'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
@comment.message = comment_params[:message]
|
||||||
|
respond_to do |format|
|
||||||
|
format.json do
|
||||||
|
if @comment.save
|
||||||
|
render json: {}, status: :ok
|
||||||
|
else
|
||||||
|
render json: { errors: @comment.errors.to_hash(true) },
|
||||||
|
status: :unprocessable_entity
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
respond_to do |format|
|
||||||
|
format.json do
|
||||||
|
if @comment.destroy
|
||||||
|
render json: {}, status: :ok
|
||||||
|
else
|
||||||
|
render json: { message: I18n.t('comments.delete_error') },
|
||||||
|
status: :unprocessable_entity
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def load_vars
|
def load_vars
|
||||||
|
@ -116,6 +159,19 @@ class ResultCommentsController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def check_edit_permissions
|
||||||
|
@comment = Comment.find_by_id(params[:id])
|
||||||
|
render_403 unless @comment.present? &&
|
||||||
|
can_edit_result_comment_in_module(@comment)
|
||||||
|
end
|
||||||
|
|
||||||
|
def check_destroy_permissions
|
||||||
|
@comment = Comment.find_by_id(params[:id])
|
||||||
|
render_403 unless @comment.present? &&
|
||||||
|
can_delete_result_comment_in_module(@comment)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
def comment_params
|
def comment_params
|
||||||
params.require(:comment).permit(:message)
|
params.require(:comment).permit(:message)
|
||||||
end
|
end
|
||||||
|
|
|
@ -931,14 +931,13 @@ module PermissionHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
def can_edit_step_comment_in_protocol(comment)
|
def can_edit_step_comment_in_protocol(comment)
|
||||||
|
return false if comment.step_comment.blank?
|
||||||
|
|
||||||
protocol = comment.step_comment.step.protocol
|
protocol = comment.step_comment.step.protocol
|
||||||
if protocol.in_module?
|
if protocol.in_module?
|
||||||
comment.step_comment.present? &&
|
comment.user == current_user ||
|
||||||
(
|
is_owner_of_project(
|
||||||
comment.user == current_user ||
|
protocol.my_module.experiment.project
|
||||||
is_owner_of_project(
|
|
||||||
protocol.my_module.experiment.project
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
false
|
false
|
||||||
|
@ -946,14 +945,13 @@ module PermissionHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
def can_delete_step_comment_in_protocol(comment)
|
def can_delete_step_comment_in_protocol(comment)
|
||||||
|
return false if comment.step_comment.blank?
|
||||||
|
|
||||||
protocol = comment.step_comment.step.protocol
|
protocol = comment.step_comment.step.protocol
|
||||||
if protocol.in_module?
|
if protocol.in_module?
|
||||||
comment.step_comment.present? &&
|
comment.user == current_user ||
|
||||||
(
|
is_owner_of_project(
|
||||||
comment.user == current_user ||
|
protocol.my_module.experiment.project
|
||||||
is_owner_of_project(
|
|
||||||
protocol.my_module.experiment.project
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
false
|
false
|
||||||
|
|
|
@ -4,6 +4,5 @@ class ResultComment < ActiveRecord::Base
|
||||||
|
|
||||||
belongs_to :result, inverse_of: :result_comments
|
belongs_to :result, inverse_of: :result_comments
|
||||||
belongs_to :comment,
|
belongs_to :comment,
|
||||||
inverse_of: :result_comment,
|
inverse_of: :result_comment
|
||||||
dependent: :destroy
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,12 +1,40 @@
|
||||||
<strong><%=t "my_modules.results.comment_title", user: comment.user.full_name, time: l(comment.created_at, format: :time) %></strong>
|
<div>
|
||||||
<span class="input-group">
|
<strong>
|
||||||
<input type="hidden" class="form-control" id="edit_comment_<%= comment.id %>" value="<%= comment.message %>" onkeydown="if (event.keyCode==13) results_update_comment(<%= comment.id %>)" >
|
<%=t "my_modules.results.comment_title", user: comment.user.full_name, time: l(comment.created_at, format: :time) %>
|
||||||
<span id="span_comment_<%= comment.id %>"><%= comment.message %></span>
|
</strong>
|
||||||
<div class="input-group-btn">
|
<% if can_edit_result_comment_in_module(comment) || can_delete_result_comment_in_module(comment) %>
|
||||||
<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown"><span class="caret"></span></button>
|
<div class="dropdown dropdown-comment">
|
||||||
<ul class="dropdown-menu pull-right">
|
<a href="#"
|
||||||
<li><a href="#" onclick='return results_comment_edit(<%= comment.id %>)'>Edit</a></li>
|
class="dropdown-toggle"
|
||||||
<li><a href="#" onclick='return results_comment_delete(<%= comment.id %>)'>Delete</a></li>
|
data-role="comment-options"
|
||||||
|
type="button"
|
||||||
|
id="comment-<%= comment.id %>-dropdown"
|
||||||
|
data-toggle="dropdown"
|
||||||
|
aria-haspopup="true"
|
||||||
|
aria-expanded="true">
|
||||||
|
<span class="caret"></span>
|
||||||
|
</a>
|
||||||
|
<ul class="dropdown-menu dropdown-menu-fixed" aria-labelledby="comment-<%= comment.id %>-dropdown">
|
||||||
|
<li class="dropdown-header"><%= I18n.t('comments.options_dropdown.header') %></li>
|
||||||
|
<li>
|
||||||
|
<a href="#"
|
||||||
|
data-action="edit-comment"
|
||||||
|
data-url="<%= edit_result_result_comment_path(comment.result_comment.result, comment, format: :json) %>">
|
||||||
|
<%= t('comments.options_dropdown.edit') %>
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="#"
|
||||||
|
data-action="delete-comment"
|
||||||
|
data-url="<%= result_result_comment_path(comment.result_comment.result, comment, format: :json) %>"
|
||||||
|
data-confirm-message="<%= t('comments.delete_confirm') %>">
|
||||||
|
<%= t('comments.options_dropdown.delete') %>
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</span>
|
<% end %>
|
||||||
|
</div>
|
||||||
|
<div data-role="comment-message-container">
|
||||||
|
<p data-role="comment-message"><%= comment.message %></p>
|
||||||
|
</div>
|
|
@ -7,7 +7,7 @@
|
||||||
<%= render 'result_comments/list.html.erb', comments: @comments %>
|
<%= render 'result_comments/list.html.erb', comments: @comments %>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% if @comments.length == @per_page %>
|
<% if @comments.length == @per_page %>
|
||||||
<li class="text-center">
|
<li class="comment-more text-center">
|
||||||
<a class="btn btn-default btn-more-comments" href="<%= more_comments_url %>" data-remote="true">
|
<a class="btn btn-default btn-more-comments" href="<%= more_comments_url %>" data-remote="true">
|
||||||
<%=t "general.more_comments" %>
|
<%=t "general.more_comments" %>
|
||||||
</a>
|
</a>
|
||||||
|
|
|
@ -2,11 +2,14 @@
|
||||||
<% current_day = DateTime.current.strftime('%j').to_i %>
|
<% current_day = DateTime.current.strftime('%j').to_i %>
|
||||||
|
|
||||||
<% comments.each do |comment| %>
|
<% comments.each do |comment| %>
|
||||||
<li class="comment" id="<%= comment.id %>" >
|
|
||||||
<% comment_day = comment.created_at.strftime('%j').to_i %>
|
<% comment_day = comment.created_at.strftime('%j').to_i %>
|
||||||
<% if comment_day < current_day and comment_day < day %>
|
<% if comment_day < current_day and comment_day < day %>
|
||||||
<% day = comment.created_at.strftime('%j').to_i %>
|
<% day = comment.created_at.strftime('%j').to_i %>
|
||||||
<p class="text-center"><%= comment.created_at.strftime('%d.%m.%Y') %></p>
|
<li class="comment-date-separator">
|
||||||
|
<p class="text-center"><%= comment.created_at.strftime('%d.%m.%Y') %></p>
|
||||||
|
</li>
|
||||||
<% end %>
|
<% end %>
|
||||||
<%= render 'result_comments/comment.html.erb', comment: comment %></li>
|
<li class="comment">
|
||||||
|
<%= render 'result_comments/comment.html.erb', comment: comment %>
|
||||||
|
</li>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
Loading…
Add table
Reference in a new issue