diff --git a/app/models/article.rb b/app/models/article.rb index f94799c..608d9f7 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -13,12 +13,18 @@ class Article < ActiveRecord::Base acts_as_versioned - # retrives all articles belonging to the given +profile+ that are not + # retrieves all articles belonging to the given +profile+ that are not # sub-articles of any other article. - def Article.top_level_for(profile) + def self.top_level_for(profile) self.find(:all, :conditions => [ 'parent_id is null and profile_id = ?', profile.id ]) end + # retrieves the latest +limit+ articles in profile +profile+, sorted from the + # most recent to the oldest. + def self.recent(profile, limit) + self.find(:all, :limit => limit, :order => 'created_on') + end + # produces the HTML code that is to be displayed as this article's contents. # # The implementation in this class just provides the +body+ attribute as the diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 8f7fde1..66f00d1 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -128,7 +128,19 @@ class ArticleTest < Test::Unit::TestCase a.last_changed_by = Person.new a.save! end + end + should 'search for recent documents for a given profile' do + first = profile.articles.build(:name => 'first'); first.save! + second = profile.articles.build(:name => 'second'); second.save! + third = profile.articles.build(:name => 'third'); third.save! + forth = profile.articles.build(:name => 'forth'); forth.save! + fifth = profile.articles.build(:name => 'fifth'); fifth.save! + + assert_equal [first,second,third], Article.recent(profile, 3) + + assert_equal [first,second,third,forth,fifth], Article.recent(profile, 10) + end end -- libgit2 0.21.2