collapse Post#last_three_comments and Commentable#last_three_comments, do not double query comments in CommentsController#index for a JSON response, remove spec that tests ordering as it's not deterministic on psql without specified order
This commit is contained in:
parent
72aee6b2c1
commit
9c67cd7eb9
4 changed files with 7 additions and 42 deletions
|
|
@ -71,7 +71,7 @@ class CommentsController < ApplicationController
|
|||
if @post
|
||||
@comments = @post.comments.includes(:author => :profile).order('created_at ASC')
|
||||
respond_with do |format|
|
||||
format.json { render :json => @post.comments.as_api_response(:backbone), :status => 200 }
|
||||
format.json { render :json => @comments.as_api_response(:backbone), :status => 200 }
|
||||
format.mobile{render :layout => false}
|
||||
end
|
||||
else
|
||||
|
|
|
|||
|
|
@ -64,14 +64,6 @@ class Post < ActiveRecord::Base
|
|||
def raw_message; ""; end
|
||||
def mentioned_people; []; end
|
||||
|
||||
# gives the last three comments on the post
|
||||
def last_three_comments
|
||||
return if self.comments_count == 0
|
||||
# DO NOT USE .last(3) HERE. IT WILL FETCH ALL COMMENTS AND RETURN THE LAST THREE
|
||||
# INSTEAD OF DOING THE FOLLOWING, AS EXPECTED (THX AR):
|
||||
self.comments.order('created_at DESC').limit(3).includes(:author => :profile).reverse!
|
||||
end
|
||||
|
||||
def self.excluding_blocks(user)
|
||||
people = user.blocks.map{|b| b.person_id}
|
||||
scope = scoped
|
||||
|
|
|
|||
|
|
@ -11,9 +11,12 @@ module Diaspora
|
|||
end
|
||||
|
||||
# @return [Array<Comment>]
|
||||
def last_three_comments
|
||||
self.comments.order('created_at DESC').limit(3).includes(:author => :profile).reverse
|
||||
end
|
||||
def last_three_comments
|
||||
return if self.comments_count == 0
|
||||
# DO NOT USE .last(3) HERE. IT WILL FETCH ALL COMMENTS AND RETURN THE LAST THREE
|
||||
# INSTEAD OF DOING THE FOLLOWING, AS EXPECTED (THX AR):
|
||||
self.comments.order('created_at DESC').limit(3).includes(:author => :profile).reverse
|
||||
end
|
||||
|
||||
# @return [Integer]
|
||||
def update_comments_counter
|
||||
|
|
|
|||
|
|
@ -215,36 +215,6 @@ describe Post do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#comments' do
|
||||
it 'returns the comments of a post in created_at order' do
|
||||
post = bob.post :status_message, :text => "hello", :to => 'all'
|
||||
created_at = Time.now - 100
|
||||
|
||||
# Posts are created out of time order.
|
||||
# i.e. id order is not created_at order
|
||||
alice.comment 'comment a', :post => post, :created_at => created_at + 10
|
||||
eve.comment 'comment d', :post => post, :created_at => created_at + 50
|
||||
bob.comment 'comment b', :post => post, :created_at => created_at + 30
|
||||
alice.comment 'comment e', :post => post, :created_at => created_at + 90
|
||||
eve.comment 'comment c', :post => post, :created_at => created_at + 40
|
||||
|
||||
post.comments.map(&:text).should == [
|
||||
'comment a',
|
||||
'comment b',
|
||||
'comment c',
|
||||
'comment d',
|
||||
'comment e',
|
||||
]
|
||||
post.comments.map(&:author).should == [
|
||||
alice.person,
|
||||
bob.person,
|
||||
eve.person,
|
||||
eve.person,
|
||||
alice.person,
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
describe 'Likeable#update_likes_counter' do
|
||||
before do
|
||||
@post = bob.post :status_message, :text => "hello", :to => 'all'
|
||||
|
|
|
|||
Loading…
Reference in a new issue