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
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.rbindex 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+ }+ endenddef 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])endend- def set_option_value(opt_name, opt_value)+ def set_option_value(opt_name, opt_value, opt_type_position = nil)# no option values on masterreturn 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_nameo.save!end@@ -242,16 +246,21 @@ module Spreeif 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.parameterizeoption_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_valueo.save!end@@ -261,11 +270,15 @@ module Spreeenddef 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)+ endenddef price_in(currency)diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rbindex 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 doendend+ 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' dolet(:variant) { build :variant }let(:fake_presenter) { double :fake_presenter }