Commit 6f7fdaf9ed2c97e07961507e88b34224f73689df
1 parent
7d54b7cc
Exists in
master
and in
29 other branches
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. git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1342 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
2 changed files
with
28 additions
and
1 deletions
Show diff stats
app/models/profile_list_block.rb
@@ -6,9 +6,25 @@ class ProfileListBlock < Block | @@ -6,9 +6,25 @@ class ProfileListBlock < Block | ||
6 | _('A block that displays random profiles') | 6 | _('A block that displays random profiles') |
7 | end | 7 | end |
8 | 8 | ||
9 | + # Override this method to make the block list specific types of profiles | ||
10 | + # instead of anyone. | ||
11 | + # | ||
12 | + # In this class this method just returns <tt>Profile</tt> (the class). In | ||
13 | + # subclasses you could return <tt>Person</tt>, for instance, if you only want | ||
14 | + # to list people, or <tt>Organization</tt>, if you want organizations only. | ||
15 | + # | ||
16 | + # You don't need to return only classes. You can for instance return an | ||
17 | + # association array from a has_many ActiveRecord association, for example. | ||
18 | + # Actually the only requirement for the object returned by this method is to | ||
19 | + # have a <tt>find</tt> method that accepts the same interface as the | ||
20 | + # ActiveRecord::Base's find method . | ||
21 | + def profile_finder | ||
22 | + Profile | ||
23 | + end | ||
24 | + | ||
9 | def profiles | 25 | def profiles |
10 | # FIXME pick random people instead | 26 | # FIXME pick random people instead |
11 | - Profile.find(:all, :limit => self.limit, :order => 'created_at desc') | 27 | + profile_finder.find(:all, :limit => self.limit, :order => 'created_at desc') |
12 | end | 28 | end |
13 | 29 | ||
14 | def random(top) | 30 | def random(top) |
test/unit/profile_list_block_test.rb
@@ -40,6 +40,17 @@ class ProfileListBlockTest < Test::Unit::TestCase | @@ -40,6 +40,17 @@ class ProfileListBlockTest < Test::Unit::TestCase | ||
40 | assert_kind_of String, instance_eval(&block.content) | 40 | assert_kind_of String, instance_eval(&block.content) |
41 | end | 41 | end |
42 | 42 | ||
43 | + should 'find in Profile by default' do | ||
44 | + assert_equal Profile, ProfileListBlock.new.profile_finder | ||
45 | + end | ||
46 | + | ||
47 | + should 'ask profile finder for profiles' do | ||
48 | + block = ProfileListBlock.new | ||
49 | + block.expects(:profile_finder).returns(Profile).once | ||
50 | + Profile.expects(:find).returns([]) | ||
51 | + block.profiles | ||
52 | + end | ||
53 | + | ||
43 | should 'pick random people' | 54 | should 'pick random people' |
44 | 55 | ||
45 | should 'use Kernel.rand to generate random numbers' do | 56 | should 'use Kernel.rand to generate random numbers' do |