From cfb2b54198c2f4048663bffcd0c809b2f1df4af1 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Tue, 8 Feb 2011 16:35:07 -0300 Subject: [PATCH] ProfileListBlock option to prioritize profiles with image --- app/models/profile_list_block.rb | 8 ++++++-- app/views/box_organizer/_profile_list_block.rhtml | 1 + test/unit/profile_list_block_test.rb | 21 ++++++++++++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/app/models/profile_list_block.rb b/app/models/profile_list_block.rb index dc3c671..8240057 100644 --- a/app/models/profile_list_block.rb +++ b/app/models/profile_list_block.rb @@ -1,6 +1,7 @@ class ProfileListBlock < Block settings_items :limit, :type => :integer, :default => 6 + settings_items :prioritize_profiles_with_image, :type => :boolean, :default => false def self.description _('Random profiles') @@ -12,8 +13,7 @@ class ProfileListBlock < Block end def profile_list - random = randomizer - profiles.visible.all(:limit => limit, :select => 'DISTINCT profiles.*, ' + random, :order => random) + profiles.visible.all(:include => :image, :limit => limit, :select => 'DISTINCT profiles.*, ' + image_prioritizer + randomizer, :order => image_prioritizer + randomizer) end def profile_count @@ -24,6 +24,10 @@ class ProfileListBlock < Block @randomizer ||= "(profiles.id % #{rand(profile_count) + 1})" end + def image_prioritizer + prioritize_profiles_with_image ? '(images.id is null),' : '' + end + # the title of the block. Probably will be overriden in subclasses. def default_title _('{#} People or Groups') diff --git a/app/views/box_organizer/_profile_list_block.rhtml b/app/views/box_organizer/_profile_list_block.rhtml index 391fe2d..f2f87cf 100644 --- a/app/views/box_organizer/_profile_list_block.rhtml +++ b/app/views/box_organizer/_profile_list_block.rhtml @@ -1,4 +1,5 @@
<%= labelled_form_field _('Limit of items'), text_field(:block, :limit, :size => 3) %> + <%= labelled_form_field _('Prioritize profiles with image'), check_box(:block, :prioritize_profiles_with_image) %>
diff --git a/test/unit/profile_list_block_test.rb b/test/unit/profile_list_block_test.rb index a052085..9e1f8b9 100644 --- a/test/unit/profile_list_block_test.rb +++ b/test/unit/profile_list_block_test.rb @@ -145,7 +145,7 @@ class ProfileListBlockTest < Test::Unit::TestCase block.stubs(:owner).returns(env) # force the "random" function to return something we know - block.stubs(:randomizer).returns('-id') + block.stubs(:randomizer).returns('-profiles.id') assert_equal [p3.id, p2.id, p1.id], block.profile_list.map(&:id) end @@ -164,4 +164,23 @@ class ProfileListBlockTest < Test::Unit::TestCase assert_no_match /profiles.id % 0/, block.randomizer end + should 'prioritize profiles with image if this option is turned on' do + env = fast_create(Environment) + p1 = fast_create(Person, :environment_id => env.id) + img1 = Image.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :owner => p1) + p2 = fast_create(Person, :environment_id => env.id) + img2 = Image.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :owner => p2) + + p_without_image = fast_create(Person, :environment_id => env.id) + + block = ProfileListBlock.new + block.stubs(:owner).returns(env) + block.stubs(:prioritize_profiles_with_image).returns(true) + + # force the "random" function to return something we know + block.stubs(:randomizer).returns('-profiles.id') + + assert_not_includes block.profile_list[0..1], p_without_image + end + end -- libgit2 0.21.2