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.

Use parameterized option type/values names in getters/setters used in… (#12164)

Damian Legawiecd8691642c2e4
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index bc66ed8e45..fd09bfd1a2 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -212,28 +212,32 @@ module Spree
# Returns an array of hashes with the option type name, value and presentation
# @return [Array<Hash>]
def options
- option_values.includes(:option_type).map do |option_value|
- {
- name: option_value.option_type.name,
- value: option_value.name,
- presentation: option_value.presentation
- }
- end
+ @options ||= option_values.
+ includes(option_type: :product_option_types).
+ merge(product.product_option_types).
+ reorder('spree_product_option_types.position').
+ map do |option_value|
+ {
+ name: option_value.option_type.name,
+ value: option_value.name,
+ presentation: option_value.presentation
+ }
+ end
end
def options=(options = {})
options.each do |option|
next if option[:name].blank? || option[:value].blank?
- set_option_value(option[:name], option[:value])
+ set_option_value(option[:name], option[:value], option[:position])
end
end
- def set_option_value(opt_name, opt_value)
+ def set_option_value(opt_name, opt_value, opt_type_position = nil)
# no option values on master
return if is_master
- option_type = Spree::OptionType.where(['LOWER(name) = ?', opt_name.downcase.strip]).first_or_initialize do |o|
+ option_type = Spree::OptionType.where(name: opt_name.parameterize).first_or_initialize do |o|
o.name = o.presentation = opt_name
o.save!
end
@@ -242,16 +246,21 @@ module Spree
if current_value.nil?
# then we have to check to make sure that the product has the option type
- unless product.option_types.include? option_type
- product.option_types << option_type
- end
+ product_option_type = if (existing_prod_ot = product.product_option_types.find { |ot| ot.option_type_id == option_type.id })
+ existing_prod_ot
+ else
+ product_option_type = product.product_option_types.new
+ product_option_type.option_type = option_type
+ end
+ product_option_type.position = opt_type_position if opt_type_position
+ product_option_type.save! if product_option_type.new_record? || product_option_type.changed?
else
- return if current_value.name.downcase.strip == opt_value.downcase.strip
+ return if current_value.name.parameterize == opt_value.parameterize
option_values.delete(current_value)
end
- option_value = option_type.option_values.where(['LOWER(name) = ?', opt_value.downcase.strip]).first_or_initialize do |o|
+ option_value = option_type.option_values.where(name: opt_value.parameterize).first_or_initialize do |o|
o.name = o.presentation = opt_value
o.save!
end
@@ -261,11 +270,15 @@ module Spree
end
def find_option_value(opt_name)
- option_values.detect { |o| o.option_type.name.downcase.strip == opt_name.downcase.strip }
+ option_values.includes(:option_type).detect { |o| o.option_type.name.parameterize == opt_name.parameterize }
end
- def option_value(opt_name)
- find_option_value(opt_name).try(:presentation)
+ def option_value(option_type)
+ if option_type.is_a?(Spree::OptionType)
+ option_values.detect { |o| o.option_type_id == option_type.id }.try(:presentation)
+ else
+ find_option_value(option_type).try(:presentation)
+ end
end
def price_in(currency)
diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb
index f79421e5b5..1470a9da60 100644
--- a/core/spec/models/spree/variant_spec.rb
+++ b/core/spec/models/spree/variant_spec.rb
@@ -475,6 +475,30 @@ describe Spree::Variant, type: :model do
end
end
+ describe '#options' do
+ let(:product) { create(:product, option_types: [option_type, option_type2]) }
+ let(:variant) { create(:variant, product: product, option_values: [option_value, option_value2]) }
+ let!(:option_type2) { create(:option_type, name: "material") }
+ let!(:option_type) { Spree::OptionType.find_by(name: "size") || create(:option_type, name: "size") }
+ let(:option_value) { create(:option_value, name: "medium", presentation: "M", option_type: option_type) }
+ let(:option_value2) { create(:option_value, name: "wool", presentation: "Wool", option_type: option_type2) }
+
+ it "returns an array of hashes with option type name, value, and presentation orderd by option type position" do
+ expect(variant.options).to eq([
+ {
+ name: "size",
+ value: "medium",
+ presentation: "M"
+ },
+ {
+ name: "material",
+ value: "wool",
+ presentation: "Wool"
+ }
+ ])
+ end
+ end
+
describe '#options_text' do
let(:variant) { build :variant }
let(:fake_presenter) { double :fake_presenter }