Commit 8c39a04d61e6acc1232e53df865f3e1d06616e21
Committed by
Braulio Bhavamitra
1 parent
174c9bb9
Exists in
master
and in
29 other branches
Avoid reload of product's inputs
Conflicts: test/unit/product_test.rb
Showing
2 changed files
with
11 additions
and
11 deletions
Show diff stats
app/models/product.rb
@@ -236,7 +236,7 @@ class Product < ActiveRecord::Base | @@ -236,7 +236,7 @@ class Product < ActiveRecord::Base | ||
236 | 236 | ||
237 | def percentage_from_solidarity_economy | 237 | def percentage_from_solidarity_economy |
238 | se_i = t_i = 0 | 238 | se_i = t_i = 0 |
239 | - self.inputs(true).each{ |i| t_i += 1; se_i += 1 if i.is_from_solidarity_economy } | 239 | + self.inputs.each{ |i| t_i += 1; se_i += 1 if i.is_from_solidarity_economy } |
240 | t_i = 1 if t_i == 0 # avoid division by 0 | 240 | t_i = 1 if t_i == 0 # avoid division by 0 |
241 | p = case (se_i.to_f/t_i)*100 | 241 | p = case (se_i.to_f/t_i)*100 |
242 | when 0 then [0, ''] | 242 | when 0 then [0, ''] |
test/unit/product_test.rb
@@ -480,31 +480,31 @@ class ProductTest < ActiveSupport::TestCase | @@ -480,31 +480,31 @@ class ProductTest < ActiveSupport::TestCase | ||
480 | prod = fast_create(Product, :name => 'test product1', :product_category_id => @product_category.id, :profile_id => @profile.id) | 480 | prod = fast_create(Product, :name => 'test product1', :product_category_id => @product_category.id, :profile_id => @profile.id) |
481 | assert_equal 0, prod.percentage_from_solidarity_economy.first | 481 | assert_equal 0, prod.percentage_from_solidarity_economy.first |
482 | 482 | ||
483 | - create(Input, :product_id => prod.id, :product_category_id => @product_category.id, | 483 | + prod.inputs.create!(:product_id => prod.id, :product_category_id => @product_category.id, |
484 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false) | 484 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false) |
485 | assert_equal 0, prod.percentage_from_solidarity_economy.first | 485 | assert_equal 0, prod.percentage_from_solidarity_economy.first |
486 | 486 | ||
487 | - create(Input, :product_id => prod.id, :product_category_id => @product_category.id, | 487 | + prod.inputs.create!(:product_id => prod.id, :product_category_id => @product_category.id, |
488 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) | 488 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) |
489 | assert_equal 50, prod.percentage_from_solidarity_economy.first | 489 | assert_equal 50, prod.percentage_from_solidarity_economy.first |
490 | 490 | ||
491 | - create(Input, :product_id => prod.id, :product_category_id => @product_category.id, | 491 | + prod.inputs.create!(:product_id => prod.id, :product_category_id => @product_category.id, |
492 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false) | 492 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false) |
493 | assert_equal 25, prod.percentage_from_solidarity_economy.first | 493 | assert_equal 25, prod.percentage_from_solidarity_economy.first |
494 | 494 | ||
495 | - prod = fast_create(Product, :name => 'test product1', :product_category_id => @product_category.id, :profile_id => @profile.id) | ||
496 | - create(Input, :product_id => prod.id, :product_category_id => @product_category.id, | 495 | + prod = fast_create(Product, :name => 'test product1', :product_category_id => @product_category.id, :enterprise_id => @profile.id) |
496 | + prod.inputs.create!(:product_id => prod.id, :product_category_id => @product_category.id, | ||
497 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) | 497 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) |
498 | - create(Input, :product_id => prod.id, :product_category_id => @product_category.id, | 498 | + prod.inputs.create!(:product_id => prod.id, :product_category_id => @product_category.id, |
499 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) | 499 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) |
500 | - create(Input, :product_id => prod.id, :product_category_id => @product_category.id, | 500 | + prod.inputs.create!(:product_id => prod.id, :product_category_id => @product_category.id, |
501 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) | 501 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) |
502 | - create(Input, :product_id => prod.id, :product_category_id => @product_category.id, | 502 | + prod.inputs.create!(:product_id => prod.id, :product_category_id => @product_category.id, |
503 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false) | 503 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false) |
504 | assert_equal 75, prod.percentage_from_solidarity_economy.first | 504 | assert_equal 75, prod.percentage_from_solidarity_economy.first |
505 | 505 | ||
506 | - prod = fast_create(Product, :name => 'test product', :product_category_id => @product_category.id, :profile_id => @profile.id) | ||
507 | - create(Input, :product_id => prod.id, :product_category_id => @product_category.id, | 506 | + prod = fast_create(Product, :name => 'test product', :product_category_id => @product_category.id, :enterprise_id => @profile.id) |
507 | + prod.inputs.create!(:product_id => prod.id, :product_category_id => @product_category.id, | ||
508 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) | 508 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) |
509 | assert_equal 100, prod.percentage_from_solidarity_economy.first | 509 | assert_equal 100, prod.percentage_from_solidarity_economy.first |
510 | end | 510 | end |