Commit 2281745efeac390a63dbc040a0245eebee2b2b32

Authored by Larissa Reis
1 parent 93103d85

Only looks at relevant to price inputs in cost sum

  Creates a named_scope that filters the 'relevant to price' inputs and
  inside product model only looks at these filtered inputs to calculate
  the sum for the price composition.

(ActionItem2366)
app/models/input.rb
@@ -9,6 +9,8 @@ class Input < ActiveRecord::Base @@ -9,6 +9,8 @@ class Input < ActiveRecord::Base
9 9
10 belongs_to :unit 10 belongs_to :unit
11 11
  12 + named_scope :relevant_to_price, :conditions => { :relevant_to_price => true }
  13 +
12 include FloatHelper 14 include FloatHelper
13 15
14 def price_per_unit=(value) 16 def price_per_unit=(value)
app/models/product.rb
@@ -175,7 +175,7 @@ class Product < ActiveRecord::Base @@ -175,7 +175,7 @@ class Product < ActiveRecord::Base
175 175
176 def inputs_cost 176 def inputs_cost
177 return 0 if inputs.empty? 177 return 0 if inputs.empty?
178 - inputs.map(&:cost).inject { |sum,price| sum + price } 178 + inputs.relevant_to_price.map(&:cost).inject { |sum,price| sum + price }
179 end 179 end
180 180
181 def total_production_cost 181 def total_production_cost
test/unit/input_test.rb
@@ -177,4 +177,18 @@ class InputTest < ActiveSupport::TestCase @@ -177,4 +177,18 @@ class InputTest < ActiveSupport::TestCase
177 assert_equal 0.00, input.cost 177 assert_equal 0.00, input.cost
178 end 178 end
179 179
  180 + should 'list inputs relevants to price' do
  181 + product_category = fast_create(ProductCategory)
  182 + product = fast_create(Product, :product_category_id => product_category.id)
  183 +
  184 + i1 = Input.create!(:product => product, :product_category => product_category, :relevant_to_price => true)
  185 +
  186 + i2 = Input.create!(:product => product, :product_category => product_category, :relevant_to_price => false)
  187 +
  188 + i1.save!
  189 + i2.save!
  190 + assert_includes Input.relevant_to_price, i1
  191 + assert_not_includes Input.relevant_to_price, i2
  192 + end
  193 +
180 end 194 end