From 2ab9e1f4275c5bf808c40432b88f7df9a70e2d1e Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Sat, 9 Feb 2008 17:59:03 +0000 Subject: [PATCH] ActionItem41: actually generating the list of people --- app/controllers/my_profile/profile_design_controller.rb | 2 +- app/helpers/block_helper.rb | 7 +++++++ app/models/profile_list_block.rb | 23 +++++++++++++++++++++-- test/unit/profile_list_block_test.rb | 29 ++++++++++++++++++++++++++--- 4 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 app/helpers/block_helper.rb diff --git a/app/controllers/my_profile/profile_design_controller.rb b/app/controllers/my_profile/profile_design_controller.rb index 108ccf0..51c00d0 100644 --- a/app/controllers/my_profile/profile_design_controller.rb +++ b/app/controllers/my_profile/profile_design_controller.rb @@ -3,7 +3,7 @@ class ProfileDesignController < BoxOrganizerController needs_profile def available_blocks - @available_blocks ||= [ Block, ArticleBlock, TagsBlock, RecentDocumentsBlock ] + @available_blocks ||= [ Block, ArticleBlock, TagsBlock, RecentDocumentsBlock, ProfileInfoBlock ] end end diff --git a/app/helpers/block_helper.rb b/app/helpers/block_helper.rb new file mode 100644 index 0000000..d90fdb1 --- /dev/null +++ b/app/helpers/block_helper.rb @@ -0,0 +1,7 @@ +module BlockHelper + + def block_title(title) + content_tag('h3', title, :class => 'block-title') + end + +end diff --git a/app/models/profile_list_block.rb b/app/models/profile_list_block.rb index 0ba6eea..7b88ac3 100644 --- a/app/models/profile_list_block.rb +++ b/app/models/profile_list_block.rb @@ -1,15 +1,34 @@ class ProfileListBlock < Block - settings_items :limit, :default => 10 + settings_items :limit, :default => 6 def self.description _('A block that displays random profiles') end + def profiles + top = Profile.count + + result = [] + maxsize = [limit,top].compact.min + + maxsize.times do + profile = Profile.find(random(top) + 1) + result << profile + end + + result + end + + def random(top) + Kernel.rand(top) + end + def content profiles = self.profiles lambda do - profiles.map {|item| profile_image_link(item) } + block_title(_('People')) + + profiles.map {|item| content_tag('div', profile_image_link(item)) }.join("\n") end end diff --git a/test/unit/profile_list_block_test.rb b/test/unit/profile_list_block_test.rb index 4fbf153..2a385fd 100644 --- a/test/unit/profile_list_block_test.rb +++ b/test/unit/profile_list_block_test.rb @@ -6,9 +6,9 @@ class ProfileListBlockTest < Test::Unit::TestCase assert_not_equal Block.description, ProfileListBlock.description end - should 'accept a limit of people to be displayed (and default to 10)' do + should 'accept a limit of people to be displayed (and default to 6)' do block = ProfileListBlock.new - assert_equal 10, block.limit + assert_equal 6, block.limit block.limit = 20 assert_equal 20, block.limit @@ -33,7 +33,30 @@ class ProfileListBlockTest < Test::Unit::TestCase self.expects(:profile_image_link).with(person2).never self.expects(:profile_image_link).with(person3).once - instance_eval(&block.content) + self.expects(:content_tag).returns('
').at_least_once + self.expects(:_).returns('text').at_least_once + self.expects(:block_title).returns('block title').at_least_once + + assert_kind_of String, instance_eval(&block.content) + end + + should 'pick random people' do + block = ProfileListBlock.new + + Profile.expects(:count).returns(3) + + block.expects(:random).times(3).returns(7).then.returns(4).then.returns(5) + + Profile.expects(:find).with(8) + Profile.expects(:find).with(5) + Profile.expects(:find).with(6) + + block.profiles + end + + should 'use Kernel.rand to generate random numbers' do + Kernel.expects(:rand).with(77).once + ProfileListBlock.new.random(77) end end -- libgit2 0.21.2