Commit 2281745efeac390a63dbc040a0245eebee2b2b32
1 parent
93103d85
Exists in
master
and in
28 other branches
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)
Showing
3 changed files
with
17 additions
and
1 deletions
Show diff stats
app/models/input.rb
app/models/product.rb
| ... | ... | @@ -175,7 +175,7 @@ class Product < ActiveRecord::Base |
| 175 | 175 | |
| 176 | 176 | def inputs_cost |
| 177 | 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 | 179 | end |
| 180 | 180 | |
| 181 | 181 | def total_production_cost | ... | ... |
test/unit/input_test.rb
| ... | ... | @@ -177,4 +177,18 @@ class InputTest < ActiveSupport::TestCase |
| 177 | 177 | assert_equal 0.00, input.cost |
| 178 | 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 | 194 | end | ... | ... |