diff --git a/app/models/block.rb b/app/models/block.rb index 1b9e91e..77b108e 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -1,6 +1,7 @@ class Block < ActiveRecord::Base # to be able to generate HTML + include ActionView::Helpers::UrlHelper include ActionView::Helpers::TagHelper acts_as_list :scope => :box diff --git a/app/models/recent_documents_block.rb b/app/models/recent_documents_block.rb index 3d21627..9e3430f 100644 --- a/app/models/recent_documents_block.rb +++ b/app/models/recent_documents_block.rb @@ -1,12 +1,21 @@ class RecentDocumentsBlock < Block + def self.description + _('List of recent content') + end + + settings_item :limit + def content - lambda do - content_tag( - 'ul', - profile.recent_documents.map {|item| content_tag('li', link_to_document(item)) }.join("\n") - ) - end + docs = + if self.limit.nil? + owner.recent_documents + else + owner.recent_documents(self.limit) + end + + content_tag('ul', docs.map {|item| content_tag('li', link_to(item.title, item.url))}.join("\n")) + end end diff --git a/app/models/tags_block.rb b/app/models/tags_block.rb index d22b22c..f5ec354 100644 --- a/app/models/tags_block.rb +++ b/app/models/tags_block.rb @@ -1,7 +1,6 @@ class TagsBlock < Block include TagsHelper - include ActionView::Helpers::UrlHelper def self.description _('List count of contents by tag') diff --git a/test/unit/recent_documents_block_test.rb b/test/unit/recent_documents_block_test.rb index 44cbfa5..84d0293 100644 --- a/test/unit/recent_documents_block_test.rb +++ b/test/unit/recent_documents_block_test.rb @@ -2,26 +2,44 @@ require File.dirname(__FILE__) + '/../test_helper' class RecentDocumentsBlockTest < Test::Unit::TestCase - # Replace this with your real tests. - def test_should_output_list_with_links_to_recent_documents - profile = mock - profile.stubs(:identifier).returns('a_test_profile') - - doc1 = mock - doc2 = mock - doc3 = mock - profile.expects(:recent_documents).returns([doc1, doc2, doc3]) - - helper = mock - helper.expects(:profile).returns(profile) - helper.expects(:link_to_document).with(doc1).returns('doc1') - helper.expects(:content_tag).with('li', 'doc1').returns('doc1') - helper.expects(:link_to_document).with(doc2).returns('doc2') - helper.expects(:content_tag).with('li', 'doc2').returns('doc2') - helper.expects(:link_to_document).with(doc3).returns('doc3') - helper.expects(:content_tag).with('li', 'doc3').returns('doc3') - helper.expects(:content_tag).with('ul', "doc1\ndoc2\ndoc3").returns('the_tag') - - assert_equal('the_tag', helper.instance_eval(&RecentDocumentsBlock.new.content)) + def setup + profile = create_user('testinguser').person + profile.articles.build(:name => 'first').save! + profile.articles.build(:name => 'second').save! + profile.articles.build(:name => 'third').save! + profile.articles.build(:name => 'forth').save! + profile.articles.build(:name => 'fifth').save! + + box = Box.create!(:owner => profile) + @block = RecentDocumentsBlock.create!(:box_id => box.id) + + end + attr_reader :block + + should 'describe itself' do + assert_not_equal Block.description, RecentDocumentsBlock.description + end + + should 'output list with links to recent documents' do + output = block.content + + assert_match /href=.*\/testinguser\/first/, output + assert_match /href=.*\/testinguser\/second/, output + assert_match /href=.*\/testinguser\/third/, output + assert_match /href=.*\/testinguser\/forth/, output + assert_match /href=.*\/testinguser\/fifth/, output end + + should 'respect the maximum number of items as configured' do + block.limit = 3 + + output = block.content + + assert_match /href=.*\/testinguser\/first/, output + assert_match /href=.*\/testinguser\/second/, output + assert_match /href=.*\/testinguser\/third/, output + assert_no_match /href=.*\/testinguser\/forth/, output + assert_no_match /href=.*\/testinguser\/fifth/, output + end + end -- libgit2 0.21.2