Commit c9ea46745213b34c18ba53dae253eba290b47e1f

Authored by Antonio Terceiro
1 parent 3d567c77

Reorganizing the way we find tagged content

(ActionItem1553)
app/controllers/public/search_controller.rb
@@ -226,7 +226,7 @@ class SearchController < PublicController @@ -226,7 +226,7 @@ class SearchController < PublicController
226 @tag = environment.tags.find_by_name(params[:tag]) 226 @tag = environment.tags.find_by_name(params[:tag])
227 @tag_cache_key = "tag_#{CGI.escape(@tag.to_s)}_env_#{environment.id.to_s}_page_#{params[:npage]}" 227 @tag_cache_key = "tag_#{CGI.escape(@tag.to_s)}_env_#{environment.id.to_s}_page_#{params[:npage]}"
228 if is_cache_expired?(@tag_cache_key, true) 228 if is_cache_expired?(@tag_cache_key, true)
229 - @tagged = environment.articles.find_tagged_with(@tag).paginate(:per_page => 10, :page => params[:npage]) 229 + @tagged = environment.articles.find_tagged_with(@tag.name).paginate(:per_page => 10, :page => params[:npage])
230 end 230 end
231 end 231 end
232 232
app/models/article.rb
@@ -335,10 +335,6 @@ class Article < ActiveRecord::Base @@ -335,10 +335,6 @@ class Article < ActiveRecord::Base
335 $1 || '' 335 $1 || ''
336 end 336 end
337 337
338 - def self.find_tagged_with(tag)  
339 - self.find(:all, :include => :taggings, :conditions => ['taggings.tag_id = ?', tag.id])  
340 - end  
341 -  
342 def creator 338 def creator
343 creator_id = versions[0][:last_changed_by_id] 339 creator_id = versions[0][:last_changed_by_id]
344 creator_id && Profile.find(creator_id) 340 creator_id && Profile.find(creator_id)
app/models/profile.rb
@@ -420,8 +420,7 @@ private :generate_url, :url_options @@ -420,8 +420,7 @@ private :generate_url, :url_options
420 end 420 end
421 421
422 def find_tagged_with(tag) 422 def find_tagged_with(tag)
423 - # FIXME: this can be SLOW  
424 - articles.select {|item| item.tags.map(&:name).include?(tag) } 423 + self.articles.find_tagged_with(tag)
425 end 424 end
426 425
427 # Tells whether a specified profile has members or nor. 426 # Tells whether a specified profile has members or nor.
test/unit/article_test.rb
@@ -729,8 +729,7 @@ class ArticleTest < Test::Unit::TestCase @@ -729,8 +729,7 @@ class ArticleTest < Test::Unit::TestCase
729 729
730 should 'get tagged with tag' do 730 should 'get tagged with tag' do
731 a = Article.create!(:name => 'Published at', :profile => profile, :tag_list => 'bli') 731 a = Article.create!(:name => 'Published at', :profile => profile, :tag_list => 'bli')
732 - t = a.tags[0]  
733 - as = Article.find_tagged_with(t) 732 + as = Article.find_tagged_with('bli')
734 733
735 assert_includes as, a 734 assert_includes as, a
736 end 735 end
@@ -742,8 +741,7 @@ class ArticleTest < Test::Unit::TestCase @@ -742,8 +741,7 @@ class ArticleTest < Test::Unit::TestCase
742 user_from_other_environment = create_user('other_user', :environment => other_environment).person 741 user_from_other_environment = create_user('other_user', :environment => other_environment).person
743 article_from_other_enviroment = create(Article, :profile => user_from_other_environment, :tag_list => 'bli') 742 article_from_other_enviroment = create(Article, :profile => user_from_other_environment, :tag_list => 'bli')
744 743
745 - tag = article_from_other_enviroment.tags.first  
746 - tagged_articles_in_other_environment = other_environment.articles.find_tagged_with(tag) 744 + tagged_articles_in_other_environment = other_environment.articles.find_tagged_with('bli')
747 745
748 assert_includes tagged_articles_in_other_environment, article_from_other_enviroment 746 assert_includes tagged_articles_in_other_environment, article_from_other_enviroment
749 assert_not_includes tagged_articles_in_other_environment, article_from_this_environment 747 assert_not_includes tagged_articles_in_other_environment, article_from_this_environment