From 6f7fdaf9ed2c97e07961507e88b34224f73689df Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Sat, 9 Feb 2008 20:04:04 +0000 Subject: [PATCH] ActionItem43: using a finder in ProfileListBlock instead of looking explicitly in Profile. This way subclasses can just override the finder and reuse the logic of the list generation. --- app/models/profile_list_block.rb | 18 +++++++++++++++++- test/unit/profile_list_block_test.rb | 11 +++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/app/models/profile_list_block.rb b/app/models/profile_list_block.rb index cb49e20..456b588 100644 --- a/app/models/profile_list_block.rb +++ b/app/models/profile_list_block.rb @@ -6,9 +6,25 @@ class ProfileListBlock < Block _('A block that displays random profiles') end + # Override this method to make the block list specific types of profiles + # instead of anyone. + # + # In this class this method just returns Profile (the class). In + # subclasses you could return Person, for instance, if you only want + # to list people, or Organization, if you want organizations only. + # + # You don't need to return only classes. You can for instance return an + # association array from a has_many ActiveRecord association, for example. + # Actually the only requirement for the object returned by this method is to + # have a find method that accepts the same interface as the + # ActiveRecord::Base's find method . + def profile_finder + Profile + end + def profiles # FIXME pick random people instead - Profile.find(:all, :limit => self.limit, :order => 'created_at desc') + profile_finder.find(:all, :limit => self.limit, :order => 'created_at desc') end def random(top) diff --git a/test/unit/profile_list_block_test.rb b/test/unit/profile_list_block_test.rb index e6761d0..46c7003 100644 --- a/test/unit/profile_list_block_test.rb +++ b/test/unit/profile_list_block_test.rb @@ -40,6 +40,17 @@ class ProfileListBlockTest < Test::Unit::TestCase assert_kind_of String, instance_eval(&block.content) end + should 'find in Profile by default' do + assert_equal Profile, ProfileListBlock.new.profile_finder + end + + should 'ask profile finder for profiles' do + block = ProfileListBlock.new + block.expects(:profile_finder).returns(Profile).once + Profile.expects(:find).returns([]) + block.profiles + end + should 'pick random people' should 'use Kernel.rand to generate random numbers' do -- libgit2 0.21.2