Commit 756c7f80ccf7a86311569ccd5f4f24dc478dd0b4

Authored by Daniela Feitosa
Committed by Antonio Terceiro
1 parent 8ab65398

ActionItem1225: adding method to count tags

app/controllers/public/search_controller.rb
@@ -221,10 +221,7 @@ class SearchController < PublicController @@ -221,10 +221,7 @@ class SearchController < PublicController
221 def tags 221 def tags
222 @tags_cache_key = "tags_env_#{environment.id.to_s}" 222 @tags_cache_key = "tags_env_#{environment.id.to_s}"
223 if is_cache_expired?(@tags_cache_key, true) 223 if is_cache_expired?(@tags_cache_key, true)
224 - @tags = environment.tags.inject({}) do |memo,tag|  
225 - memo[tag.name] = tag.taggings.count  
226 - memo  
227 - end 224 + @tags = environment.tags_count
228 end 225 end
229 end 226 end
230 227
app/models/environment.rb
@@ -534,6 +534,15 @@ class Environment < ActiveRecord::Base @@ -534,6 +534,15 @@ class Environment < ActiveRecord::Base
534 534
535 has_many :tags, :through => :articles 535 has_many :tags, :through => :articles
536 536
  537 + def tags_count
  538 + options = Article.find_options_for_tag_counts.merge(:conditions => ['profiles.environment_id = ?', self.id])
  539 + options[:joins] = options[:joins] + ' LEFT OUTER JOIN profiles on profiles.id = articles.profile_id'
  540 + Tag.find(:all, options).inject({}) do |memo,tag|
  541 + memo[tag.name] = tag.count
  542 + memo
  543 + end
  544 + end
  545 +
537 def theme 546 def theme
538 self[:theme] || 'default' 547 self[:theme] || 'default'
539 end 548 end
app/models/profile.rb
@@ -386,19 +386,11 @@ private :generate_url, :url_options @@ -386,19 +386,11 @@ private :generate_url, :url_options
386 environment.domains + domains 386 environment.domains + domains
387 end 387 end
388 388
389 - # FIXME this can be SLOW  
390 def article_tags 389 def article_tags
391 - totals = {}  
392 - articles.each do |article|  
393 - article.tags.each do |tag|  
394 - if totals[tag.name]  
395 - totals[tag.name] += 1  
396 - else  
397 - totals[tag.name] = 1  
398 - end  
399 - end 390 + articles.tag_counts.inject({}) do |memo,tag|
  391 + memo[tag.name] = tag.count
  392 + memo
400 end 393 end
401 - totals  
402 end 394 end
403 395
404 def find_tagged_with(tag) 396 def find_tagged_with(tag)
test/unit/environment_test.rb
@@ -804,4 +804,22 @@ class EnvironmentTest < Test::Unit::TestCase @@ -804,4 +804,22 @@ class EnvironmentTest < Test::Unit::TestCase
804 804
805 assert_equal 4, e.news_amount_by_folder 805 assert_equal 4, e.news_amount_by_folder
806 end 806 end
  807 +
  808 + should 'list tags with their counts' do
  809 + user = create_user('testinguser').person
  810 + user.articles.build(:name => 'article 1', :tag_list => 'first-tag').save!
  811 + user.articles.build(:name => 'article 2', :tag_list => 'first-tag, second-tag').save!
  812 + user.articles.build(:name => 'article 3', :tag_list => 'first-tag, second-tag, third-tag').save!
  813 +
  814 + assert_equal({ 'first-tag' => 3, 'second-tag' => 2, 'third-tag' => 1 }, Environment.default.tags_count)
  815 + end
  816 +
  817 + should 'not list tags count from other environment' do
  818 + e = Environment.create!(:name => 'test_env')
  819 + user = create_user('testinguser', :environment => e).person
  820 + user.articles.build(:name => 'article 1', :tag_list => 'first-tag').save!
  821 +
  822 + assert_equal({}, Environment.default.tags_count)
  823 + end
  824 +
807 end 825 end