Commit cfb2b54198c2f4048663bffcd0c809b2f1df4af1
Committed by
Daniela Feitosa
1 parent
8c5ac225
Exists in
master
and in
23 other branches
ProfileListBlock option to prioritize profiles with image
(ActionItem1800)
Showing
3 changed files
with
27 additions
and
3 deletions
Show diff stats
app/models/profile_list_block.rb
| 1 | class ProfileListBlock < Block | 1 | class ProfileListBlock < Block |
| 2 | 2 | ||
| 3 | settings_items :limit, :type => :integer, :default => 6 | 3 | settings_items :limit, :type => :integer, :default => 6 |
| 4 | + settings_items :prioritize_profiles_with_image, :type => :boolean, :default => false | ||
| 4 | 5 | ||
| 5 | def self.description | 6 | def self.description |
| 6 | _('Random profiles') | 7 | _('Random profiles') |
| @@ -12,8 +13,7 @@ class ProfileListBlock < Block | @@ -12,8 +13,7 @@ class ProfileListBlock < Block | ||
| 12 | end | 13 | end |
| 13 | 14 | ||
| 14 | def profile_list | 15 | def profile_list |
| 15 | - random = randomizer | ||
| 16 | - profiles.visible.all(:limit => limit, :select => 'DISTINCT profiles.*, ' + random, :order => random) | 16 | + profiles.visible.all(:include => :image, :limit => limit, :select => 'DISTINCT profiles.*, ' + image_prioritizer + randomizer, :order => image_prioritizer + randomizer) |
| 17 | end | 17 | end |
| 18 | 18 | ||
| 19 | def profile_count | 19 | def profile_count |
| @@ -24,6 +24,10 @@ class ProfileListBlock < Block | @@ -24,6 +24,10 @@ class ProfileListBlock < Block | ||
| 24 | @randomizer ||= "(profiles.id % #{rand(profile_count) + 1})" | 24 | @randomizer ||= "(profiles.id % #{rand(profile_count) + 1})" |
| 25 | end | 25 | end |
| 26 | 26 | ||
| 27 | + def image_prioritizer | ||
| 28 | + prioritize_profiles_with_image ? '(images.id is null),' : '' | ||
| 29 | + end | ||
| 30 | + | ||
| 27 | # the title of the block. Probably will be overriden in subclasses. | 31 | # the title of the block. Probably will be overriden in subclasses. |
| 28 | def default_title | 32 | def default_title |
| 29 | _('{#} People or Groups') | 33 | _('{#} People or Groups') |
app/views/box_organizer/_profile_list_block.rhtml
| 1 | <div id='edit-profile-list-block'> | 1 | <div id='edit-profile-list-block'> |
| 2 | <%= labelled_form_field _('Limit of items'), text_field(:block, :limit, :size => 3) %> | 2 | <%= labelled_form_field _('Limit of items'), text_field(:block, :limit, :size => 3) %> |
| 3 | + <%= labelled_form_field _('Prioritize profiles with image'), check_box(:block, :prioritize_profiles_with_image) %> | ||
| 3 | </div> | 4 | </div> |
| 4 | 5 |
test/unit/profile_list_block_test.rb
| @@ -145,7 +145,7 @@ class ProfileListBlockTest < Test::Unit::TestCase | @@ -145,7 +145,7 @@ class ProfileListBlockTest < Test::Unit::TestCase | ||
| 145 | block.stubs(:owner).returns(env) | 145 | block.stubs(:owner).returns(env) |
| 146 | 146 | ||
| 147 | # force the "random" function to return something we know | 147 | # force the "random" function to return something we know |
| 148 | - block.stubs(:randomizer).returns('-id') | 148 | + block.stubs(:randomizer).returns('-profiles.id') |
| 149 | 149 | ||
| 150 | assert_equal [p3.id, p2.id, p1.id], block.profile_list.map(&:id) | 150 | assert_equal [p3.id, p2.id, p1.id], block.profile_list.map(&:id) |
| 151 | end | 151 | end |
| @@ -164,4 +164,23 @@ class ProfileListBlockTest < Test::Unit::TestCase | @@ -164,4 +164,23 @@ class ProfileListBlockTest < Test::Unit::TestCase | ||
| 164 | assert_no_match /profiles.id % 0/, block.randomizer | 164 | assert_no_match /profiles.id % 0/, block.randomizer |
| 165 | end | 165 | end |
| 166 | 166 | ||
| 167 | + should 'prioritize profiles with image if this option is turned on' do | ||
| 168 | + env = fast_create(Environment) | ||
| 169 | + p1 = fast_create(Person, :environment_id => env.id) | ||
| 170 | + img1 = Image.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :owner => p1) | ||
| 171 | + p2 = fast_create(Person, :environment_id => env.id) | ||
| 172 | + img2 = Image.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :owner => p2) | ||
| 173 | + | ||
| 174 | + p_without_image = fast_create(Person, :environment_id => env.id) | ||
| 175 | + | ||
| 176 | + block = ProfileListBlock.new | ||
| 177 | + block.stubs(:owner).returns(env) | ||
| 178 | + block.stubs(:prioritize_profiles_with_image).returns(true) | ||
| 179 | + | ||
| 180 | + # force the "random" function to return something we know | ||
| 181 | + block.stubs(:randomizer).returns('-profiles.id') | ||
| 182 | + | ||
| 183 | + assert_not_includes block.profile_list[0..1], p_without_image | ||
| 184 | + end | ||
| 185 | + | ||
| 167 | end | 186 | end |