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
Added `Variant#options` method
Damian Legawiec1bdbc81534d0
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex b7dee21b67..5fff956f93 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -172,6 +172,18 @@ module Spree!!deleted_atend+ # 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+ end+def options=(options = {})options.each do |option|next if option[:name].blank? || option[:value].blank?diff --git a/core/app/presenters/spree/variants/options_presenter.rb b/core/app/presenters/spree/variants/options_presenter.rbindex d740689807..55f47441d9 100644--- a/core/app/presenters/spree/variants/options_presenter.rb+++ b/core/app/presenters/spree/variants/options_presenter.rb@@ -19,10 +19,22 @@ module Spreejoin_options(options)end+ def to_hash+ options = option_values+ options = sort_options(options)+ options = present_options_as_hash(options)++ join_hash_options(options)+ end+privatedef sort_options(options)- options.sort_by { |o| o.option_type.position }+ if options.first&.association(:option_type)&.loaded?+ options.sort_by { |o| o.option_type.position }+ else+ options.includes(:option_type).sort_by { |o| o.option_type.position }+ endenddef present_options(options)@@ -34,7 +46,7 @@ module Spreeenddef present_color_option(option)- "#{option.option_type.presentation}: #{option.name}"+ "#{option.option_type.presentation}: #{option.presentation}"enddef present_option(option)@@ -44,6 +56,26 @@ module Spreedef join_options(options)options.to_sentence(words_connector: WORDS_CONNECTOR, two_words_connector: WORDS_CONNECTOR)end++ def present_options_as_hash(options)+ options.map do |ov|+ method = "present_#{ov.option_type.name}_option_as_hash"++ respond_to?(method, true) ? send(method, ov) : present_option_as_hash(ov)+ end+ end++ def present_option_as_hash(option)+ {}.tap do |hash|+ hash.store(option.option_type.presentation.downcase, option.presentation)+ end+ end++ def join_hash_options(options)+ return {} if options.empty?++ options.inject(:merge).symbolize_keys+ endendendenddiff --git a/core/spec/presenters/spree/variants/options_presenter_spec.rb b/core/spec/presenters/spree/variants/options_presenter_spec.rbindex 67e745e306..396ade05e2 100644--- a/core/spec/presenters/spree/variants/options_presenter_spec.rb+++ b/core/spec/presenters/spree/variants/options_presenter_spec.rb@@ -16,15 +16,30 @@ describe Spree::Variants::OptionsPresenter doit 'orders by bar than foo' doexpect(to_sentence).to eq 'Bar Type: Bar, Foo Type: Foo'end+ end++ describe '#to_hash' do+ subject { described_class.new(variant).to_hash }++ let(:option_type_1) { create :option_type, name: 'color', presentation: 'Color' }+ let(:option_value_1) { create :option_value, option_type: option_type_1, name: 'white', presentation: 'White' }+ let(:option_type_2) { create :option_type, name: 'size', presentation: 'Size' }+ let(:option_value_2) { create :option_value, option_type: option_type_2, name: 'Medium', presentation: 'Medium' }- context 'when OptionType is color' do- let(:option_type) { create :option_type, name: 'color', presentation: 'Color' }- let(:option_value) { create :option_value, option_type: option_type, name: 'white', presentation: '#FFFFFF' }+ context 'when variant has option values' do+ let(:variant) { create :variant, option_values: [option_value_1, option_value_2] }++ it 'returns valid hash' do+ expect(subject).to eq({ color: 'White', size: 'Medium' })+ end+ end- let(:variant) { build :variant, option_values: [option_value] }+ context 'when variant has no option values' do+ let(:product) { create(:product) }+ let(:variant) { product.master }- it 'uses name of OptionValue instead of presentation' do- expect(to_sentence).to eq 'Color: white'+ it 'returns empty hash' do+ expect(subject).to eq({})endendend