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
@ -31,7 +31,7 @@ class Post < ActiveRecord::Base
belongs_to :o_embed_cache
belongs_to :author, :class_name => 'Person'
validates :guid, :uniqueness => true
after_create :cache_for_author
@ -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

@ -44,7 +44,7 @@ describe Post do
end
describe 'includes for a stream' do
it 'inclues author profile and mentions'
it 'inclues author profile and mentions'
it 'should include photos and root of reshares(but does not)'
end
@ -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
@ -199,7 +217,7 @@ describe Post do
@post.should_cache_for_author?.should be_false
end
end
describe "#receive" do
it 'returns false if the post does not verify' do
@post = Factory(:status_message, :author => bob.person)
@ -225,7 +243,7 @@ describe Post do
@known_post.should_receive(:update_attributes)
@post.send(:receive_persisted, bob, eve.person, @known_post).should == true
end
it 'returns false if trying to update a non-mutable object' do
@known_post.stub(:mutable?).and_return(false)
@known_post.should_not_receive(:update_attributes)
@ -242,7 +260,7 @@ describe Post do
it "receives the post from the contact of the author" do
@post.send(:receive_persisted, bob, eve.person, @known_post).should == true
end
it 'notifies the user if they are mentioned' do
bob.stub(:contact_for).with(eve.person).and_return(stub(:receive_post => true))
bob.should_receive(:notify_if_mentioned).and_return(true)
@ -264,7 +282,7 @@ describe Post do
bob.should_receive(:contact_for).with(eve.person).and_return(stub(:receive_post => true))
@post.send(:receive_non_persisted, bob, eve.person).should == true
end
it 'notifies the user if they are mentioned' do
bob.stub(:contact_for).with(eve.person).and_return(stub(:receive_post => true))
bob.should_receive(:notify_if_mentioned).and_return(true)