diff --git a/app/assets/javascripts/step_comments.js b/app/assets/javascripts/step_comments.js index 90974db89..a3f5cf240 100644 --- a/app/assets/javascripts/step_comments.js +++ b/app/assets/javascripts/step_comments.js @@ -64,7 +64,7 @@ var list = $(this).parents("ul"); var moreBtn = list.find(".btn-more-comments"); var listItem = moreBtn.parents('li'); - $(data.html).insertBefore(listItem); + $(data.html).insertAfter(listItem); if (data.results_number < data.per_page) { moreBtn.remove(); } else { @@ -111,6 +111,10 @@ // }); // } + /** + * Initializes the steps comments + * + */ function initializeComments(){ var steps = $(".step-comment"); $.each(steps, function(){ @@ -164,6 +168,10 @@ }); } + /** + * Listen for ajax POST request and trigger the + * refreshComment when the comment is successfully posted + */ function bindCommentAjax(id){ $(id) .on('ajax:success', function() { @@ -181,7 +189,7 @@ }); } - + // Updates the comments when new comment is created function updateCommentHTML(parent, data) { var id; if ( $(parent.find(".comment-form")).attr("id") !== undefined ) { diff --git a/app/controllers/step_comments_controller.rb b/app/controllers/step_comments_controller.rb index fad1c454d..db22d4753 100644 --- a/app/controllers/step_comments_controller.rb +++ b/app/controllers/step_comments_controller.rb @@ -20,7 +20,7 @@ class StepCommentsController < ApplicationController if @comments.count > 0 more_url = url_for(step_step_comments_path(@step, format: :json, - from: @comments.last.id)) + from: @comments.first.id)) end render :json => { per_page: @per_page, diff --git a/app/models/step.rb b/app/models/step.rb index 2b59b1907..28393d221 100644 --- a/app/models/step.rb +++ b/app/models/step.rb @@ -88,12 +88,13 @@ class Step < ActiveRecord::Base end def last_comments(last_id = 1, per_page = 20) - last_id = 9999999999999 if last_id <= 1 - Comment.joins(:step_comment) - .where(step_comments: { step_id: id }) - .where('comments.id < ?', last_id) - .order(created_at: :asc) - .limit(per_page) + last_id = 9999999999 if last_id <= 1 + comments = Comment.joins(:step_comment) + .where(step_comments: { step_id: id }) + .where('comments.id < ?', last_id) + .order(created_at: :desc) + .limit(per_page) + comments.reverse end def save(current_user=nil)