diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 45763bb280..1377ba97ba 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -102,29 +102,32 @@ Spree::Product.class_eval do end end + def variant_unit_option_type + if variant_unit.present? + option_type_name = "unit_#{variant_unit}" + option_type_presentation = variant_unit.capitalize + + Spree::OptionType.find_by_name(option_type_name) || + Spree::OptionType.create!(name: option_type_name, + presentation: option_type_presentation) + end + end + + private def set_available_on_to_now self.available_on ||= Time.now end - def update_units if variant_unit_changed? option_types.delete self.class.all_variant_unit_option_types - option_types << option_type_for_variant_unit if variant_unit.present? + option_types << variant_unit_option_type if variant_unit.present? variants.each { |v| v.delete_unit_option_values } end end - def option_type_for_variant_unit - option_type_name = "unit_#{variant_unit}" - option_type_presentation = variant_unit.capitalize - - Spree::OptionType.find_by_name(option_type_name) || - Spree::OptionType.create!(name: option_type_name, presentation: option_type_presentation) - end - def self.all_variant_unit_option_types Spree::OptionType.where('name LIKE ?', 'unit_%%') end diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 83a7a06785..517510ee46 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -396,6 +396,13 @@ module Spree end end + describe "returning the variant unit option type" do + it "returns nil when variant_unit is not set" do + p = create(:simple_product, variant_unit: nil) + p.variant_unit_option_type.should be_nil + end + end + it "finds all variant unit option types" do ot1 = create(:option_type, name: 'unit_weight', presentation: 'Weight') ot2 = create(:option_type, name: 'unit_volume', presentation: 'Volume')