Commit c4f59660f0f2849aee78c2e7267cd12d434e2470
1 parent
5da04adb
Exists in
master
and in
22 other branches
products-block: fetch sample products without load all products on the memory
ActionItem3167
Showing
2 changed files
with
4 additions
and
11 deletions
Show diff stats
app/models/products_block.rb
| @@ -46,17 +46,10 @@ class ProductsBlock < Block | @@ -46,17 +46,10 @@ class ProductsBlock < Block | ||
| 46 | 46 | ||
| 47 | def products(reload = false) | 47 | def products(reload = false) |
| 48 | if product_ids.blank? | 48 | if product_ids.blank? |
| 49 | - products_list = owner.products(reload) | ||
| 50 | - result = [] | ||
| 51 | - [4, products_list.size].min.times do | ||
| 52 | - p = products_list.rand | ||
| 53 | - result << p | ||
| 54 | - products_list -= [p] | ||
| 55 | - end | ||
| 56 | - result | 49 | + owner.products.order('RANDOM()').limit([4,owner.products.count].min) |
| 57 | else | 50 | else |
| 58 | - product_ids.map {|item| owner.products.find(item) } | ||
| 59 | - end | 51 | + owner.products.where(:id => product_ids) |
| 52 | + end.compact | ||
| 60 | end | 53 | end |
| 61 | 54 | ||
| 62 | end | 55 | end |
test/unit/products_block_test.rb
| @@ -86,7 +86,7 @@ class ProductsBlockTest < ActiveSupport::TestCase | @@ -86,7 +86,7 @@ class ProductsBlockTest < ActiveSupport::TestCase | ||
| 86 | block.stubs(:owner).returns(enterprise) | 86 | block.stubs(:owner).returns(enterprise) |
| 87 | 87 | ||
| 88 | block.product_ids = [p1, p3, p5].map(&:id) | 88 | block.product_ids = [p1, p3, p5].map(&:id) |
| 89 | - assert_equal [p1, p3, p5], block.products | 89 | + assert_equivalent [p1, p3, p5], block.products |
| 90 | end | 90 | end |
| 91 | 91 | ||
| 92 | should 'save product_ids' do | 92 | should 'save product_ids' do |