Use display_as as option value for variant when it is present

This commit is contained in:
Rob H
2014-06-04 17:23:11 +10:00
parent 7f2b3d62f4
commit 3240a4e08e
2 changed files with 49 additions and 16 deletions

View File

@@ -56,11 +56,19 @@ Spree::Variant.class_eval do
delete_unit_option_values
option_type = self.product.variant_unit_option_type
option_value_namer = OpenFoodNetwork::OptionValueNamer.new self
if option_type
name = option_value_namer.name
name = option_value_name
ov = Spree::OptionValue.where(option_type_id: option_type, name: name, presentation: name).first || Spree::OptionValue.create!({option_type: option_type, name: name, presentation: name}, without_protection: true)
option_values << ov
end
end
def option_value_name
if display_as.present?
display_as
else
option_value_namer = OpenFoodNetwork::OptionValueNamer.new self
option_value_namer.name
end
end
end

View File

@@ -1,4 +1,5 @@
require 'spec_helper'
require 'open_food_network/option_value_namer'
module Spree
describe Variant do
@@ -207,25 +208,49 @@ module Spree
v_orig.option_values.should include ov
end
end
end
context "when the variant already has a value set (and all required option values exist)" do
let!(:p0) { create(:simple_product, variant_unit: 'weight', variant_unit_scale: 1) }
let!(:v0) { create(:variant, product: p0, unit_value: 10, unit_description: 'foo') }
context "when the variant already has a value set (and all required option values exist)" do
let!(:p0) { create(:simple_product, variant_unit: 'weight', variant_unit_scale: 1) }
let!(:v0) { create(:variant, product: p0, unit_value: 10, unit_description: 'foo') }
let!(:p) { create(:simple_product, variant_unit: 'weight', variant_unit_scale: 1) }
let!(:v) { create(:variant, product: p, unit_value: 5, unit_description: 'bar') }
let!(:p) { create(:simple_product, variant_unit: 'weight', variant_unit_scale: 1) }
let!(:v) { create(:variant, product: p, unit_value: 5, unit_description: 'bar') }
it "removes the old option value and assigns the new one" do
ov_orig = v.option_values.last
ov_new = v0.option_values.last
it "removes the old option value and assigns the new one" do
ov_orig = v.option_values.last
ov_new = v0.option_values.last
expect {
v.update_attributes!(unit_value: 10, unit_description: 'foo')
}.to change(Spree::OptionValue, :count).by(0)
expect {
v.update_attributes!(unit_value: 10, unit_description: 'foo')
}.to change(Spree::OptionValue, :count).by(0)
v.option_values.should_not include ov_orig
v.option_values.should include ov_new
end
v.option_values.should_not include ov_orig
v.option_values.should include ov_new
end
end
context "when the variant does not have a display_as value set" do
let!(:p) { create(:simple_product, variant_unit: 'weight', variant_unit_scale: 1) }
let!(:v) { create(:variant, product: p, unit_value: 5, unit_description: 'bar', display_as: '') }
it "requests the name of the new option_value from OptionValueName" do
OpenFoodNetwork::OptionValueNamer.any_instance.should_receive(:name).exactly(1).times.and_call_original
v.update_attributes(unit_value: 10, unit_description: 'foo')
ov = v.option_values.last
ov.name.should == "10g foo"
end
end
context "when the variant has a display_as value set" do
let!(:p) { create(:simple_product, variant_unit: 'weight', variant_unit_scale: 1) }
let!(:v) { create(:variant, product: p, unit_value: 5, unit_description: 'bar', display_as: 'FOOS!') }
it "requests the name of the new option_value from OptionValueName" do
OpenFoodNetwork::OptionValueNamer.any_instance.should_not_receive(:name)
v.update_attributes!(unit_value: 10, unit_description: 'foo')
ov = v.option_values.last
ov.name.should == "FOOS!"
end
end
end