Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Improve preventing creation of duplicate option types/option values (#11622)
Damian Legawiecc441fea590c5
diff --git a/core/app/models/spree/option_type.rb b/core/app/models/spree/option_type.rbindex 3edff3959b..84366de9ef 100644--- a/core/app/models/spree/option_type.rb+++ b/core/app/models/spree/option_type.rb@@ -7,6 +7,7 @@ module Spreeendacts_as_list+ auto_strip_attributes :name, :presentationwith_options dependent: :destroy, inverse_of: :option_type dohas_many :option_values, -> { order(:position) }diff --git a/core/app/models/spree/option_value.rb b/core/app/models/spree/option_value.rbindex f39a84504c..ccd0623bdf 100644--- a/core/app/models/spree/option_value.rb+++ b/core/app/models/spree/option_value.rb@@ -6,7 +6,9 @@ module Spreeendbelongs_to :option_type, class_name: 'Spree::OptionType', touch: true, inverse_of: :option_values+acts_as_list scope: :option_type+ auto_strip_attributes :name, :presentationhas_many :option_value_variants, class_name: 'Spree::OptionValueVariant'has_many :variants, through: :option_value_variants, class_name: 'Spree::Variant'diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex f137f4f514..2d8323bbb6 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -170,6 +170,8 @@ module Spreedef options=(options = {})options.each do |option|+ skip if option[:name].blank? || option[:value].blank?+set_option_value(option[:name], option[:value])endend@@ -178,12 +180,12 @@ module Spree# no option values on masterreturn if is_master- option_type = Spree::OptionType.where(name: opt_name).first_or_initialize do |o|- o.presentation = opt_name+ option_type = Spree::OptionType.where(['LOWER(name) = ?', opt_name.downcase.strip]).first_or_initialize do |o|+ o.name = o.presentation = opt_nameo.save!end- current_value = option_values.detect { |o| o.option_type.name == opt_name }+ current_value = find_option_value(opt_name)if current_value.nil?# then we have to check to make sure that the product has the option type@@ -191,13 +193,13 @@ module Spreeproduct.option_types << option_typeendelse- return if current_value.name == opt_value+ return if current_value.name.downcase.strip == opt_value.downcase.stripoption_values.delete(current_value)end- option_value = Spree::OptionValue.where(option_type_id: option_type.id, name: opt_value).first_or_initialize do |o|- o.presentation = opt_value+ option_value = option_type.option_values.where(['LOWER(name) = ?', opt_value.downcase.strip]).first_or_initialize do |o|+ o.name = o.presentation = opt_valueo.save!end@@ -205,8 +207,12 @@ module Spreesaveend+ def find_option_value(opt_name)+ option_values.detect { |o| o.option_type.name.downcase.strip == opt_name.downcase.strip }+ end+def option_value(opt_name)- option_values.detect { |o| o.option_type.name == opt_name }.try(:presentation)+ find_option_value(opt_name).try(:presentation)enddef price_in(currency)diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rbindex b2d7a87491..4bdb645e7d 100644--- a/core/spec/models/spree/variant_spec.rb+++ b/core/spec/models/spree/variant_spec.rb@@ -300,7 +300,8 @@ describe Spree::Variant, type: :model domulti_variant.set_option_value('media_type', 'CD')expect do- multi_variant.set_option_value('media_type', 'DVD')+ multi_variant.set_option_value('media_type ', ' DVD ')+ multi_variant.set_option_value('Media_Type ', ' dvd ')end.not_to change(multi_variant.option_values, :count)expect do