Skip to content

armgit / spree

Public Imported from GitHub
0

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.

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.rb
index 3edff3959b..84366de9ef 100644
--- a/core/app/models/spree/option_type.rb
+++ b/core/app/models/spree/option_type.rb
@@ -7,6 +7,7 @@ module Spree
end
acts_as_list
+ auto_strip_attributes :name, :presentation
with_options dependent: :destroy, inverse_of: :option_type do
has_many :option_values, -> { order(:position) }
diff --git a/core/app/models/spree/option_value.rb b/core/app/models/spree/option_value.rb
index f39a84504c..ccd0623bdf 100644
--- a/core/app/models/spree/option_value.rb
+++ b/core/app/models/spree/option_value.rb
@@ -6,7 +6,9 @@ module Spree
end
belongs_to :option_type, class_name: 'Spree::OptionType', touch: true, inverse_of: :option_values
+
acts_as_list scope: :option_type
+ auto_strip_attributes :name, :presentation
has_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.rb
index f137f4f514..2d8323bbb6 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -170,6 +170,8 @@ module Spree
def options=(options = {})
options.each do |option|
+ skip if option[:name].blank? || option[:value].blank?
+
set_option_value(option[:name], option[:value])
end
end
@@ -178,12 +180,12 @@ module Spree
# no option values on master
return 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_name
o.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 Spree
product.option_types << option_type
end
else
- return if current_value.name == opt_value
+ return if current_value.name.downcase.strip == opt_value.downcase.strip
option_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_value
o.save!
end
@@ -205,8 +207,12 @@ module Spree
save
end
+ 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)
end
def price_in(currency)
diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb
index 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 do
multi_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