Commit 2e999a095a94f1b7e7bf563eeaeb53f8eee53020
1 parent
8238d31d
Exists in
master
and in
29 other branches
ActionItem152: revamping recent documents block
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1263 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
4 changed files
with
55 additions
and
28 deletions
Show diff stats
app/models/block.rb
1 | class Block < ActiveRecord::Base | 1 | class Block < ActiveRecord::Base |
2 | 2 | ||
3 | # to be able to generate HTML | 3 | # to be able to generate HTML |
4 | + include ActionView::Helpers::UrlHelper | ||
4 | include ActionView::Helpers::TagHelper | 5 | include ActionView::Helpers::TagHelper |
5 | 6 | ||
6 | acts_as_list :scope => :box | 7 | acts_as_list :scope => :box |
app/models/recent_documents_block.rb
1 | class RecentDocumentsBlock < Block | 1 | class RecentDocumentsBlock < Block |
2 | 2 | ||
3 | + def self.description | ||
4 | + _('List of recent content') | ||
5 | + end | ||
6 | + | ||
7 | + settings_item :limit | ||
8 | + | ||
3 | def content | 9 | def content |
4 | - lambda do | ||
5 | - content_tag( | ||
6 | - 'ul', | ||
7 | - profile.recent_documents.map {|item| content_tag('li', link_to_document(item)) }.join("\n") | ||
8 | - ) | ||
9 | - end | 10 | + docs = |
11 | + if self.limit.nil? | ||
12 | + owner.recent_documents | ||
13 | + else | ||
14 | + owner.recent_documents(self.limit) | ||
15 | + end | ||
16 | + | ||
17 | + content_tag('ul', docs.map {|item| content_tag('li', link_to(item.title, item.url))}.join("\n")) | ||
18 | + | ||
10 | end | 19 | end |
11 | 20 | ||
12 | end | 21 | end |
app/models/tags_block.rb
test/unit/recent_documents_block_test.rb
@@ -2,26 +2,44 @@ require File.dirname(__FILE__) + '/../test_helper' | @@ -2,26 +2,44 @@ require File.dirname(__FILE__) + '/../test_helper' | ||
2 | 2 | ||
3 | class RecentDocumentsBlockTest < Test::Unit::TestCase | 3 | class RecentDocumentsBlockTest < Test::Unit::TestCase |
4 | 4 | ||
5 | - # Replace this with your real tests. | ||
6 | - def test_should_output_list_with_links_to_recent_documents | ||
7 | - profile = mock | ||
8 | - profile.stubs(:identifier).returns('a_test_profile') | ||
9 | - | ||
10 | - doc1 = mock | ||
11 | - doc2 = mock | ||
12 | - doc3 = mock | ||
13 | - profile.expects(:recent_documents).returns([doc1, doc2, doc3]) | ||
14 | - | ||
15 | - helper = mock | ||
16 | - helper.expects(:profile).returns(profile) | ||
17 | - helper.expects(:link_to_document).with(doc1).returns('doc1') | ||
18 | - helper.expects(:content_tag).with('li', 'doc1').returns('doc1') | ||
19 | - helper.expects(:link_to_document).with(doc2).returns('doc2') | ||
20 | - helper.expects(:content_tag).with('li', 'doc2').returns('doc2') | ||
21 | - helper.expects(:link_to_document).with(doc3).returns('doc3') | ||
22 | - helper.expects(:content_tag).with('li', 'doc3').returns('doc3') | ||
23 | - helper.expects(:content_tag).with('ul', "doc1\ndoc2\ndoc3").returns('the_tag') | ||
24 | - | ||
25 | - assert_equal('the_tag', helper.instance_eval(&RecentDocumentsBlock.new.content)) | 5 | + def setup |
6 | + profile = create_user('testinguser').person | ||
7 | + profile.articles.build(:name => 'first').save! | ||
8 | + profile.articles.build(:name => 'second').save! | ||
9 | + profile.articles.build(:name => 'third').save! | ||
10 | + profile.articles.build(:name => 'forth').save! | ||
11 | + profile.articles.build(:name => 'fifth').save! | ||
12 | + | ||
13 | + box = Box.create!(:owner => profile) | ||
14 | + @block = RecentDocumentsBlock.create!(:box_id => box.id) | ||
15 | + | ||
16 | + end | ||
17 | + attr_reader :block | ||
18 | + | ||
19 | + should 'describe itself' do | ||
20 | + assert_not_equal Block.description, RecentDocumentsBlock.description | ||
21 | + end | ||
22 | + | ||
23 | + should 'output list with links to recent documents' do | ||
24 | + output = block.content | ||
25 | + | ||
26 | + assert_match /href=.*\/testinguser\/first/, output | ||
27 | + assert_match /href=.*\/testinguser\/second/, output | ||
28 | + assert_match /href=.*\/testinguser\/third/, output | ||
29 | + assert_match /href=.*\/testinguser\/forth/, output | ||
30 | + assert_match /href=.*\/testinguser\/fifth/, output | ||
26 | end | 31 | end |
32 | + | ||
33 | + should 'respect the maximum number of items as configured' do | ||
34 | + block.limit = 3 | ||
35 | + | ||
36 | + output = block.content | ||
37 | + | ||
38 | + assert_match /href=.*\/testinguser\/first/, output | ||
39 | + assert_match /href=.*\/testinguser\/second/, output | ||
40 | + assert_match /href=.*\/testinguser\/third/, output | ||
41 | + assert_no_match /href=.*\/testinguser\/forth/, output | ||
42 | + assert_no_match /href=.*\/testinguser\/fifth/, output | ||
43 | + end | ||
44 | + | ||
27 | end | 45 | end |