Commit a6de20aa107e3d209f019f68cf1fe7731dd1afaa

Authored by AntonioTerceiro
1 parent 99581ed1

ActionItem243: relating comments to category


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1597 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/article_categorization.rb 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +class ArticleCategorization < ActiveRecord::Base
  2 + set_table_name :articles_categories
  3 + belongs_to :article
  4 + belongs_to :category
  5 +end
... ...
app/models/category.rb
... ... @@ -21,17 +21,21 @@ class Category &lt; ActiveRecord::Base
21 21  
22 22 acts_as_filesystem
23 23  
24   - has_and_belongs_to_many :articles
  24 + has_many :article_categorizations
  25 + has_many :articles, :through => :article_categorizations
  26 + has_many :comments, :through => :articles
  27 +
25 28 def recent_articles(limit = 10)
26 29 self.articles.recent(limit)
27 30 end
28 31  
29 32 def recent_comments(limit = 10)
30   - # FIXME this can become SLOW
31   - Comment.find(:all, :order => 'created_on DESC, comments.id DESC', :limit => limit, :conditions => { :article_id => article_ids})
  33 + comments.find(:all, :order => 'created_on DESC, comments.id DESC', :limit => limit)
32 34 end
33 35  
34 36 def most_commented_articles(limit = 10)
35 37 self.articles.most_commented(limit)
36 38 end
  39 +
  40 +
37 41 end
... ...
test/unit/article_categorization_test.rb 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class ArticleCategorizationTest < Test::Unit::TestCase
  4 +
  5 + should 'use articles_categories table' do
  6 + assert_equal 'articles_categories', ArticleCategorization.table_name
  7 + end
  8 +
  9 + should 'belong to article' do
  10 + p = create_user('testuser').person
  11 + article = p.articles.build(:name => 'test article'); article.save!
  12 + assert_equal article, ArticleCategorization.create!(:article => article).article
  13 + end
  14 +
  15 + should 'belong to category' do
  16 + category = Category.create!(:name => 'one category', :environment => Environment.default)
  17 + assert_equal category, ArticleCategorization.create!(:category => category).category
  18 + end
  19 +
  20 +end
... ...
test/unit/category_test.rb
... ... @@ -264,4 +264,19 @@ class CategoryTest &lt; Test::Unit::TestCase
264 264  
265 265 end
266 266  
  267 + should 'have comments' do
  268 + c = @env.categories.build(:name => 'my category'); c.save!
  269 + person = create_user('testuser').person
  270 +
  271 + a1 = person.articles.build(:name => 'art1', :categories => [c]); a1.save!
  272 + a2 = person.articles.build(:name => 'art2', :categories => [c]); a2.save!
  273 + a3 = person.articles.build(:name => 'art3', :categories => [c]); a3.save!
  274 +
  275 + c1 = a1.comments.build(:title => 'test', :body => 'asdsa', :author => person); c1.save!
  276 + c2 = a2.comments.build(:title => 'test', :body => 'asdsa', :author => person); c2.save!
  277 + c3 = a3.comments.build(:title => 'test', :body => 'asdsa', :author => person); c3.save!
  278 +
  279 + assert_equivalent [c1, c2, c3], c.comments
  280 + end
  281 +
267 282 end
... ...
test/unit/comment_test.rb
... ... @@ -93,7 +93,7 @@ class CommentTest &lt; Test::Unit::TestCase
93 93 assert_equal 'comment-4321', comment.anchor
94 94 end
95 95  
96   - should 'be searched by contents' do
  96 + should 'be searched by contents of title' do
97 97 owner = create_user('testuser').person
98 98 art = owner.articles.build(:name => 'ytest'); art.save!
99 99 c1 = art.comments.build(:title => 'test comment', :body => 'anything', :author => owner); c1.save!
... ... @@ -101,4 +101,12 @@ class CommentTest &lt; Test::Unit::TestCase
101 101 assert_includes Comment.find_by_contents('test'), c1
102 102 end
103 103  
  104 + should 'be searched by contents of body' do
  105 + owner = create_user('testuser').person
  106 + art = owner.articles.build(:name => 'ytest'); art.save!
  107 + c1 = art.comments.build(:title => 'test comment', :body => 'anything', :author => owner); c1.save!
  108 +
  109 + assert_includes Comment.find_by_contents('anything'), c1
  110 + end
  111 +
104 112 end
... ...