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 :public
xml_attr :created_at xml_attr :created_at
has_many :comments, :dependent => :destroy has_many :comments, :order => 'created_at', :dependent => :destroy
has_many :aspect_visibilities has_many :aspect_visibilities
has_many :aspects, :through => :aspect_visibilities has_many :aspects, :through => :aspect_visibilities
@ -31,7 +31,7 @@ class Post < ActiveRecord::Base
belongs_to :o_embed_cache belongs_to :o_embed_cache
belongs_to :author, :class_name => 'Person' belongs_to :author, :class_name => 'Person'
validates :guid, :uniqueness => true validates :guid, :uniqueness => true
after_create :cache_for_author after_create :cache_for_author
@ -111,11 +111,6 @@ class Post < ActiveRecord::Base
I18n.t('notifier.a_post_you_shared') I18n.t('notifier.a_post_you_shared')
end end
# @return [Array<Comment>]
def last_three_comments
self.comments.order('created_at DESC').limit(3).includes(:author => :profile).reverse
end
# @return [Integer] # @return [Integer]
def update_comments_counter def update_comments_counter
self.class.where(:id => self.id). self.class.where(:id => self.id).

View file

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

View file

@ -44,7 +44,7 @@ describe Post do
end end
describe 'includes for a stream' do 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)' it 'should include photos and root of reshares(but does not)'
end end
@ -106,15 +106,33 @@ describe Post do
end end
end end
describe '#last_three_comments' do describe '#comments' do
it 'returns the last three comments of a post' do it 'returns the comments of a post in created_at order' do
post = bob.post :status_message, :text => "hello", :to => 'all' post = bob.post :status_message, :text => "hello", :to => 'all'
created_at = Time.now - 100 created_at = Time.now - 100
comments = [alice, eve, bob, alice].map do |u|
created_at = created_at + 10 # Posts are created out of time order.
u.comment("hey", :post => post, :created_at => created_at) # i.e. id order is not created_at order
end alice.comment 'comment a', :post => post, :created_at => created_at + 10
post.last_three_comments.map{|c| c.id}.should == comments[1,3].map{|c| c.id} 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
end end
@ -199,7 +217,7 @@ describe Post do
@post.should_cache_for_author?.should be_false @post.should_cache_for_author?.should be_false
end end
end end
describe "#receive" do describe "#receive" do
it 'returns false if the post does not verify' do it 'returns false if the post does not verify' do
@post = Factory(:status_message, :author => bob.person) @post = Factory(:status_message, :author => bob.person)
@ -225,7 +243,7 @@ describe Post do
@known_post.should_receive(:update_attributes) @known_post.should_receive(:update_attributes)
@post.send(:receive_persisted, bob, eve.person, @known_post).should == true @post.send(:receive_persisted, bob, eve.person, @known_post).should == true
end end
it 'returns false if trying to update a non-mutable object' do it 'returns false if trying to update a non-mutable object' do
@known_post.stub(:mutable?).and_return(false) @known_post.stub(:mutable?).and_return(false)
@known_post.should_not_receive(:update_attributes) @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 it "receives the post from the contact of the author" do
@post.send(:receive_persisted, bob, eve.person, @known_post).should == true @post.send(:receive_persisted, bob, eve.person, @known_post).should == true
end end
it 'notifies the user if they are mentioned' do it 'notifies the user if they are mentioned' do
bob.stub(:contact_for).with(eve.person).and_return(stub(:receive_post => true)) bob.stub(:contact_for).with(eve.person).and_return(stub(:receive_post => true))
bob.should_receive(:notify_if_mentioned).and_return(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)) bob.should_receive(:contact_for).with(eve.person).and_return(stub(:receive_post => true))
@post.send(:receive_non_persisted, bob, eve.person).should == true @post.send(:receive_non_persisted, bob, eve.person).should == true
end end
it 'notifies the user if they are mentioned' do it 'notifies the user if they are mentioned' do
bob.stub(:contact_for).with(eve.person).and_return(stub(:receive_post => true)) bob.stub(:contact_for).with(eve.person).and_return(stub(:receive_post => true))
bob.should_receive(:notify_if_mentioned).and_return(true) bob.should_receive(:notify_if_mentioned).and_return(true)