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/app/models/comment.rb b/app/models/comment.rb index f0d829dd4..fd416b370 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -13,6 +13,11 @@ class Comment < ActiveRecord::Base include Diaspora::Relayable include Diaspora::Socketable + include Diaspora::Taggable + + acts_as_taggable_on :tags + extract_tags_from :text + before_create :build_tags xml_attr :text xml_attr :diaspora_handle diff --git a/app/models/status_message.rb b/app/models/status_message.rb index a5aed41b3..d037ec7d7 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -43,7 +43,7 @@ class StatusMessage < Post escaped_message = opts[:plain_text] ? self.raw_message: ERB::Util.h(self.raw_message) mentioned_message = self.format_mentions(escaped_message, opts) - self.format_tags(mentioned_message, opts) + Diaspora::Taggable.format_tags(mentioned_message, opts) end def format_mentions(text, opts = {}) diff --git a/app/views/comments/_comment.html.haml b/app/views/comments/_comment.html.haml index 0809a407b..19e54da98 100644 --- a/app/views/comments/_comment.html.haml +++ b/app/views/comments/_comment.html.haml @@ -12,7 +12,7 @@ = person_link(comment.author, :class => "hovercardable") %span{:class => direction_for(comment.text)} - = markdownify(comment.text, :youtube_maps => comment.youtube_titles) + = markdownify(Diaspora::Taggable.format_tags(comment.text), :youtube_maps => comment.youtube_titles) %br %time.timeago{:datetime => comment.created_at} diff --git a/app/views/people/_person.html.haml b/app/views/people/_person.html.haml index b01b479df..fb8e759ae 100644 --- a/app/views/people/_person.html.haml +++ b/app/views/people/_person.html.haml @@ -15,5 +15,5 @@ =person_link(person) .info - = person.profile.format_tags(person.profile.tag_string) + = Diaspora::Taggable.format_tags(person.profile.tag_string) diff --git a/app/views/people/contacts.haml b/app/views/people/contacts.haml index 0c704398d..d3a8b47b2 100644 --- a/app/views/people/contacts.haml +++ b/app/views/people/contacts.haml @@ -47,7 +47,7 @@ = @person.diaspora_handle .description - if !@person.profile.tag_string.blank? - = @person.profile.format_tags(@person.profile.tag_string) + = Diapsora::Taggable.format_tags(@person.profile.tag_string) - if user_signed_in? && @person == current_user.person %span.hover_edit = link_to t('people.show.edit'), edit_profile_path diff --git a/app/views/people/show.html.haml b/app/views/people/show.html.haml index f163c814d..32e429da4 100644 --- a/app/views/people/show.html.haml +++ b/app/views/people/show.html.haml @@ -38,7 +38,7 @@ .description - if !@person.profile.tag_string.blank? && user_signed_in? && (@contact.persisted? || @person == current_user.person || @incoming_request) - = @person.profile.format_tags(@person.profile.tag_string) + = Diaspora::Taggable.format_tags(@person.profile.tag_string) - if user_signed_in? && @person == current_user.person %span.hover_edit = link_to t('.edit'), edit_profile_path diff --git a/lib/diaspora/taggable.rb b/lib/diaspora/taggable.rb index 2eff35442..7c3ab66bd 100644 --- a/lib/diaspora/taggable.rb +++ b/lib/diaspora/taggable.rb @@ -36,7 +36,7 @@ module Diaspora unique_matches.values end - def format_tags(text, opts={}) + def self.format_tags(text, opts={}) return text if opts[:plain_text] regex = /(^|\s)#(#{VALID_TAG_BODY})/ form_message = text.gsub(regex) do |matched_string| 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 diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index 74f6d7871..f815a2c64 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -126,4 +126,11 @@ describe Comment do it_should_behave_like 'it is relayable' end + describe 'tags' do + before do + @object = Factory.build(:comment) + end + it_should_behave_like 'it is taggable' + end + end diff --git a/spec/shared_behaviors/taggable.rb b/spec/shared_behaviors/taggable.rb index 0b1bd95f0..cb47fe038 100644 --- a/spec/shared_behaviors/taggable.rb +++ b/spec/shared_behaviors/taggable.rb @@ -12,7 +12,7 @@ describe Diaspora::Taggable do def controller end - describe '#format_tags' do + describe '.format_tags' do before do @str = '#what #hey #vöglein' @object.send(@object.class.field_with_tags_setter, @str) @@ -21,10 +21,10 @@ describe Diaspora::Taggable do end it 'links the tag to /p' do link = link_to('#vöglein', '/tags/vöglein', :class => 'tag') - @object.format_tags(@str).should include(link) + Diaspora::Taggable.format_tags(@str).should include(link) end it 'responds to plain_text' do - @object.format_tags(@str, :plain_text => true).should == @str + Diaspora::Taggable.format_tags(@str, :plain_text => true).should == @str end end describe '#build_tags' do