Commit a70c44b190df0765b0f28f3a259df0b4c586d4ab
1 parent
dc7ddee6
Exists in
master
and in
29 other branches
ActionItem560: products is uniquely listed in the block
Showing
2 changed files
with
19 additions
and
1 deletions
Show diff stats
app/models/products_block.rb
... | ... | @@ -41,7 +41,9 @@ class ProductsBlock < Block |
41 | 41 | products_list = owner.products(reload) |
42 | 42 | result = [] |
43 | 43 | [4, products_list.size].min.times do |
44 | - result << products_list.rand | |
44 | + p = products_list.rand | |
45 | + result << p | |
46 | + products_list -= [p] | |
45 | 47 | end |
46 | 48 | result |
47 | 49 | else | ... | ... |
test/unit/products_block_test.rb
... | ... | @@ -107,4 +107,20 @@ class ProductsBlockTest < ActiveSupport::TestCase |
107 | 107 | assert_equal [1, 2], block.product_ids |
108 | 108 | end |
109 | 109 | |
110 | + should 'not repeat products' do | |
111 | + enterprise = Enterprise.create!(:name => 'test_enterprise', :identifier => 'test_enterprise') | |
112 | + p1 = enterprise.products.create!(:name => 'product one') | |
113 | + p2 = enterprise.products.create!(:name => 'product two') | |
114 | + p3 = enterprise.products.create!(:name => 'product three') | |
115 | + p4 = enterprise.products.create!(:name => 'product four') | |
116 | + | |
117 | + block = ProductsBlock.new | |
118 | + enterprise.boxes.first.blocks << block | |
119 | + block.save! | |
120 | + | |
121 | + 4.times do # to keep a minimal chance of false positive, its random after all | |
122 | + assert_equivalent [p1, p2, p3, p4], block.products | |
123 | + end | |
124 | + end | |
125 | + | |
110 | 126 | end | ... | ... |