diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index 4852e17a5c..2924c7eaea 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -4,8 +4,9 @@ Spree::LineItem.class_eval do include OpenFoodNetwork::VariantAndLineItemNaming has_and_belongs_to_many :option_values, join_table: 'spree_option_values_line_items', class_name: 'Spree::OptionValue' - attr_accessible :max_quantity, :final_weight_volume - attr_accessible :final_weight_volume, :price, :as => :api + attr_accessible :max_quantity, :final_weight_volume, :price, :as => :api + + before_create :inherit_units_from_variant after_save :update_units delegate :unit_description, to: :variant @@ -68,6 +69,14 @@ Spree::LineItem.class_eval do def unit_value return 0 if quantity == 0 - final_weight_volume / quantity unless final_weight_volume.nil? + (final_weight_volume || 0) / quantity + end + + private + + def inherit_units_from_variant + if final_weight_volume || variant.andand.unit_value + self.final_weight_volume = final_weight_volume || ((variant.andand.unit_value) * quantity) + end end end diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index 4e1ae0736c..16dae08d8a 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -130,12 +130,6 @@ Spree::Order.class_eval do else current_item = Spree::LineItem.new(:quantity => quantity, max_quantity: max_quantity) current_item.variant = variant - current_item.option_values = variant.option_values - if variant.unit_value - current_item.final_weight_volume = variant.unit_value * quantity - else - current_item.final_weight_volume = 0 - end if currency current_item.currency = currency unless currency.nil? current_item.price = variant.price_in(currency).amount diff --git a/app/models/spree/variant_decorator.rb b/app/models/spree/variant_decorator.rb index c16a5ab5e9..01145eb24f 100644 --- a/app/models/spree/variant_decorator.rb +++ b/app/models/spree/variant_decorator.rb @@ -69,7 +69,7 @@ Spree::Variant.class_eval do name = product.name name += " - #{name_to_display}" if name_to_display != product.name - name += " (#{options_text})" if options_text + name += " (#{unit_text})" if unit_text name end @@ -94,6 +94,6 @@ Spree::Variant.class_eval do private def update_weight_from_unit_value - self.weight = weight_from_unit_value + self.weight = weight_from_unit_value if self.product.variant_unit == 'weight' && unit_value.present? end end diff --git a/lib/open_food_network/variant_and_line_item_naming.rb b/lib/open_food_network/variant_and_line_item_naming.rb index 33d93bf5f3..f882470326 100644 --- a/lib/open_food_network/variant_and_line_item_naming.rb +++ b/lib/open_food_network/variant_and_line_item_naming.rb @@ -57,7 +57,7 @@ module OpenFoodNetwork end def weight_from_unit_value - unit_value / 1000 if self.product.variant_unit == 'weight' && unit_value.present? + (unit_value || 0) / 1000 end private diff --git a/spec/features/admin/variants_spec.rb b/spec/features/admin/variants_spec.rb index 0d3003ad02..528481b32b 100644 --- a/spec/features/admin/variants_spec.rb +++ b/spec/features/admin/variants_spec.rb @@ -68,7 +68,7 @@ feature %q{ within "tr#spree_variant_#{v.id}" do page.find('a.delete-resource').click end - page.should_not have_content v.unit_text + page.should_not have_selector "tr#spree_variant_#{v.id}" v.reload v.deleted_at.should_not be_nil diff --git a/spec/lib/open_food_network/order_and_distributor_report_spec.rb b/spec/lib/open_food_network/order_and_distributor_report_spec.rb index ab882239d4..154e697ae0 100644 --- a/spec/lib/open_food_network/order_and_distributor_report_spec.rb +++ b/spec/lib/open_food_network/order_and_distributor_report_spec.rb @@ -40,7 +40,7 @@ module OpenFoodNetwork table[0].should == [@order.created_at, @order.id, @bill_address.full_name, @order.email, @bill_address.phone, @bill_address.city, - @line_item.product.sku, @line_item.product.name, @line_item.variant.unit_text, @line_item.quantity, @line_item.max_quantity, @line_item.price * @line_item.quantity, @line_item.distribution_fee, + @line_item.product.sku, @line_item.product.name, @line_item.unit_text, @line_item.quantity, @line_item.max_quantity, @line_item.price * @line_item.quantity, @line_item.distribution_fee, @payment_method.name, @distributor.name, @distributor.address.address1, @distributor.address.city, @distributor.address.zipcode, @shipping_instructions ] end diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index 28cd80443a..a52ebafecf 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -80,6 +80,32 @@ module Spree end describe "unit value/description" do + describe "inheriting units" do + let!(:p) { create(:product, variant_unit: "weight", variant_unit_scale: 1, master: create(:variant, unit_value: 1000 )) } + let!(:v) { p.variants.first } + let!(:o) { create(:order) } + + describe "when no final_weight_volume is set" do + let(:li) { build(:line_item, order: o, variant: v, quantity: 3) } + + it "initializes final_weight_volume from the variant's unit_value on create" do + expect(li.final_weight_volume).to be nil + li.save + expect(li.final_weight_volume).to eq 3000 + end + end + + describe "when a final_weight_volume has been set" do + let(:li) { build(:line_item, order: o, variant: v, quantity: 3, final_weight_volume: 2000) } + + it "uses the existing value" do + expect(li.final_weight_volume).to eq 2000 + li.save + expect(li.final_weight_volume).to eq 2000 + end + end + end + describe "generating the full name" do let(:li) { LineItem.new } @@ -120,7 +146,7 @@ module Spree allow(li).to receive(:unit_description) { 'foo' } expect { - li.update_attributes!(final_weight_volume: 10) + li.update_attribute(:final_weight_volume, 10) }.to change(Spree::OptionValue, :count).by(1) li.option_values.should_not include ov_orig @@ -143,7 +169,7 @@ module Spree allow(li).to receive(:unit_description) { 'bar' } expect { - li.update_attributes!(final_weight_volume: 10) + li.update_attribute(:final_weight_volume, 10) }.to change(Spree::OptionValue, :count).by(0) li.option_values.should_not include ov_orig diff --git a/spec/models/spree/variant_spec.rb b/spec/models/spree/variant_spec.rb index 6f1b6c8056..55018828a0 100644 --- a/spec/models/spree/variant_spec.rb +++ b/spec/models/spree/variant_spec.rb @@ -122,7 +122,7 @@ module Spree before do v.stub(:product) { p } v.stub(:name_to_display) { p.name } - v.stub(:options_text) { nil } + v.stub(:unit_text) { nil } end it "returns the product name only when there's no extra info" do @@ -135,13 +135,13 @@ module Spree end it "shows the options text when present" do - v.stub(:options_text) { 'OT' } + v.stub(:unit_text) { 'OT' } v.product_and_variant_name.should == 'product (OT)' end it "displays all attributes" do v.stub(:name_to_display) { 'NTD' } - v.stub(:options_text) { 'OT' } + v.stub(:unit_text) { 'OT' } v.product_and_variant_name.should == 'product - NTD (OT)' end end