diff --git a/app/models/article_categorization.rb b/app/models/article_categorization.rb new file mode 100644 index 0000000..8a7f517 --- /dev/null +++ b/app/models/article_categorization.rb @@ -0,0 +1,5 @@ +class ArticleCategorization < ActiveRecord::Base + set_table_name :articles_categories + belongs_to :article + belongs_to :category +end diff --git a/app/models/category.rb b/app/models/category.rb index ced7c88..6bb3836 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -21,17 +21,21 @@ class Category < ActiveRecord::Base acts_as_filesystem - has_and_belongs_to_many :articles + has_many :article_categorizations + has_many :articles, :through => :article_categorizations + has_many :comments, :through => :articles + def recent_articles(limit = 10) self.articles.recent(limit) end def recent_comments(limit = 10) - # FIXME this can become SLOW - Comment.find(:all, :order => 'created_on DESC, comments.id DESC', :limit => limit, :conditions => { :article_id => article_ids}) + comments.find(:all, :order => 'created_on DESC, comments.id DESC', :limit => limit) end def most_commented_articles(limit = 10) self.articles.most_commented(limit) end + + end diff --git a/test/unit/article_categorization_test.rb b/test/unit/article_categorization_test.rb new file mode 100644 index 0000000..93772f6 --- /dev/null +++ b/test/unit/article_categorization_test.rb @@ -0,0 +1,20 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class ArticleCategorizationTest < Test::Unit::TestCase + + should 'use articles_categories table' do + assert_equal 'articles_categories', ArticleCategorization.table_name + end + + should 'belong to article' do + p = create_user('testuser').person + article = p.articles.build(:name => 'test article'); article.save! + assert_equal article, ArticleCategorization.create!(:article => article).article + end + + should 'belong to category' do + category = Category.create!(:name => 'one category', :environment => Environment.default) + assert_equal category, ArticleCategorization.create!(:category => category).category + end + +end diff --git a/test/unit/category_test.rb b/test/unit/category_test.rb index 8b57c27..4612b45 100644 --- a/test/unit/category_test.rb +++ b/test/unit/category_test.rb @@ -264,4 +264,19 @@ class CategoryTest < Test::Unit::TestCase end + should 'have comments' do + c = @env.categories.build(:name => 'my category'); c.save! + person = create_user('testuser').person + + a1 = person.articles.build(:name => 'art1', :categories => [c]); a1.save! + a2 = person.articles.build(:name => 'art2', :categories => [c]); a2.save! + a3 = person.articles.build(:name => 'art3', :categories => [c]); a3.save! + + c1 = a1.comments.build(:title => 'test', :body => 'asdsa', :author => person); c1.save! + c2 = a2.comments.build(:title => 'test', :body => 'asdsa', :author => person); c2.save! + c3 = a3.comments.build(:title => 'test', :body => 'asdsa', :author => person); c3.save! + + assert_equivalent [c1, c2, c3], c.comments + end + end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index ec58bff..180027d 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -93,7 +93,7 @@ class CommentTest < Test::Unit::TestCase assert_equal 'comment-4321', comment.anchor end - should 'be searched by contents' do + should 'be searched by contents of title' do owner = create_user('testuser').person art = owner.articles.build(:name => 'ytest'); art.save! c1 = art.comments.build(:title => 'test comment', :body => 'anything', :author => owner); c1.save! @@ -101,4 +101,12 @@ class CommentTest < Test::Unit::TestCase assert_includes Comment.find_by_contents('test'), c1 end + should 'be searched by contents of body' do + owner = create_user('testuser').person + art = owner.articles.build(:name => 'ytest'); art.save! + c1 = art.comments.build(:title => 'test comment', :body => 'anything', :author => owner); c1.save! + + assert_includes Comment.find_by_contents('anything'), c1 + end + end -- libgit2 0.21.2