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
Make `Taxon#pretty_name` persistent in the database (#12129)
Damian Legawiecff4fbe1a6722
diff --git a/api/app/controllers/concerns/spree/api/v2/caching.rb b/api/app/controllers/concerns/spree/api/v2/caching.rbindex 4036a26004..d0e88b1874 100644--- a/api/app/controllers/concerns/spree/api/v2/caching.rb+++ b/api/app/controllers/concerns/spree/api/v2/caching.rb@@ -5,8 +5,7 @@ module Spreeextend ActiveSupport::Concerndef collection_cache_key(collection)- # Calling reselect('*') is a workaround for the joined translations table- collection_cache_key = collection.unscope(:includes).unscope(:order).unscope(:group).reselect('*').cache_key_with_version+ collection_cache_key = collection.unscope(:includes).unscope(:order).unscope(:group).cache_key_with_versionparams.delete(:page) if params[:page] == 1diff --git a/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb b/api/spec/requests/spree/api/v2/storefront/taxons_spec.rbindex 4c8fb5c4bd..637d79c600 100644--- a/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb+++ b/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb@@ -60,7 +60,7 @@ describe 'Taxons Spec', type: :request doMobility.with_locale(:pl) dotaxonomy_localized.update!(name: "Localized Taxonomy PL")- taxonomy_root_localized.update!(name: "Localized Taxonomy PL")+ taxonomy_root_localized.update!(name: "Localized Taxonomy PL", pretty_name: "Localized Taxonomy PL")endtaxonomy_localized@@ -70,7 +70,7 @@ describe 'Taxons Spec', type: :request dotaxons_localized = create_list(:taxon, 2, taxonomy: taxonomy, parent: taxonomy.root)taxons_localized.each_with_index do |taxon, index|name = taxon.name- Mobility.with_locale(:pl) { taxon.update!(name: "Localized Taxon #{index + 1} PL") }+ Mobility.with_locale(:pl) { taxon.update!(name: "Localized Taxon #{index + 1} PL", pretty_name: "Localized Taxon #{index + 1} PL") }endtaxons_localizeddiff --git a/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rbindex 323099a6a3..346ec54af2 100644--- a/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb+++ b/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb@@ -3,17 +3,17 @@ require 'spec_helper'describe Spree::Api::V2::Platform::TaxonSerializer doinclude_context 'API v2 serializers params'- subject { described_class.new(taxon, params: serializer_params).serializable_hash }+ subject { described_class.new(taxon.reload, params: serializer_params).serializable_hash }let(:taxonomy) { create(:taxonomy, store: store) }- let(:taxon) { create(:taxon, products: create_list(:product, 2, stores: [store]), taxonomy: taxonomy) }- let!(:children) { [create(:taxon, parent: taxon, taxonomy: taxonomy), create(:taxon, parent: taxon, taxonomy: taxonomy)] }+ let(:taxon) { create(:taxon, taxonomy: taxonomy, products: create_list(:product, 2, stores: [store])) }+ let!(:children) { create_list(:taxon, 2, taxonomy: taxonomy, parent: taxon) }it { expect(subject).to be_kind_of(Hash) }context 'without products' doit do- expect(subject).to eq(+ expect(subject).to match({data: {id: taxon.id.to_s,@@ -83,7 +83,7 @@ describe Spree::Api::V2::Platform::TaxonSerializer doendit do- expect(subject).to eq(+ expect(subject).to match({data: {id: taxon.id.to_s,@@ -141,7 +141,7 @@ describe Spree::Api::V2::Platform::TaxonSerializer do]},children: {- data: [+ data: contain_exactly({id: taxon.children.first.id.to_s,type: :taxon@@ -150,7 +150,7 @@ describe Spree::Api::V2::Platform::TaxonSerializer doid: taxon.children.second.id.to_s,type: :taxon}- ]+ )}}}diff --git a/core/app/models/spree/taxon.rb b/core/app/models/spree/taxon.rbindex 092a08b93a..a3117ff184 100644--- a/core/app/models/spree/taxon.rb+++ b/core/app/models/spree/taxon.rb@@ -47,6 +47,8 @@ module Spreeafter_save :touch_ancestors_and_taxonomyafter_update :sync_taxonomy_nameafter_touch :touch_ancestors_and_taxonomy+ after_commit :regenerate_pretty_name_and_permalink, on: :update, if: :should_regenerate_pretty_name_and_permalink?+ after_move :regenerate_pretty_name_and_permalinkhas_one :store, through: :taxonomy@@ -59,17 +61,30 @@ module Spreescope :for_stores, ->(stores) { joins(:taxonomy).where(spree_taxonomies: { store_id: stores.ids }) }- TRANSLATABLE_FIELDS = %i[name description permalink].freeze+ TRANSLATABLE_FIELDS = %i[name pretty_name description permalink].freezetranslates(*TRANSLATABLE_FIELDS, column_fallback: !Spree.always_use_translations?)self::Translation.class_eval doalias_attribute :slug, :permalinkbefore_save :set_permalink+ before_save :set_pretty_namedef set_permalinkself.permalink = generate_slugend+ def set_pretty_name+ self[:pretty_name] = generate_pretty_name+ end++ def name_with_fallback+ name.blank? ? translated_model.name : name+ end++ def pretty_name_with_fallback+ pretty_name.blank? ? translated_model.pretty_name : pretty_name+ end+privatedef generate_slug@@ -82,12 +97,22 @@ module Spreeendend+ def generate_pretty_name+ if parent.present?+ generate_pretty_name_including_parent+ elsif pretty_name.blank?+ pretty_name_with_fallback+ else+ pretty_name+ end+ end+def generate_permalink_including_parent[parent_permalink_with_fallback, (permalink.blank? ? name_with_fallback.to_url : permalink.split('/').last.to_url)].join('/')end- def name_with_fallback- name.blank? ? translated_model.name : name+ def generate_pretty_name_including_parent+ [parent_pretty_name_with_fallback, (pretty_name.blank? ? name_with_fallback : pretty_name)].compact.join(' -> ')enddef parent@@ -98,6 +123,11 @@ module Spreelocalized_parent = parent.translations.find_by(locale: locale)localized_parent.present? ? localized_parent.permalink : parent.permalinkend++ def parent_pretty_name_with_fallback+ localized_parent = parent.translations.find_by(locale: locale)+ localized_parent.present? ? localized_parent.pretty_name : parent.pretty_name+ endend# indicate which filters should be used for a taxon@@ -117,13 +147,12 @@ module Spreemeta_title.blank? ? name : meta_titleend- # Creates permalink base for friendly_id- def set_permalink- if Spree.use_translations?- translations.each(&:set_permalink)- else- self.permalink = generate_slug- end+ def set_pretty_name+ self[:pretty_name] = generate_pretty_name+ end++ def generate_pretty_name+ [parent&.pretty_name, name].compact.join(' -> ')enddef generate_slug@@ -136,15 +165,30 @@ module Spreeendend+ def set_permalink+ if Spree.use_translations?+ translations.each(&:set_permalink)+ else+ self.permalink = generate_slug+ end+ end+def active_productsproducts.activeend- def pretty_name- ancestor_chain = ancestors.inject('') do |name, ancestor|- name += "#{ancestor.name} -> "- end- ancestor_chain + name.to_s+ def regenerate_pretty_name_and_permalink+ set_permalink+ update_columns(pretty_name: generate_pretty_name, updated_at: Time.current)++ children.reload.each(&:regenerate_pretty_name_and_permalink_as_child)+ end++ def regenerate_pretty_name_and_permalink_as_child+ set_permalink+ update_columns(pretty_name: generate_pretty_name, updated_at: Time.current)++ children.reload.each(&:regenerate_pretty_name_and_permalink_as_child)enddef cached_self_and_descendants_ids@@ -165,6 +209,10 @@ module Spreeprivate+ def should_regenerate_pretty_name_and_permalink?+ saved_changes.key?(:name) || saved_changes.key?(:permalink)+ end+def sync_taxonomy_nameif saved_changes.key?(:name) && root?return if taxonomy.name.to_s == name.to_sdiff --git a/core/app/models/spree/taxonomy.rb b/core/app/models/spree/taxonomy.rbindex 3ce1a5858b..93c8f7a052 100644--- a/core/app/models/spree/taxonomy.rb+++ b/core/app/models/spree/taxonomy.rb@@ -2,6 +2,7 @@ module Spreeclass Taxonomy < Spree::Baseinclude Spree::TranslatableResourceinclude Spree::Metadata+ include Spree::SingleStoreResourceif defined?(Spree::Webhooks::HasWebhooks)include Spree::Webhooks::HasWebhooksenddiff --git a/core/db/migrate/20240822163534_add_pretty_name_to_spree_taxons.rb b/core/db/migrate/20240822163534_add_pretty_name_to_spree_taxons.rbnew file mode 100644index 0000000000..dee15d3345--- /dev/null+++ b/core/db/migrate/20240822163534_add_pretty_name_to_spree_taxons.rb@@ -0,0 +1,9 @@+class AddPrettyNameToSpreeTaxons < ActiveRecord::Migration[6.1]+ def change+ add_column :spree_taxons, :pretty_name, :string, null: true, if_not_exists: true+ add_index :spree_taxons, :pretty_name, if_not_exists: true++ add_column :spree_taxon_translations, :pretty_name, :string, null: true, if_not_exists: true+ add_index :spree_taxon_translations, :pretty_name, if_not_exists: true+ end+enddiff --git a/core/spec/models/spree/taxon_spec.rb b/core/spec/models/spree/taxon_spec.rbindex a69e3df440..91ff187a07 100644--- a/core/spec/models/spree/taxon_spec.rb+++ b/core/spec/models/spree/taxon_spec.rb@@ -261,7 +261,7 @@ describe Spree::Taxon, type: :model doendit "sets the slugs in slug format" do- expect(root_taxon.localized_slugs_for_store(store)).to match(expected_slugs)+ expect(root_taxon.reload.localized_slugs_for_store(store)).to match(expected_slugs)endend@@ -282,4 +282,60 @@ describe Spree::Taxon, type: :model doendendend++ describe '#pretty_name' do+ let!(:taxon) { create(:taxon, name: 'Category#1', taxonomy: taxonomy) }++ context '1 lvl deep' do+ it 'returns only name' do+ expect(taxon.pretty_name).to eq('Category#1')+ end+ end++ context '2+ lvl deep' do+ let(:taxon_parent) { create(:taxon, name: 'Parent', taxonomy: taxonomy) }++ before do+ taxon.parent = taxon_parent+ taxon.save!+ end++ it 'returns name and parent names' do+ expect(taxon.reload.pretty_name).to eq('Parent -> Category#1')+ end++ context 'when taxon name is "Categories"' do+ before do+ taxon_parent.name = 'Categories'+ taxon_parent.save!+ end++ it 'should still return correct pretty name' do+ expect(taxon.reload.pretty_name).to eq('Categories -> Category#1')+ end+ end++ context 'when name is updated' do+ before do+ taxon.name = 'New Name'+ taxon.save!+ end++ it 'returns the updated pretty name' do+ expect(taxon.reload.pretty_name).to eq('Parent -> New Name')+ end+ end++ context 'when parent name is updated' do+ before do+ taxon_parent.name = 'New Parent'+ taxon_parent.save!+ end++ it 'returns the updated pretty name' do+ expect(taxon.reload.pretty_name).to eq('New Parent -> Category#1')+ end+ end+ end+ endend