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.

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.rb
index 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 Spree
module PageSections
class FeaturedTaxon < Spree::PageSection
scope :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.rb
index 88ddf1cc43..acf388130d 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -169,6 +169,7 @@ module Spree
before_save :ensure_supported_currencies, :ensure_supported_locales
after_create :ensure_default_taxonomies_are_created
after_create :ensure_default_automatic_taxons
+ after_create :ensure_default_post_categories_are_created
after_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 Spree
end
end
+ 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 feature
def should_generate_new_friendly_id?
diff --git a/core/app/models/spree/taxon.rb b/core/app/models/spree/taxon.rb
index 08320e454c..998f8c014b 100644
--- a/core/app/models/spree/taxon.rb
+++ b/core/app/models/spree/taxon.rb
@@ -83,6 +83,10 @@ module Spree
after_move :regenerate_pretty_name_and_permalink
after_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 Spree
Spree::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
+
private
def should_regenerate_pretty_name_and_permalink?
@@ -424,5 +436,13 @@ module Spree
def regenerate_translations_pretty_name_and_permalink
translations.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
+ end
end
end
diff --git a/core/app/services/spree/products/auto_match_taxons.rb b/core/app/services/spree/products/auto_match_taxons.rb
index 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 Spree
all_affected_taxons = (taxons_to_remove + taxons_to_add).uniq
if 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.touch
end
diff --git a/core/app/services/spree/taxons/touch_featured_sections.rb b/core/app/services/spree/taxons/touch_featured_sections.rb
new file mode 100644
index 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
+end
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index 5b9af2e5fa..fe27ada85d 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -784,6 +784,10 @@ en:
default: Default
default_country_cannot_be_deleted: Default country cannot be deleted
default_currency: Default currency
+ default_post_categories:
+ articles: Articles
+ news: News
+ resources: Resources
default_refund_amount: Default Refund Amount
default_tax: Default Tax
default_tax_zone: Default Tax Zone
diff --git a/core/spec/models/spree/page_section_spec.rb b/core/spec/models/spree/page_section_spec.rb
index 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 do
let!(: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'
end
diff --git a/core/spec/models/spree/page_sections/header_spec.rb b/core/spec/models/spree/page_sections/header_spec.rb
index 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 do
let(: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')
end
end
end
diff --git a/core/spec/models/spree/taxon_spec.rb b/core/spec/models/spree/taxon_spec.rb
index 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 do
end
end
end
+
+ 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
+ end
end
context 'when using another locale' do
@@ -753,4 +763,44 @@ describe Spree::Taxon, type: :model do
end
end
end
+
+ 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
+ end
end
diff --git a/core/spec/services/spree/products/auto_match_taxons_spec.rb b/core/spec/services/spree/products/auto_match_taxons_spec.rb
index 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 do
expect(taxon.products.reload).not_to include(product)
end
end
+
+ 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
+ end
end