From 9d506231b1cd09e7352fb7d4ea20ef35c32956ca Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Thu, 7 Jul 2011 12:21:22 -0700 Subject: [PATCH] querying correctly in tagscontroller to include posts with comments containing hashtag --- app/controllers/tags_controller.rb | 15 +++++++++++---- spec/controllers/tags_controller_spec.rb | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 5bf799cfe..9aa9616d9 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -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 diff --git a/spec/controllers/tags_controller_spec.rb b/spec/controllers/tags_controller_spec.rb index 34d892e5f..c412313d0 100644 --- a/spec/controllers/tags_controller_spec.rb +++ b/spec/controllers/tags_controller_spec.rb @@ -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