Commit 28c2073af316434f847cbaf87773109725ad1a2f

Authored by Antonio Terceiro
2 parents efed6b83 8c39a04d

Merge branch 'avoid-reload-of-products-inputs' into 'master'

Avoid reload of products inputs

See merge request !468
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 236  
237 237 def percentage_from_solidarity_economy
238 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 240 t_i = 1 if t_i == 0 # avoid division by 0
241 241 p = case (se_i.to_f/t_i)*100
242 242 when 0 then [0, '']
... ...
test/unit/product_test.rb
... ... @@ -480,31 +480,31 @@ class ProductTest < ActiveSupport::TestCase
480 480 prod = fast_create(Product, :name => 'test product1', :product_category_id => @product_category.id, :profile_id => @profile.id)
481 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 484 :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false)
485 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 488 :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true)
489 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 492 :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false)
493 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 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 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 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 503 :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false)
504 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 508 :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true)
509 509 assert_equal 100, prod.percentage_from_solidarity_economy.first
510 510 end
... ...