From fb309a12b9449256be5d120116ff09f45a443ea6 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Tue, 13 Nov 2018 16:57:42 +0100 Subject: [PATCH 1/3] Refactor result and step comments initialization [SCI-2830] --- app/assets/javascripts/comments.js.erb | 30 +++++++++++-------- app/controllers/result_comments_controller.rb | 15 ++++++---- app/controllers/step_comments_controller.rb | 13 ++++---- app/views/comments/_edit.html.erb | 2 +- app/views/my_modules/_result.html.erb | 2 ++ app/views/result_comments/_comment.html.erb | 3 +- app/views/result_comments/_index.html.erb | 16 ++++++---- app/views/step_comments/_comment.html.erb | 3 +- app/views/step_comments/_index.html.erb | 24 +++++++++------ app/views/steps/_step.html.erb | 2 ++ 10 files changed, 68 insertions(+), 42 deletions(-) diff --git a/app/assets/javascripts/comments.js.erb b/app/assets/javascripts/comments.js.erb index bfdc5a599..b1a694f26 100644 --- a/app/assets/javascripts/comments.js.erb +++ b/app/assets/javascripts/comments.js.erb @@ -16,18 +16,21 @@ var Comments = (function() { $.each(comments, function(){ var that = $(this); var link = that.attr('data-href'); - $.ajax({ method: 'GET', - url: link, - beforeSend: animateSpinner(that, true) }) - .done(function(data) { - that.html(data.html); - initCommentForm(that); - initCommentsLink(that); - scrollBottom(that.find('.content-comments')); - }) - .always(function() { - animateSpinner(that, false); - }); + if(that.html().length === 0) { + $.ajax({ method: 'GET', + url: link, + beforeSend: animateSpinner(that, true) }) + .done(function(data) { + that.html(data.html); + + }) + .always(function() { + animateSpinner(that, false); + }); + } + initCommentForm(that); + initCommentsLink(that); + scrollBottom(that.find('.content-comments')); }); } } @@ -238,7 +241,8 @@ var Comments = (function() { } function initDeleteComments(parent) { - $(parent).on('click', '[data-action=delete-comment]', function() { + $(parent).on('click', '[data-action=delete-comment]', function(ev) { + ev.preventDefault(); var $this = $(this); if (confirm($this.attr('data-confirm-message'))) { $.ajax({ diff --git a/app/controllers/result_comments_controller.rb b/app/controllers/result_comments_controller.rb index 6d4c48544..f96febdfc 100644 --- a/app/controllers/result_comments_controller.rb +++ b/app/controllers/result_comments_controller.rb @@ -18,9 +18,9 @@ class ResultCommentsController < ApplicationController # messages. 'list' partial is used for showing more # comments. partial = 'index.html.erb' - partial = 'list.html.erb' if @last_comment_id > 0 + partial = 'list.html.erb' if @last_comment_id.positive? more_url = '' - if @comments.count > 0 + if @comments.size.positive? more_url = url_for(result_result_comments_path(@result, format: :json, from: @comments @@ -28,11 +28,14 @@ class ResultCommentsController < ApplicationController end render json: { perPage: @per_page, - resultsNumber: @comments.length, + resultsNumber: @comments.size, moreUrl: more_url, - html: render_to_string(partial: partial, - locals: { comments: @comments, - more_comments_url: more_url }) + html: render_to_string( + partial: partial, + locals: { + result: @result, comments: @comments, per_page: @per_page + } + ) } end end diff --git a/app/controllers/step_comments_controller.rb b/app/controllers/step_comments_controller.rb index 713592122..825479b6a 100644 --- a/app/controllers/step_comments_controller.rb +++ b/app/controllers/step_comments_controller.rb @@ -19,20 +19,21 @@ class StepCommentsController < ApplicationController # messages. 'list' partial is used for showing more # comments. partial = 'index.html.erb' - partial = 'list.html.erb' if @last_comment_id > 0 + partial = 'list.html.erb' if @last_comment_id.positive? more_url = '' - if @comments.count > 0 + if @comments.size.positive? more_url = url_for(step_step_comments_path(@step, format: :json, from: @comments.first.id)) end render json: { perPage: @per_page, - resultsNumber: @comments.length, + resultsNumber: @comments.size, moreUrl: more_url, - html: render_to_string(partial: partial, - locals: { comments: @comments, - more_comments_url: more_url }) + html: render_to_string( + partial: partial, + locals: { step: @step, comments: @comments, per_page: @per_page } + ) } end end diff --git a/app/views/comments/_edit.html.erb b/app/views/comments/_edit.html.erb index 3ff6603cf..b756f358e 100644 --- a/app/views/comments/_edit.html.erb +++ b/app/views/comments/_edit.html.erb @@ -14,6 +14,6 @@ - <%= t('general.cancel') %> + <%= t('general.cancel') %> <% end %> diff --git a/app/views/my_modules/_result.html.erb b/app/views/my_modules/_result.html.erb index 66a9c2b3f..40fb1268e 100644 --- a/app/views/my_modules/_result.html.erb +++ b/app/views/my_modules/_result.html.erb @@ -51,6 +51,8 @@
+ <%= render partial: "result_comments/index.html.erb", + locals: { result: result, comments: result.last_comments, per_page: Constants::COMMENTS_SEARCH_LIMIT } %>
<% end %> diff --git a/app/views/result_comments/_comment.html.erb b/app/views/result_comments/_comment.html.erb index 14512fc93..5dd51f498 100644 --- a/app/views/result_comments/_comment.html.erb +++ b/app/views/result_comments/_comment.html.erb @@ -1,3 +1,4 @@ +
<%=t "my_modules.results.comment_title", user: comment.user.full_name, time: l(comment.created_at, format: :time) %> @@ -28,7 +29,7 @@ data-action="delete-comment" data-url="<%= result_result_comment_path(comment.result, comment, format: :json) %>" data-confirm-message="<%= t('comments.delete_confirm') %>" - data-no-turbolink="true"> + data-turbolinks="false"> <%= t('comments.options_dropdown.delete') %> diff --git a/app/views/result_comments/_index.html.erb b/app/views/result_comments/_index.html.erb index 1c3d30497..0b1528f56 100644 --- a/app/views/result_comments/_index.html.erb +++ b/app/views/result_comments/_index.html.erb @@ -4,24 +4,30 @@

<% if can_create_comments_in_module?(@my_module) then %>