From 756c7f80ccf7a86311569ccd5f4f24dc478dd0b4 Mon Sep 17 00:00:00 2001 From: Daniela Soares Feitosa Date: Wed, 16 Sep 2009 11:31:29 -0300 Subject: [PATCH] ActionItem1225: adding method to count tags --- app/controllers/public/search_controller.rb | 5 +---- app/models/environment.rb | 9 +++++++++ app/models/profile.rb | 14 +++----------- test/unit/environment_test.rb | 18 ++++++++++++++++++ 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index 6176344..590ccba 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -221,10 +221,7 @@ class SearchController < PublicController def tags @tags_cache_key = "tags_env_#{environment.id.to_s}" if is_cache_expired?(@tags_cache_key, true) - @tags = environment.tags.inject({}) do |memo,tag| - memo[tag.name] = tag.taggings.count - memo - end + @tags = environment.tags_count end end diff --git a/app/models/environment.rb b/app/models/environment.rb index 5e8d163..0618c65 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -534,6 +534,15 @@ class Environment < ActiveRecord::Base has_many :tags, :through => :articles + def tags_count + options = Article.find_options_for_tag_counts.merge(:conditions => ['profiles.environment_id = ?', self.id]) + options[:joins] = options[:joins] + ' LEFT OUTER JOIN profiles on profiles.id = articles.profile_id' + Tag.find(:all, options).inject({}) do |memo,tag| + memo[tag.name] = tag.count + memo + end + end + def theme self[:theme] || 'default' end diff --git a/app/models/profile.rb b/app/models/profile.rb index d86bd6e..ed90d84 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -386,19 +386,11 @@ private :generate_url, :url_options environment.domains + domains end - # FIXME this can be SLOW def article_tags - totals = {} - articles.each do |article| - article.tags.each do |tag| - if totals[tag.name] - totals[tag.name] += 1 - else - totals[tag.name] = 1 - end - end + articles.tag_counts.inject({}) do |memo,tag| + memo[tag.name] = tag.count + memo end - totals end def find_tagged_with(tag) diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb index 03998ba..5c8671c 100644 --- a/test/unit/environment_test.rb +++ b/test/unit/environment_test.rb @@ -804,4 +804,22 @@ class EnvironmentTest < Test::Unit::TestCase assert_equal 4, e.news_amount_by_folder end + + should 'list tags with their counts' do + user = create_user('testinguser').person + user.articles.build(:name => 'article 1', :tag_list => 'first-tag').save! + user.articles.build(:name => 'article 2', :tag_list => 'first-tag, second-tag').save! + user.articles.build(:name => 'article 3', :tag_list => 'first-tag, second-tag, third-tag').save! + + assert_equal({ 'first-tag' => 3, 'second-tag' => 2, 'third-tag' => 1 }, Environment.default.tags_count) + end + + should 'not list tags count from other environment' do + e = Environment.create!(:name => 'test_env') + user = create_user('testinguser', :environment => e).person + user.articles.build(:name => 'article 1', :tag_list => 'first-tag').save! + + assert_equal({}, Environment.default.tags_count) + end + end -- libgit2 0.21.2