Line items inherit units from variants upon creation

This commit is contained in:
Rob Harrington
2015-09-30 13:23:54 +10:00
parent 1946bac8e4
commit eba13a0dd7
8 changed files with 48 additions and 19 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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