Commit 26d1c265de82811b5a958c53f6b392f6f7caad8e

Authored by Rodrigo Souto
1 parent 109fffc3

[postgres-tests] Fixing features products block offset logic

Also fixing a test that was sort dependent.
app/models/featured_products_block.rb
... ... @@ -7,8 +7,9 @@ class FeaturedProductsBlock < Block
7 7  
8 8 before_save do |block|
9 9 if block.owner.kind_of?(Environment) && block.product_ids.blank?
10   - seed = block.owner.products.count
11   - block.product_ids = block.owner.highlighted_products_with_image(:offset => (rand(seed) % (seed - block.groups_of * 3)), :limit => block.groups_of * 3).map(&:id)
  10 + total = block.owner.products.count
  11 + offset = rand([(total - block.groups_of * 3) + 1, 1].max)
  12 + block.product_ids = block.owner.highlighted_products_with_image(:offset => offset, :limit => block.groups_of * 3).map(&:id)
12 13 end
13 14 block.groups_of = block.groups_of.to_i
14 15 end
... ...
test/unit/featured_products_block_test.rb
... ... @@ -129,7 +129,11 @@ class FeaturedProductsBlockTest < ActiveSupport::TestCase
129 129 })
130 130 @environment.boxes.first.blocks<< block
131 131  
132   - assert_equal products, block.products_for_selection
  132 + products_for_selection = block.products_for_selection
  133 +
  134 + products.each do |product|
  135 + assert_includes products_for_selection, product
  136 + end
133 137 end
134 138  
135 139 end
... ...