From da4e64e36d066927142841fac4589901c71ccaf4 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Mon, 10 Sep 2007 20:32:50 +0000 Subject: [PATCH] ActionItem18: implementing recent documents in Profile class --- app/models/profile.rb | 8 ++++++++ test/unit/profile_test.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 0 deletions(-) diff --git a/app/models/profile.rb b/app/models/profile.rb index 12dc5dd..9d97e1d 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -89,4 +89,12 @@ class Profile < ActiveRecord::Base nil end + # gets recent documents in this profile. + # + # +limit+ is the maximum number of documents to be returned. It defaults to + # 10. + def recent_documents(limit = 10) + homepage.children.find(:all, :limit => limit, :order => 'created_on desc') + end + end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index a993406..077a5ac 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -114,6 +114,33 @@ class ProfileTest < Test::Unit::TestCase assert_invalid_identifier 'test' end + def test_should_provide_recent_documents + profile = Profile.create!(:name => 'Testing Recent documents', :identifier => 'testing_recent_documents') + doc1 = Article.new(:title => 'document 1', :body => 'la la la la la') + doc1.parent = profile.homepage + doc1.save! + + doc2 = Article.new(:title => 'document 2', :body => 'la la la la la') + doc2.parent = profile.homepage + doc2.save! + + docs = profile.recent_documents(2) + assert_equal 2, docs.size + assert docs.map(&:id).include?(doc1.id) + assert docs.map(&:id).include?(doc2.id) + end + + def test_should_provide_most_recent_documents + profile = Profile.create!(:name => 'Testing Recent documents', :identifier => 'testing_recent_documents') + doc1 = Article.new(:title => 'document 1', :body => 'la la la la la') + doc1.parent = profile.homepage + doc1.save! + + docs = profile.recent_documents(1) + assert_equal 1, docs.size + assert_equal doc1.id, docs.first.id + end + private def assert_invalid_identifier(id) -- libgit2 0.21.2