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
Open source more Taxon and Store stuff (#12361)
Łukasz Adamczyk26261447b282
diff --git a/core/app/models/spree/page_sections/featured_taxon.rb b/core/app/models/spree/page_sections/featured_taxon.rbindex 4fc9156457..718e8fdf0e 100644--- a/core/app/models/spree/page_sections/featured_taxon.rb+++ b/core/app/models/spree/page_sections/featured_taxon.rb@@ -2,7 +2,7 @@ module Spreemodule PageSectionsclass FeaturedTaxon < Spree::PageSectionscope :by_taxon_id, lambda { |taxon_ids|- regexp = [*taxon_ids].map { |taxon_id| "taxon_id: #{taxon_id}" }.join('|')+ regexp = [*taxon_ids].map { |taxon_id| "taxon_id: '#{taxon_id}'" }.join('|')where("#{Spree::PageSections::FeaturedTaxon.table_name}.preferences ~ ?", regexp)}diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rbindex 88ddf1cc43..acf388130d 100644--- a/core/app/models/spree/store.rb+++ b/core/app/models/spree/store.rb@@ -169,6 +169,7 @@ module Spreebefore_save :ensure_supported_currencies, :ensure_supported_localesafter_create :ensure_default_taxonomies_are_createdafter_create :ensure_default_automatic_taxons+ after_create :ensure_default_post_categories_are_createdafter_create :import_products_from_store, if: -> { import_products_from_store_id.present? }after_create :import_payment_methods_from_store, if: -> { import_payment_methods_from_store_id.present? }after_create :create_default_theme@@ -462,6 +463,12 @@ module Spreeendend+ def ensure_default_post_categories_are_created+ post_categories.find_or_create_by(title: Spree.t('default_post_categories.resources'))+ post_categories.find_or_create_by(title: Spree.t('default_post_categories.articles'))+ post_categories.find_or_create_by(title: Spree.t('default_post_categories.news'))+ end+# code is slug, so we don't want to generate new slug when code changes# we use friendlyId only for history featuredef should_generate_new_friendly_id?diff --git a/core/app/models/spree/taxon.rb b/core/app/models/spree/taxon.rbindex 08320e454c..998f8c014b 100644--- a/core/app/models/spree/taxon.rb+++ b/core/app/models/spree/taxon.rb@@ -83,6 +83,10 @@ module Spreeafter_move :regenerate_pretty_name_and_permalinkafter_move :regenerate_translations_pretty_name_and_permalink+ after_commit :touch_featured_sections, on: [:update]+ after_touch :touch_featured_sections+ after_destroy :remove_featured_sections, if: -> { featured? }+## Scopes#@@ -384,6 +388,14 @@ module SpreeSpree::Core::Engine.routes.url_helpers.nested_taxons_path(self)end+ def featured?+ featured_sections.any?+ end++ def featured_sections+ @featured_sections ||= Spree::PageSections::FeaturedTaxon.published.by_taxon_id(id)+ end+privatedef should_regenerate_pretty_name_and_permalink?@@ -424,5 +436,13 @@ module Spreedef regenerate_translations_pretty_name_and_permalinktranslations.each(&:regenerate_pretty_name_and_permalink)end++ def touch_featured_sections+ Spree::Taxons::TouchFeaturedSections.call(taxon_ids: [id])+ end++ def remove_featured_sections+ featured_sections.destroy_all+ endendenddiff --git a/core/app/services/spree/products/auto_match_taxons.rb b/core/app/services/spree/products/auto_match_taxons.rbindex aaabbb4b91..790a06ca1e 100644--- a/core/app/services/spree/products/auto_match_taxons.rb+++ b/core/app/services/spree/products/auto_match_taxons.rb@@ -41,10 +41,7 @@ module Spreeall_affected_taxons = (taxons_to_remove + taxons_to_add).uniqif all_affected_taxons.any?- if defined?(Spree::Taxons::TouchFeaturedSections)- Spree::Taxons::TouchFeaturedSections.call(taxon_ids: all_affected_taxons.pluck(:id))- end-+ Spree::Taxons::TouchFeaturedSections.call(taxon_ids: all_affected_taxons.pluck(:id))product.touchenddiff --git a/core/app/services/spree/taxons/touch_featured_sections.rb b/core/app/services/spree/taxons/touch_featured_sections.rbnew file mode 100644index 0000000000..69bb30291a--- /dev/null+++ b/core/app/services/spree/taxons/touch_featured_sections.rb@@ -0,0 +1,21 @@+module Spree+ module Taxons+ class TouchFeaturedSections+ prepend Spree::ServiceModule::Base++ def call(taxon_ids:)+ return if taxon_ids.empty?++ featured_taxons = Spree::PageSections::FeaturedTaxon.published.by_taxon_id(taxon_ids)++ return if featured_taxons.empty?++ featured_taxons.touch_all+ pages = Spree::Page.where(id: featured_taxons.where(pageable_type: 'Spree::Page').pluck(:pageable_id))+ pages.touch_all+ themes = Spree::Theme.where(id: pages.where(pageable_type: 'Spree::Theme').pluck(:pageable_id).uniq)+ themes.touch_all+ end+ end+ end+enddiff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex 5b9af2e5fa..fe27ada85d 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -784,6 +784,10 @@ en:default: Defaultdefault_country_cannot_be_deleted: Default country cannot be deleteddefault_currency: Default currency+ default_post_categories:+ articles: Articles+ news: News+ resources: Resourcesdefault_refund_amount: Default Refund Amountdefault_tax: Default Taxdefault_tax_zone: Default Tax Zonediff --git a/core/spec/models/spree/page_section_spec.rb b/core/spec/models/spree/page_section_spec.rbindex f10262790e..6c9f9f94a1 100644--- a/core/spec/models/spree/page_section_spec.rb+++ b/core/spec/models/spree/page_section_spec.rb@@ -82,7 +82,7 @@ RSpec.describe Spree::PageSection, type: :model dolet!(:section) { Spree::PageSections::Footer.create!(pageable: theme) }let(:page_blocks_count) { 4 }- let(:page_links_count) { 8 }+ let(:page_links_count) { 7 }it_behaves_like 'deep clone'enddiff --git a/core/spec/models/spree/page_sections/header_spec.rb b/core/spec/models/spree/page_sections/header_spec.rbindex 67eb61e466..b486bb75a9 100644--- a/core/spec/models/spree/page_sections/header_spec.rb+++ b/core/spec/models/spree/page_sections/header_spec.rb@@ -5,8 +5,8 @@ describe Spree::PageSections::Header dolet(:section) { Spree::PageSections::Header.first }it 'should be created with default links' do- expect(section.links.count).to eq(5)- expect(section.links.map(&:label)).to contain_exactly('Shop All', 'Brands', 'On sale', 'New arrivals', 'Blog')+ expect(section.links.count).to eq(4)+ expect(section.links.map(&:label)).to contain_exactly('Shop All', 'On sale', 'New arrivals', 'Blog')endendenddiff --git a/core/spec/models/spree/taxon_spec.rb b/core/spec/models/spree/taxon_spec.rbindex d553aa87e9..52674fd6ef 100644--- a/core/spec/models/spree/taxon_spec.rb+++ b/core/spec/models/spree/taxon_spec.rb@@ -124,6 +124,16 @@ describe Spree::Taxon, type: :model doendendend++ describe 'after_destroy :remove_all_featured_sections' do+ let(:taxon) { create(:taxon) }+ let!(:featured_section) { create(:featured_taxon, preferred_taxon_id: taxon.id) }++ it 'removes the associated featured section' do+ expect { taxon.destroy! }.to change(Spree::PageSections::FeaturedTaxon, :count).from(3).to(2)+ expect(featured_section.reload).to be_deleted+ end+ endendcontext 'when using another locale' do@@ -753,4 +763,44 @@ describe Spree::Taxon, type: :model doendendend++ describe '#featured?' do+ subject { taxon.featured? }++ let(:taxon) { create(:taxon) }+ let!(:featured_section) { create(:featured_taxon, preferred_taxon_id: featured_taxon.id) }++ context 'with a featured section' do+ let(:featured_taxon) { taxon }++ it { is_expected.to be(true) }+ end++ context 'with no featured section' do+ let(:featured_taxon) { create(:taxon) }++ it { is_expected.to be(false) }+ end+ end++ describe '#featured_sections' do+ subject { taxon.featured_sections }++ let(:taxon) { create(:taxon) }++ let!(:featured_sections) { create_list(:featured_taxon, 2, preferred_taxon_id: featured_taxon.id) }+ let!(:other_featured_sections) { create_list(:featured_taxon, 2, preferred_taxon_id: create(:taxon).id) }++ context 'with featured sections' do+ let(:featured_taxon) { taxon }++ it { is_expected.to contain_exactly(*featured_sections) }+ end++ context 'with no featured sections' do+ let(:featured_taxon) { create(:taxon) }++ it { is_expected.to be_empty }+ end+ endenddiff --git a/core/spec/services/spree/products/auto_match_taxons_spec.rb b/core/spec/services/spree/products/auto_match_taxons_spec.rbindex bda6e4f4ed..f49bbb9fa4 100644--- a/core/spec/services/spree/products/auto_match_taxons_spec.rb+++ b/core/spec/services/spree/products/auto_match_taxons_spec.rb@@ -45,4 +45,21 @@ RSpec.describe Spree::Products::AutoMatchTaxons doexpect(taxon.products.reload).not_to include(product)endend++ context 'for a featured taxon' do+ let!(:taxon) { create(:automatic_taxon) }+ let!(:featured_taxon) { Spree::PageSections::FeaturedTaxon.create!(pageable: Spree::Page.find_by(name: 'Homepage')) }++ before do+ create(:tag_taxon_rule, taxon: taxon, value: 'cruelty-free')+ product.tag_list.add('cruelty-free')+ product.save++ featured_taxon.update!(preferred_taxon_id: taxon.id)+ end++ it 'refreshes the featured taxon' do+ expect { subject }.to change { featured_taxon.reload.updated_at }+ end+ endend