Skip to content

Commit

Permalink
Fixed : incompatibility with postgresql in the find_recent method
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrodelgallego committed Dec 4, 2009
1 parent 1f92a17 commit f76d925
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
9 changes: 5 additions & 4 deletions app/models/comment_activity.rb
Expand Up @@ -16,10 +16,11 @@ def most_recent_comment
class << self
def find_recent
Post.find(:all,
:select => 'distinct posts.*',
:joins => 'INNER JOIN comments ON comments.post_id = posts.id',
:order => 'comments.created_at DESC',
:limit => 5
:group => "comments.post_id, posts." + Post.column_names.join(", posts."),
:select => 'posts.*, max(comments.created_at), comments.post_id',
:joins => 'INNER JOIN comments ON comments.post_id = posts.id',
:order => 'max(comments.created_at) desc',
:limit => 5
).collect {|post|
CommentActivity.new(post)
}.sort_by {|activity|
Expand Down
37 changes: 37 additions & 0 deletions spec/models/comment_activity_spec.rb
@@ -1,6 +1,43 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe CommentActivity, '#comments' do

def valid_comment_attributes(extra = {})
{
:author => 'Don Alias',
:author_url => "me",
:author_email => "me@fake.com",
:body => 'This is a comment',
:post => Post.create!(:title => 'My Post', :body => "body", :tag_list => "ruby")
}.merge(extra)
end

context "find recent comments" do
before :each do
@comments = []
(1..10).each do |n|
@comments << Comment.create(valid_comment_attributes(:created_at => Time.now - n * (60 * 60 * 24)))
end
end

it "should have the comment activity sorted by when they were created" do
comment_activity = CommentActivity.find_recent
comment_activity.first.post.should == @comments.first.post
end

it do
comment_activity = CommentActivity.find_recent
comment_activity.should have_exactly(5).posts
end

it "should not return repeated posts" do
comment = Comment.create! valid_comment_attributes(:post => Post.first, :created_at => Time.now)
comment_activity = CommentActivity.find_recent
comment_activity.select{|a| a.post == comment.post}.size.should == 1
end

end

it 'finds the 5 most recent approved comments for the post' do
ret = [mock_model(Comment)]
comments = []
Expand Down

0 comments on commit f76d925

Please sign in to comment.