Commit da4e64e36d066927142841fac4589901c71ccaf4
1 parent
fdeb05eb
Exists in
master
and in
28 other branches
ActionItem18: implementing recent documents in Profile class
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@434 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
2 changed files
with
35 additions
and
0 deletions
Show diff stats
app/models/profile.rb
@@ -89,4 +89,12 @@ class Profile < ActiveRecord::Base | @@ -89,4 +89,12 @@ class Profile < ActiveRecord::Base | ||
89 | nil | 89 | nil |
90 | end | 90 | end |
91 | 91 | ||
92 | + # gets recent documents in this profile. | ||
93 | + # | ||
94 | + # +limit+ is the maximum number of documents to be returned. It defaults to | ||
95 | + # 10. | ||
96 | + def recent_documents(limit = 10) | ||
97 | + homepage.children.find(:all, :limit => limit, :order => 'created_on desc') | ||
98 | + end | ||
99 | + | ||
92 | end | 100 | end |
test/unit/profile_test.rb
@@ -114,6 +114,33 @@ class ProfileTest < Test::Unit::TestCase | @@ -114,6 +114,33 @@ class ProfileTest < Test::Unit::TestCase | ||
114 | assert_invalid_identifier 'test' | 114 | assert_invalid_identifier 'test' |
115 | end | 115 | end |
116 | 116 | ||
117 | + def test_should_provide_recent_documents | ||
118 | + profile = Profile.create!(:name => 'Testing Recent documents', :identifier => 'testing_recent_documents') | ||
119 | + doc1 = Article.new(:title => 'document 1', :body => 'la la la la la') | ||
120 | + doc1.parent = profile.homepage | ||
121 | + doc1.save! | ||
122 | + | ||
123 | + doc2 = Article.new(:title => 'document 2', :body => 'la la la la la') | ||
124 | + doc2.parent = profile.homepage | ||
125 | + doc2.save! | ||
126 | + | ||
127 | + docs = profile.recent_documents(2) | ||
128 | + assert_equal 2, docs.size | ||
129 | + assert docs.map(&:id).include?(doc1.id) | ||
130 | + assert docs.map(&:id).include?(doc2.id) | ||
131 | + end | ||
132 | + | ||
133 | + def test_should_provide_most_recent_documents | ||
134 | + profile = Profile.create!(:name => 'Testing Recent documents', :identifier => 'testing_recent_documents') | ||
135 | + doc1 = Article.new(:title => 'document 1', :body => 'la la la la la') | ||
136 | + doc1.parent = profile.homepage | ||
137 | + doc1.save! | ||
138 | + | ||
139 | + docs = profile.recent_documents(1) | ||
140 | + assert_equal 1, docs.size | ||
141 | + assert_equal doc1.id, docs.first.id | ||
142 | + end | ||
143 | + | ||
117 | private | 144 | private |
118 | 145 | ||
119 | def assert_invalid_identifier(id) | 146 | def assert_invalid_identifier(id) |