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.

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.rb
index 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 Spree
extend ActiveSupport::Concern
def 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_version
params.delete(:page) if params[:page] == 1
diff --git a/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb b/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb
index 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 do
Mobility.with_locale(:pl) do
taxonomy_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")
end
taxonomy_localized
@@ -70,7 +70,7 @@ describe 'Taxons Spec', type: :request do
taxons_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") }
end
taxons_localized
diff --git a/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb
index 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 do
include_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' do
it 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 do
end
it 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 do
id: taxon.children.second.id.to_s,
type: :taxon
}
- ]
+ )
}
}
}
diff --git a/core/app/models/spree/taxon.rb b/core/app/models/spree/taxon.rb
index 092a08b93a..a3117ff184 100644
--- a/core/app/models/spree/taxon.rb
+++ b/core/app/models/spree/taxon.rb
@@ -47,6 +47,8 @@ module Spree
after_save :touch_ancestors_and_taxonomy
after_update :sync_taxonomy_name
after_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_permalink
has_one :store, through: :taxonomy
@@ -59,17 +61,30 @@ module Spree
scope :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].freeze
translates(*TRANSLATABLE_FIELDS, column_fallback: !Spree.always_use_translations?)
self::Translation.class_eval do
alias_attribute :slug, :permalink
before_save :set_permalink
+ before_save :set_pretty_name
def set_permalink
self.permalink = generate_slug
end
+ 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
+
private
def generate_slug
@@ -82,12 +97,22 @@ module Spree
end
end
+ 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(' -> ')
end
def parent
@@ -98,6 +123,11 @@ module Spree
localized_parent = parent.translations.find_by(locale: locale)
localized_parent.present? ? localized_parent.permalink : parent.permalink
end
+
+ def parent_pretty_name_with_fallback
+ localized_parent = parent.translations.find_by(locale: locale)
+ localized_parent.present? ? localized_parent.pretty_name : parent.pretty_name
+ end
end
# indicate which filters should be used for a taxon
@@ -117,13 +147,12 @@ module Spree
meta_title.blank? ? name : meta_title
end
- # 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(' -> ')
end
def generate_slug
@@ -136,15 +165,30 @@ module Spree
end
end
+ def set_permalink
+ if Spree.use_translations?
+ translations.each(&:set_permalink)
+ else
+ self.permalink = generate_slug
+ end
+ end
+
def active_products
products.active
end
- 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)
end
def cached_self_and_descendants_ids
@@ -165,6 +209,10 @@ module Spree
private
+ def should_regenerate_pretty_name_and_permalink?
+ saved_changes.key?(:name) || saved_changes.key?(:permalink)
+ end
+
def sync_taxonomy_name
if saved_changes.key?(:name) && root?
return if taxonomy.name.to_s == name.to_s
diff --git a/core/app/models/spree/taxonomy.rb b/core/app/models/spree/taxonomy.rb
index 3ce1a5858b..93c8f7a052 100644
--- a/core/app/models/spree/taxonomy.rb
+++ b/core/app/models/spree/taxonomy.rb
@@ -2,6 +2,7 @@ module Spree
class Taxonomy < Spree::Base
include Spree::TranslatableResource
include Spree::Metadata
+ include Spree::SingleStoreResource
if defined?(Spree::Webhooks::HasWebhooks)
include Spree::Webhooks::HasWebhooks
end
diff --git a/core/db/migrate/20240822163534_add_pretty_name_to_spree_taxons.rb b/core/db/migrate/20240822163534_add_pretty_name_to_spree_taxons.rb
new file mode 100644
index 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
+end
diff --git a/core/spec/models/spree/taxon_spec.rb b/core/spec/models/spree/taxon_spec.rb
index 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 do
end
it "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)
end
end
@@ -282,4 +282,60 @@ describe Spree::Taxon, type: :model do
end
end
end
+
+ 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
+ end
end