querying correctly in tagscontroller to include posts with comments containing hashtag

This commit is contained in:
danielgrippi 2011-07-07 12:21:22 -07:00
parent 310b9969f3
commit 9d506231b1
2 changed files with 25 additions and 4 deletions

View file

@ -56,11 +56,18 @@ class TagsController < ApplicationController
@posts = StatusMessage.where(:public => true, :pending => false)
end
@posts = @posts.tagged_with(params[:name])
@tag = ActsAsTaggableOn::Tag.where(:name => params[:name]).first
if @tag
@posts = @posts.joins("LEFT OUTER JOIN comments ON comments.post_id = posts.id").
joins("INNER JOIN taggings ON (taggings.tag_id = #{@tag.id} AND
((taggable_id = posts.id AND taggable_type = 'Post') OR (taggings.taggable_type = 'Comment' AND taggings.taggable_id = comments.id)))")
max_time = params[:max_time] ? Time.at(params[:max_time].to_i) : Time.now
@posts = @posts.where(StatusMessage.arel_table[:created_at].lt(max_time))
@posts = @posts.includes({:comments => {:author => :profile}}, :photos).order('posts.created_at DESC').limit(15)
max_time = params[:max_time] ? Time.at(params[:max_time].to_i) : Time.now
@posts = @posts.where(StatusMessage.arel_table[:created_at].lt(max_time))
@posts = @posts.includes({:comments => {:author => :profile}}, :photos).order('posts.created_at DESC').limit(15)
else
@posts = []
end
@posts = PostsFake.new(@posts)
@commenting_disabled = true

View file

@ -69,6 +69,20 @@ describe TagsController do
get :show, :name => 'hello'
assigns(:posts).models.should == [stranger_post]
end
it 'displays a post with a comment containing the tag search' do
bob.post(:status_message, :text => "other post y'all", :to => 'all')
other_post = bob.post(:status_message, :text => "sup y'all", :to => 'all')
Factory(:comment, :text => "#hello", :post => other_post)
get :show, :name => 'hello'
assigns(:posts).models.should == [other_post]
response.status.should == 200
end
it 'succeeds without posts' do
get :show, :name => 'hellyes'
response.status.should == 200
end
end
context "not signed in" do