Merge pull request #2148 from Pistos/issue-2007-comment-order-on-postgresql

Explicitly specify order of comments, since we cannot assume MySQL orderi
This commit is contained in:
Daniel Grippi 2011-10-12 16:09:18 -07:00
commit b7f4f81d28
3 changed files with 33 additions and 20 deletions

View file

@ -16,7 +16,7 @@ class Post < ActiveRecord::Base
xml_attr :public
xml_attr :created_at
has_many :comments, :dependent => :destroy
has_many :comments, :order => 'created_at', :dependent => :destroy
has_many :aspect_visibilities
has_many :aspects, :through => :aspect_visibilities
@ -111,11 +111,6 @@ class Post < ActiveRecord::Base
I18n.t('notifier.a_post_you_shared')
end
# @return [Array<Comment>]
def last_three_comments
self.comments.order('created_at DESC').limit(3).includes(:author => :profile).reverse
end
# @return [Integer]
def update_comments_counter
self.class.where(:id => self.id).

View file

@ -9,7 +9,7 @@
%ul.comments{:class => ('loaded' if post.comments.size <= 3)}
-if post.comments.size > 3 && !comments_expanded
= render :partial => 'comments/comment', :collection => post.last_three_comments, :locals => {:post => post}
= render :partial => 'comments/comment', :collection => post.comments.last(3), :locals => {:post => post}
-else
= render :partial => 'comments/comment', :collection => post.comments, :locals => {:post => post}

View file

@ -106,15 +106,33 @@ describe Post do
end
end
describe '#last_three_comments' do
it 'returns the last three comments of a post' do
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
comments = [alice, eve, bob, alice].map do |u|
created_at = created_at + 10
u.comment("hey", :post => post, :created_at => created_at)
end
post.last_three_comments.map{|c| c.id}.should == comments[1,3].map{|c| c.id}
# 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