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.

refactor(core): rename taxon tables to categories and drop taxon auto-match

Łukasz Adamczyk65f528c9a646
diff --git a/spree/admin/app/controllers/spree/admin/products_controller.rb b/spree/admin/app/controllers/spree/admin/products_controller.rb
index 5a9404e13d..5bcf729380 100644
--- a/spree/admin/app/controllers/spree/admin/products_controller.rb
+++ b/spree/admin/app/controllers/spree/admin/products_controller.rb
@@ -104,7 +104,6 @@ module Spree
def bulk_remove_from_taxons
taxons = current_store.taxons.accessible_by(current_ability, :manage).where(id: params[:taxon_ids])
Spree::Taxons::RemoveProducts.call(taxons: taxons, products: bulk_collection)
- Spree::Product.bulk_auto_match_taxons(current_store, bulk_collection.ids)
handle_bulk_operation_response
end
@@ -112,7 +111,6 @@ module Spree
def bulk_add_to_taxons
taxons = current_store.taxons.accessible_by(current_ability, :manage).where(id: params[:taxon_ids])
Spree::Taxons::AddProducts.call(taxons: taxons, products: bulk_collection)
- Spree::Product.bulk_auto_match_taxons(current_store, bulk_collection.ids)
handle_bulk_operation_response
end
@@ -244,7 +242,7 @@ module Spree
private
def after_bulk_tags_change
- Spree::Product.bulk_auto_match_taxons(current_store, bulk_collection.ids)
+ Spree::Product.bulk_auto_match_collections(current_store, bulk_collection.ids)
bulk_collection.each(&:enqueue_search_index) # reindex products
end
diff --git a/spree/admin/app/views/spree/admin/classifications/create.turbo_stream.erb b/spree/admin/app/views/spree/admin/classifications/create.turbo_stream.erb
index 829bc008d3..4501d74243 100644
--- a/spree/admin/app/views/spree/admin/classifications/create.turbo_stream.erb
+++ b/spree/admin/app/views/spree/admin/classifications/create.turbo_stream.erb
@@ -1,6 +1,6 @@
<%= turbo_close_dialog %>
-<%= turbo_stream.update :product_count, @taxon.classification_count %>
+<%= turbo_stream.update :product_count, @taxon.classifications.count %>
<%= turbo_stream.replace :classifications do %>
<%= render template: 'spree/admin/classifications/index' %>
diff --git a/spree/admin/app/views/spree/admin/classifications/destroy.turbo_stream.erb b/spree/admin/app/views/spree/admin/classifications/destroy.turbo_stream.erb
index 71bab14384..1bdf7180c0 100644
--- a/spree/admin/app/views/spree/admin/classifications/destroy.turbo_stream.erb
+++ b/spree/admin/app/views/spree/admin/classifications/destroy.turbo_stream.erb
@@ -1,2 +1,2 @@
-<%= turbo_stream.update :product_count, @taxon.classification_count %>
+<%= turbo_stream.update :product_count, @taxon.classifications.count %>
<%= turbo_stream.remove spree_dom_id(@classification) %>
diff --git a/spree/admin/app/views/spree/admin/shared/sortable_tree/_taxonomy.html.erb b/spree/admin/app/views/spree/admin/shared/sortable_tree/_taxonomy.html.erb
index 66401daeb3..e30698a737 100644
--- a/spree/admin/app/views/spree/admin/shared/sortable_tree/_taxonomy.html.erb
+++ b/spree/admin/app/views/spree/admin/shared/sortable_tree/_taxonomy.html.erb
@@ -21,8 +21,8 @@
</div>
<div class="overflow-hidden text-gray-600 text-sm ms-auto">
- <% if taxon.classification_count.positive? %>
- <%= pluralize(taxon.classification_count, Spree.t(:product)) %>
+ <% if taxon.classifications.count.positive? %>
+ <%= pluralize(taxon.classifications.count, Spree.t(:product)) %>
<% end %>
</div>
diff --git a/spree/admin/app/views/spree/admin/taxons/_form.html.erb b/spree/admin/app/views/spree/admin/taxons/_form.html.erb
index e5cb747959..0abba2e676 100644
--- a/spree/admin/app/views/spree/admin/taxons/_form.html.erb
+++ b/spree/admin/app/views/spree/admin/taxons/_form.html.erb
@@ -49,7 +49,7 @@
<h5 class="card-title">
<%= Spree.t(:products) %>
<span class="badge badge-info ml-2" id="product_count">
- <%= @taxon.classification_count %>
+ <%= @taxon.classifications.count %>
</span>
</h5>
diff --git a/spree/admin/config/initializers/spree_admin_tables.rb b/spree/admin/config/initializers/spree_admin_tables.rb
index 909e7402aa..897cc34789 100644
--- a/spree/admin/config/initializers/spree_admin_tables.rb
+++ b/spree/admin/config/initializers/spree_admin_tables.rb
@@ -112,7 +112,7 @@ Rails.application.config.after_initialize do
ransack_attribute: 'taxons_id',
operators: %i[in],
search_url: ->(view_context) { view_context.spree.admin_taxons_select_options_path(format: :json) },
- method: ->(product) { product.taxons.pluck(:pretty_name).to_sentence if product.classification_count.positive? }
+ method: ->(product) { product.taxons.pluck(:pretty_name).to_sentence if product.categories_count.positive? }
# Tags - displayed as comma-separated list, filtered via autocomplete
Spree.admin.tables.products.add :tags,
diff --git a/spree/admin/spec/controllers/spree/admin/products_controller_spec.rb b/spree/admin/spec/controllers/spree/admin/products_controller_spec.rb
index 8f04436626..422ba60106 100644
--- a/spree/admin/spec/controllers/spree/admin/products_controller_spec.rb
+++ b/spree/admin/spec/controllers/spree/admin/products_controller_spec.rb
@@ -65,7 +65,7 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
'q' => {
'status_eq' => 'active',
'search' => 'anchor',
- 'classifications_taxon_id_in' => ['8b03b40e-094f-4803-8704-20ac02f9c167', '42e63c13-dbe7-483f-8237-3b0207145fed'],
+ 'classifications_category_id_in' => ['8b03b40e-094f-4803-8704-20ac02f9c167', '42e63c13-dbe7-483f-8237-3b0207145fed'],
'shipping_category_id_eq' => '8b03b40e-094f-4803-8704-20ac02f9c167',
'tags_name_in' => ['awesome'],
'deleted_at_null' => '1',
@@ -1483,8 +1483,8 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
expect(product2.reload.classifications.last.position).to eq(1)
- expect(product4.reload.classifications.find_by(taxon_id: category.id).position).to eq(2)
- expect(product4.reload.classifications.find_by(taxon_id: category2.id).position).to eq(1)
+ expect(product4.reload.classifications.find_by(category_id: category.id).position).to eq(2)
+ expect(product4.reload.classifications.find_by(category_id: category2.id).position).to eq(1)
end
context 'for empty list of taxons and products' do
@@ -1499,7 +1499,7 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
end
end
- describe 'auto matching taxons' do
+ describe 'auto matching taxons', skip: 'Legacy admin: taxon auto-matching removed in 6.0 (rule-based membership moved to Collections; admin being replaced by the SPA)' do
let(:product_ids) { [product, product2, product3, product4].pluck(:id) }
let!(:product) { create(:product, status: :active) }
@@ -1600,7 +1600,7 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
end
end
- describe 'auto matching taxons' do
+ describe 'auto matching taxons', skip: 'Legacy admin: taxon auto-matching removed in 6.0 (rule-based membership moved to Collections; admin being replaced by the SPA)' do
let(:product_ids) { [product, product2, product3, product4].pluck(:id) }
let!(:product3) { create(:product, status: :archived) }
diff --git a/spree/api/app/controllers/spree/api/v3/admin/categories/products_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/categories/products_controller.rb
index 5cea675251..3c3afdedad 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/categories/products_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/categories/products_controller.rb
@@ -57,7 +57,7 @@ module Spree
def scope
product_scope.
joins(:classifications).
- where(Spree::Classification.table_name => { taxon_id: @category.id }).
+ where(Spree::Classification.table_name => { category_id: @category.id }).
order(Spree::Classification.table_name => { position: :asc })
end
@@ -88,7 +88,7 @@ module Spree
end
def classification_for(product)
- Spree::Classification.find_by(taxon_id: @category.id, product_id: product.id)
+ Spree::Classification.find_by(category_id: @category.id, product_id: product.id)
end
end
end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
index 32687d1e6e..a0e62330ab 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
@@ -201,12 +201,11 @@ module Spree
@search_provider ||= Spree::SearchProvider::Database.new(current_store)
end
- # Mirrors `Spree::Admin::ProductsController#after_bulk_tags_change`:
- # tag changes can flip automatic-taxon matches, and `Tags::Bulk*`
+ # Tag changes can flip automatic-collection matches, and `Tags::Bulk*`
# touch records via `touch_all` (which skips `after_commit`), so the
# search index needs an explicit kick.
def after_bulk_tags_change
- Spree::Product.bulk_auto_match_taxons(current_store, bulk_collection.ids)
+ Spree::Product.bulk_auto_match_collections(current_store, bulk_collection.ids)
bulk_collection.each(&:enqueue_search_index)
end
@@ -222,7 +221,6 @@ module Spree
accessible_by(current_ability, :update).where(id: category_ids)
service.call(taxons: categories, products: bulk_collection)
- Spree::Product.bulk_auto_match_taxons(current_store, bulk_collection.ids)
render json: { product_count: bulk_collection.size, category_count: categories.size }
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/categories/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/categories/products_controller_spec.rb
index 41b0e5ac6a..1177324338 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/categories/products_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/categories/products_controller_spec.rb
@@ -10,7 +10,7 @@ RSpec.describe Spree::Api::V3::Admin::Categories::ProductsController, type: :con
before { request.headers.merge!(headers) }
def classified_ids
- Spree::Classification.where(taxon_id: category.id).order(:position).pluck(:product_id)
+ Spree::Classification.where(category_id: category.id).order(:position).pluck(:product_id)
end
describe 'GET #index' do
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb
index 86f89f8b48..f8e0eb4614 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb
@@ -1427,55 +1427,6 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
expect(category.reload.updated_at).to be > category_old_updated_at
end
- # Legacy spec coverage: `bulk_auto_match_taxons` only enqueues jobs for
- # products that are non-deleted and non-archived. Two active products
- # should fire two jobs; archived + soft-deleted siblings are skipped.
- describe 'auto matching taxons' do
- let!(:active_a) { create(:product, status: :active) }
- let!(:active_b) { create(:product, status: :active) }
- let!(:archived) { create(:product, status: :archived) }
- let!(:soft_deleted) { create(:product, status: :draft, deleted_at: Time.current) }
-
- let(:bulk_ids) do
- [active_a, active_b, archived, soft_deleted].map(&:prefixed_id)
- end
-
- before { Spree::Taxon.delete_all }
-
- context 'on a store with automatic taxons' do
- let!(:auto_taxon) { create(:automatic_taxon) }
- let!(:plain_taxon) { create(:taxon) }
-
- it 'auto matches taxons in bulk only for live active products' do
- expect do
- post :bulk_add_to_categories, params: {
- ids: bulk_ids,
- category_ids: [plain_taxon.prefixed_id]
- }, as: :json
- end.to have_enqueued_job(Spree::Products::AutoMatchTaxonsJob)
- .on_queue(Spree.queues.taxons)
- .exactly(:twice)
-
- jobs = Spree::Products::AutoMatchTaxonsJob.queue_adapter.enqueued_jobs.last(2)
- expect(jobs.map { |job| job['arguments'] }).to contain_exactly(
- [active_a.id], [active_b.id]
- )
- end
- end
-
- context 'on a store without any automatic taxons' do
- let!(:plain_taxon) { create(:taxon) }
-
- it 'skips auto matching taxons' do
- expect do
- post :bulk_add_to_categories, params: {
- ids: bulk_ids,
- category_ids: [plain_taxon.prefixed_id]
- }, as: :json
- end.not_to have_enqueued_job(Spree::Products::AutoMatchTaxonsJob)
- end
- end
- end
end
describe 'POST #bulk_remove_from_categories' do
@@ -1565,52 +1516,6 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
expect(positions).to eq([1, 2])
end
- describe 'auto matching taxons' do
- let!(:active_a) { create(:product, status: :active) }
- let!(:active_b) { create(:product, status: :active) }
- let!(:archived) { create(:product, status: :archived) }
- let!(:soft_deleted) { create(:product, status: :draft, deleted_at: Time.current) }
-
- let(:bulk_ids) do
- [active_a, active_b, archived, soft_deleted].map(&:prefixed_id)
- end
-
- before { Spree::Taxon.delete_all }
-
- context 'on a store with automatic taxons' do
- let!(:auto_taxon) { create(:automatic_taxon) }
- let!(:plain_taxon) { create(:taxon) }
-
- it 'auto matches taxons in bulk only for live active products' do
- expect do
- post :bulk_remove_from_categories, params: {
- ids: bulk_ids,
- category_ids: [plain_taxon.prefixed_id]
- }, as: :json
- end.to have_enqueued_job(Spree::Products::AutoMatchTaxonsJob)
- .on_queue(Spree.queues.taxons)
- .exactly(:twice)
-
- jobs = Spree::Products::AutoMatchTaxonsJob.queue_adapter.enqueued_jobs.last(2)
- expect(jobs.map { |job| job['arguments'] }).to contain_exactly(
- [active_a.id], [active_b.id]
- )
- end
- end
-
- context 'on a store without any automatic taxons' do
- let!(:plain_taxon) { create(:taxon) }
-
- it 'skips auto matching taxons' do
- expect do
- post :bulk_remove_from_categories, params: {
- ids: bulk_ids,
- category_ids: [plain_taxon.prefixed_id]
- }, as: :json
- end.not_to have_enqueued_job(Spree::Products::AutoMatchTaxonsJob)
- end
- end
- end
end
describe 'POST #bulk_add_to_channels' do
@@ -1706,6 +1611,43 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
before { request.headers.merge!(headers) }
+ # Tag changes flip automatic-collection matches; after_bulk_tags_change kicks
+ # bulk_auto_match_collections, which enqueues one job per live (non-deleted,
+ # non-archived) product, and only when the store has automatic collections.
+ describe 'auto matching collections' do
+ let!(:active_a) { create(:product, status: :active) }
+ let!(:active_b) { create(:product, status: :active) }
+ let!(:archived) { create(:product, status: :archived) }
+ let!(:soft_deleted) { create(:product, status: :draft, deleted_at: Time.current) }
+
+ let(:bulk_ids) { [active_a, active_b, archived, soft_deleted].map(&:prefixed_id) }
+
+ context 'on a store with automatic collections' do
+ let!(:auto_collection) { create(:automatic_collection, store: store) }
+
+ it 'auto matches collections in bulk only for live active products' do
+ expect do
+ post :bulk_add_tags, params: { ids: bulk_ids, tags: ['summer'] }, as: :json
+ end.to have_enqueued_job(Spree::Products::AutoMatchCollectionsJob)
+ .on_queue(Spree.queues.collections)
+ .exactly(:twice)
+
+ jobs = Spree::Products::AutoMatchCollectionsJob.queue_adapter.enqueued_jobs.last(2)
+ expect(jobs.map { |job| job['arguments'] }).to contain_exactly(
+ [active_a.id], [active_b.id]
+ )
+ end
+ end
+
+ context 'on a store without any automatic collections' do
+ it 'skips auto matching collections' do
+ expect do
+ post :bulk_add_tags, params: { ids: bulk_ids, tags: ['summer'] }, as: :json
+ end.not_to have_enqueued_job(Spree::Products::AutoMatchCollectionsJob)
+ end
+ end
+ end
+
it 'adds the listed tags to every listed product' do
post :bulk_add_tags, params: {
ids: [product.prefixed_id, second_product.prefixed_id],
diff --git a/spree/core/app/finders/spree/products/find.rb b/spree/core/app/finders/spree/products/find.rb
index d7e6fddf78..8721d3c093 100644
--- a/spree/core/app/finders/spree/products/find.rb
+++ b/spree/core/app/finders/spree/products/find.rb
@@ -142,7 +142,7 @@ module Spree
def by_taxons(products)
return products unless taxons?
- products.joins(:classifications).where(Classification.table_name => { taxon_id: taxons })
+ products.joins(:classifications).where(Classification.table_name => { category_id: taxons })
end
def by_concat_taxons(products)
@@ -150,7 +150,7 @@ module Spree
product_ids = Spree::Product.
joins(:classifications).
- where(Classification.table_name => { taxon_id: concat_taxons }).
+ where(Classification.table_name => { category_id: concat_taxons }).
group("#{Spree::Product.table_name}.id").
having("COUNT(#{Spree::Product.table_name}.id) = ?", concat_taxons.length).
ids
@@ -165,7 +165,7 @@ module Spree
return products if taxon_groups.empty?
- taxonomies_products = products.joins(:classifications).where(Classification.table_name => { taxon_id: taxon_groups.flatten.uniq })
+ taxonomies_products = products.joins(:classifications).where(Classification.table_name => { category_id: taxon_groups.flatten.uniq })
# No need to filter if there is only one taxonomy
return taxonomies_products if taxonomies.size == 1
diff --git a/spree/core/app/jobs/spree/products/auto_match_taxons_job.rb b/spree/core/app/jobs/spree/products/auto_match_taxons_job.rb
deleted file mode 100644
index f4fa7f1e2a..0000000000
--- a/spree/core/app/jobs/spree/products/auto_match_taxons_job.rb
+++ /dev/null
@@ -1,14 +0,0 @@
-module Spree
- module Products
- class AutoMatchTaxonsJob < ::Spree::BaseJob
- queue_as Spree.queues.taxons
-
- def perform(product_id)
- product = Spree::Product.find_by(id: product_id)
- return unless product.present?
-
- Spree::Products::AutoMatchTaxons.call(product: product)
- end
- end
- end
-end
diff --git a/spree/core/app/models/concerns/spree/product_scopes.rb b/spree/core/app/models/concerns/spree/product_scopes.rb
index dee9d91a6e..6d56169a19 100644
--- a/spree/core/app/models/concerns/spree/product_scopes.rb
+++ b/spree/core/app/models/concerns/spree/product_scopes.rb
@@ -144,7 +144,7 @@ module Spree
# Spree::Product.in_taxon(taxon).count(distinct: true)
scope :in_taxon, ->(taxon) {
joins(:classifications).
- where("#{Classification.table_name}.taxon_id" => taxon.cached_self_and_descendants_ids).distinct
+ where("#{Classification.table_name}.category_id" => taxon.cached_self_and_descendants_ids).distinct
}
# Products in a category AND all its descendants.
@@ -171,7 +171,7 @@ module Spree
taxon_ids = records.flat_map(&:cached_self_and_descendants_ids).uniq
- joins(:classifications).where(Classification.table_name => { taxon_id: taxon_ids }).distinct
+ joins(:classifications).where(Classification.table_name => { category_id: taxon_ids }).distinct
end
# Products in a collection (flat — collections have no hierarchy).
@@ -194,7 +194,7 @@ module Spree
min_position_sql = "MIN(#{Classification.table_name}.position)"
joins(:classifications).
- where(Classification.table_name => { taxon_id: taxon_ids }).
+ where(Classification.table_name => { category_id: taxon_ids }).
select("#{Product.table_name}.*", "#{min_position_sql} AS min_taxon_position").
group("#{Product.table_name}.id").
order(Arel.sql("#{min_position_sql} ASC"))
@@ -441,7 +441,7 @@ module Spree
# specifically avoid having an order for taxon search (conflicts with main order)
def self.prepare_taxon_conditions(taxons)
ids = taxons.map(&:cached_self_and_descendants_ids).flatten.uniq
- joins(:classifications).where(Classification.table_name => { taxon_id: ids })
+ joins(:classifications).where(Classification.table_name => { category_id: ids })
end
private_class_method :prepare_taxon_conditions
diff --git a/spree/core/app/models/spree/classification.rb b/spree/core/app/models/spree/classification.rb
index fc0347ff77..136d542c7d 100644
--- a/spree/core/app/models/spree/classification.rb
+++ b/spree/core/app/models/spree/classification.rb
@@ -1,17 +1,22 @@
module Spree
class Classification < Spree.base_class
- self.table_name = 'spree_products_taxons'
- acts_as_list scope: :taxon
+ # Phase 2 transitional: table + FK column renamed (spree_products_taxons ->
+ # spree_product_categories, taxon_id -> category_id). The class rename to
+ # Spree::ProductCategory is Phase 4; the :taxon association stays, pointed at
+ # the renamed FK. The taxon-side counter_cache (classification_count) is gone —
+ # the descendant-inclusive products_count is kept up to date separately.
+ self.table_name = 'spree_product_categories'
+ acts_as_list scope: :category_id
with_options inverse_of: :classifications, touch: true do
- belongs_to :product, class_name: 'Spree::Product', counter_cache: :classification_count
- belongs_to :taxon, class_name: 'Spree::Taxon', counter_cache: :classification_count
+ belongs_to :product, class_name: 'Spree::Product', counter_cache: :categories_count
+ belongs_to :taxon, class_name: 'Spree::Taxon', foreign_key: :category_id
end
validates :taxon, :product, presence: true
validates :position, numericality: { only_integer: true, allow_blank: true, allow_nil: true }
# For #3494
- validates :taxon_id, uniqueness: { scope: :product_id, message: :already_linked, allow_blank: true }
+ validates :category_id, uniqueness: { scope: :product_id, message: :already_linked, allow_blank: true }
# Keep the taxon's descendant-inclusive products_count (and its ancestors')
# in sync on direct create/destroy. Bulk Taxons::AddProducts/RemoveProducts
@@ -19,7 +24,7 @@ module Spree
after_create :recalculate_taxon_products_count
after_destroy :recalculate_taxon_products_count
- self.whitelisted_ransackable_attributes = ['taxon_id', 'product_id']
+ self.whitelisted_ransackable_attributes = ['category_id', 'product_id']
scope :by_best_selling, lambda { |order_direction = :desc|
left_joins(product: :orders).
@@ -31,14 +36,14 @@ module Spree
}
scope :grouped_taxon_ids_for_products, lambda { |product_ids, taxon_groups|
- where(product_id: product_ids, taxon_id: taxon_groups).
+ where(product_id: product_ids, category_id: taxon_groups).
group(:product_id).
then do |query|
case ActiveRecord::Base.connection.adapter_name
when 'PostgreSQL'
- query.pluck(:product_id, Arel.sql("STRING_AGG(taxon_id::text, ',')"))
+ query.pluck(:product_id, Arel.sql("STRING_AGG(category_id::text, ',')"))
when 'Mysql2', 'SQLite'
- query.pluck(:product_id, Arel.sql('GROUP_CONCAT(taxon_id)'))
+ query.pluck(:product_id, Arel.sql('GROUP_CONCAT(category_id)'))
end
end
}
@@ -46,7 +51,7 @@ module Spree
private
def recalculate_taxon_products_count
- Spree::Taxon.recalculate_products_count(taxon_id)
+ Spree::Taxon.recalculate_products_count(category_id)
end
end
end
diff --git a/spree/core/app/models/spree/price.rb b/spree/core/app/models/spree/price.rb
index 89390832be..ea37cea693 100644
--- a/spree/core/app/models/spree/price.rb
+++ b/spree/core/app/models/spree/price.rb
@@ -70,9 +70,9 @@ module Spree
where(variant_id: Spree::Variant.search(query).select(:id))
}
- attribute :eligible_for_taxon_matching, :boolean, default: false
- before_validation -> { self.eligible_for_taxon_matching = new_record? ? discounted? : discounted? != was_discounted? }
- after_commit -> { variant&.product&.auto_match_taxons }, if: -> { eligible_for_taxon_matching? }
+ attribute :eligible_for_collection_matching, :boolean, default: false
+ before_validation -> { self.eligible_for_collection_matching = new_record? ? discounted? : discounted? != was_discounted? }
+ after_commit -> { variant&.product&.auto_match_collections }, if: -> { eligible_for_collection_matching? }
def money
Spree::Money.new(amount || 0, currency: currency.upcase)
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rb
index 6852e795f8..cac9a011aa 100644
--- a/spree/core/app/models/spree/product.rb
+++ b/spree/core/app/models/spree/product.rb
@@ -144,8 +144,7 @@ module Spree
after_save :reset_nested_changes
after_touch :touch_taxons
- after_commit :auto_match_taxons, if: :eligible_for_taxon_matching?
- after_commit :auto_match_collections, if: :eligible_for_taxon_matching?
+ after_commit :auto_match_collections, if: :eligible_for_collection_matching?
with_options length: { maximum: 255 }, allow_blank: true do
validates :meta_keywords
@@ -327,16 +326,16 @@ module Spree
after_transition to: :draft, do: [:after_draft, :send_product_drafted_webhook]
end
- def self.bulk_auto_match_taxons(store, product_ids)
- return if store.taxons.automatic.none?
+ def self.bulk_auto_match_collections(store, product_ids)
+ return if store.collections.automatic.none?
products_to_auto_match_ids = store.products.not_deleted.not_archived.where(id: product_ids).ids
- auto_match_taxons_jobs = products_to_auto_match_ids.map do |product_id|
- Spree::Products::AutoMatchTaxonsJob.new(product_id).tap { |job| job.scheduled_at = 30.seconds.from_now }
+ auto_match_collections_jobs = products_to_auto_match_ids.map do |product_id|
+ Spree::Products::AutoMatchCollectionsJob.new(product_id).tap { |job| job.scheduled_at = 30.seconds.from_now }
end
- ActiveJob.perform_all_later(auto_match_taxons_jobs)
+ ActiveJob.perform_all_later(auto_match_collections_jobs)
end
# Can't use short form block syntax due to https://github.com/Netflix/fast_jsonapi/issues/259
@@ -616,7 +615,7 @@ module Spree
# Returns the brand taxon for the product
# @return [Spree::Taxon]
def brand_taxon
- @brand_taxon ||= if classification_count.zero?
+ @brand_taxon ||= if categories_count.zero?
nil
elsif Spree.use_translations?
taxons.joins(:taxonomy).
@@ -636,7 +635,7 @@ module Spree
end
def main_taxon
- return if classification_count.zero?
+ return if categories_count.zero?
@main_taxon ||= taxons.first
end
@@ -649,7 +648,7 @@ module Spree
end
def taxons_for_store(store)
- return if classification_count.zero?
+ return if categories_count.zero?
taxons.loaded? ? taxons.find_all { |taxon| taxon.taxonomy.store_id == store.id } : taxons.for_store(store)
end
@@ -671,14 +670,6 @@ module Spree
@digital ||= shipping_category&.includes_digital_shipping_method?
end
- def auto_match_taxons
- return if deleted?
- return if archived?
- return if store.nil? || store.taxons.automatic.none?
-
- Spree::Products::AutoMatchTaxonsJob.set(wait: 30.seconds).perform_later(id)
- end
-
def auto_match_collections
return if deleted?
return if archived?
@@ -1024,7 +1015,7 @@ module Spree
true
end
- def eligible_for_taxon_matching?
+ def eligible_for_collection_matching?
previously_new_record? || tag_list_previously_changed? || available_on_previously_changed?
end
diff --git a/spree/core/app/models/spree/promotion/rules/taxon.rb b/spree/core/app/models/spree/promotion/rules/taxon.rb
index 87e4d5ee01..038822ca2d 100644
--- a/spree/core/app/models/spree/promotion/rules/taxon.rb
+++ b/spree/core/app/models/spree/promotion/rules/taxon.rb
@@ -76,7 +76,7 @@ module Spree
end
def actionable?(line_item)
- Spree::Classification.where(taxon_id: eligible_taxon_ids_including_children, product_id: line_item.product_id).exists?
+ Spree::Classification.where(category_id: eligible_taxon_ids_including_children, product_id: line_item.product_id).exists?
end
def taxon_ids_string
@@ -108,7 +108,7 @@ module Spree
# IDs of taxons in order that match rule taxons (or their children), plus all ancestors
def taxon_ids_in_order_including_ancestors(order)
# Get taxon IDs from order products that are within rule taxons or their children
- order_taxon_ids = Spree::Classification.where(product_id: order.product_ids, taxon_id: eligible_taxon_ids_including_children).pluck(:taxon_id).uniq
+ order_taxon_ids = Spree::Classification.where(product_id: order.product_ids, category_id: eligible_taxon_ids_including_children).pluck(:category_id).uniq
return [] if order_taxon_ids.empty?
diff --git a/spree/core/app/models/spree/taxon.rb b/spree/core/app/models/spree/taxon.rb
index 70710d7289..f3d8c49232 100644
--- a/spree/core/app/models/spree/taxon.rb
+++ b/spree/core/app/models/spree/taxon.rb
@@ -4,6 +4,11 @@ require 'stringex'
module Spree
class Taxon < Spree.base_class
+ # Phase 2 transitional: the table was renamed to spree_categories (the class
+ # rename to Spree::Category is Phase 4). Point the model at the renamed table
+ # so the app and the Phase 3 data migration work in the interim state.
+ self.table_name = 'spree_categories'
+
include Spree::SingleStoreResource
has_prefix_id :ctg
@@ -149,14 +154,18 @@ module Spree
#
self.whitelisted_ransackable_associations = %w[taxonomy parent]
self.whitelisted_ransackable_attributes = %w[name permalink automatic depth is_root children_count
- classification_count products_count pretty_name hide_from_nav parent_id]
+ products_count pretty_name hide_from_nav parent_id]
#
# Translations
#
TRANSLATABLE_FIELDS = %i[name pretty_name description permalink].freeze
RICH_TEXT_TRANSLATABLE_FIELDS = %i[description].freeze
- translates(*TRANSLATABLE_FIELDS, column_fallback: !Spree.always_use_translations?)
+ # Phase 2 transitional: table_name is now spree_categories, but the Mobility
+ # translation table + FK are NOT renamed until Phase 4. Pin them so Mobility
+ # doesn't derive spree_category_translations/spree_category_id from table_name.
+ translates(*TRANSLATABLE_FIELDS, column_fallback: !Spree.always_use_translations?,
+ table_name: 'spree_taxon_translations', foreign_key: 'spree_taxon_id')
#
# Action Text
@@ -176,9 +185,6 @@ module Spree
scope :manual, -> { where.not(automatic: true) }
scope :automatic, -> { where(automatic: true) }
- after_commit :regenerate_taxon_products, on: [:update], if: -> { automatic? && saved_change_to_rules_match_policy? }
- attribute :marked_for_regenerate_taxon_products, :boolean, default: true
-
def manual?
!automatic?
end
@@ -205,7 +211,7 @@ module Spree
.active
.where(
Spree::Classification.table_name => {
- taxon_id: descendants.ids + [id]
+ category_id: descendants.ids + [id]
}
)
end
@@ -224,7 +230,7 @@ module Spree
affected = changed.flat_map { |taxon| taxon.self_and_ancestors.to_a }.uniq(&:id)
affected.each do |taxon|
- count = Spree::Classification.where(taxon_id: taxon.self_and_descendants.select(:id))
+ count = Spree::Classification.where(category_id: taxon.self_and_descendants.select(:id))
.distinct.count(:product_id)
taxon.update_column(:products_count, count) if taxon.products_count != count
end
@@ -265,16 +271,6 @@ module Spree
products
end
- # we need to create a new taxon product (classification) record for each product that matches the rules
- # so we can later use them for product filtering and so on
- # if we want to fire the service once during object lifecycle - pass only_once: true
- def regenerate_taxon_products(only_once: false)
- return unless marked_for_regenerate_taxon_products?
-
- Spree::Taxons::RegenerateProducts.call(taxon: self)
- self.marked_for_regenerate_taxon_products = false if !frozen? && only_once
- end
-
def slug
permalink
end
@@ -474,7 +470,7 @@ module Spree
doomed_ids = self_and_descendants.ids
ancestors.each do |ancestor|
remaining = Spree::Classification.
- where(taxon_id: ancestor.self_and_descendants.where.not(id: doomed_ids).select(:id)).
+ where(category_id: ancestor.self_and_descendants.where.not(id: doomed_ids).select(:id)).
distinct.count(:product_id)
ancestor.update_column(:products_count, remaining) if ancestor.products_count != remaining
end
diff --git a/spree/core/app/models/spree/taxon_rule.rb b/spree/core/app/models/spree/taxon_rule.rb
index 7d4d3c3a3c..05f48ce2b3 100644
--- a/spree/core/app/models/spree/taxon_rule.rb
+++ b/spree/core/app/models/spree/taxon_rule.rb
@@ -14,14 +14,11 @@ module Spree
validates :taxon, :type, :value, presence: true
validates :match_policy, inclusion: { in: MATCH_POLICIES }, presence: true
- after_commit :regenerate_taxon_products, if: -> { saved_change_to_value? || destroyed? || saved_change_to_match_policy? }
-
delegate :store, to: :taxon
- private
-
- def regenerate_taxon_products
- taxon.regenerate_taxon_products(only_once: true)
- end
+ # Retained as a data-only model in 6.0 so the taxons -> collections data
+ # migration can read existing automatic-taxon rules; dropped in 6.1 along
+ # with the spree_taxon_rules table. Automatic membership now lives on
+ # Spree::Collection (rule matching + regeneration moved there).
end
end
diff --git a/spree/core/app/services/spree/products/auto_match_taxons.rb b/spree/core/app/services/spree/products/auto_match_taxons.rb
deleted file mode 100644
index ef2f8fb291..0000000000
--- a/spree/core/app/services/spree/products/auto_match_taxons.rb
+++ /dev/null
@@ -1,64 +0,0 @@
-module Spree
- module Products
- class AutoMatchTaxons
- prepend ::Spree::ServiceModule::Base
-
- def call(product:)
- return unless product.present?
-
- taxons_to_remove = []
- taxons_to_add = []
-
- # we need to check if existing taxons still match
- product.taxons.automatic.includes(:taxon_rules).each do |taxon|
- # products shouldn't be part of the taxon anymore
- # eg. product tags or category was changed
- taxons_to_remove << taxon unless taxon.products_matching_rules.ids.include?(product.id)
- end
-
- Spree::Classification.where(taxon: taxons_to_remove, product: product).delete_all if taxons_to_remove.any?
-
- # we need to check if product matches any existing taxons
- Spree::Taxon.automatic.includes(:taxon_rules, :products).each do |taxon|
- taxons_to_add << taxon if taxon.products.exclude?(product) && taxon.products_matching_rules.ids.include?(product.id)
- end
-
- if taxons_to_add.any?
- products_counts = Spree::Taxon.where(id: taxons_to_add.pluck(:id)).
- joins(:classifications).
- reorder('').
- group(:taxon_id).
- count(:product_id)
-
- Spree::Classification.insert_all(
- taxons_to_add.map do |taxon|
- position = products_counts[taxon.id].to_i + 1
- classification_attributes(taxon, product, position)
- end
- )
- end
-
- all_affected_taxons = (taxons_to_remove + taxons_to_add).uniq
-
- if all_affected_taxons.any? && defined?(Spree::Taxons::TouchFeaturedSections)
- Spree::Taxons::TouchFeaturedSections.call(taxon_ids: all_affected_taxons.pluck(:id))
- product.touch
- end
-
- success(product)
- end
-
- private
-
- def classification_attributes(taxon, product, position)
- {
- taxon_id: taxon.id,
- product_id: product.id,
- position: position,
- created_at: Time.current,
- updated_at: Time.current
- }
- end
- end
- end
-end
diff --git a/spree/core/app/services/spree/taxons/add_products.rb b/spree/core/app/services/spree/taxons/add_products.rb
index 4a6c941d21..dce3c21386 100644
--- a/spree/core/app/services/spree/taxons/add_products.rb
+++ b/spree/core/app/services/spree/taxons/add_products.rb
@@ -13,11 +13,11 @@ module Spree
# build the params for the insert_all
classifications_params = taxons.pluck(:id).flat_map do |taxon_id|
- position = Spree::Classification.where(taxon_id: taxon_id).count
+ position = Spree::Classification.where(category_id: taxon_id).count
products.pluck(:id).map do |product_id|
{
- taxon_id: taxon_id,
+ category_id: taxon_id,
product_id: product_id,
position: (position += 1),
created_at: Time.current,
@@ -31,7 +31,6 @@ module Spree
# update counter caches
taxon_ids = taxons.pluck(:id)
product_ids = products.pluck(:id)
- taxon_ids.each { |id| Spree::Taxon.reset_counters(id, :classifications) }
product_ids.each { |id| Spree::Product.reset_counters(id, :classifications) }
# Recompute the descendant-inclusive products_count for the taxons and
# their ancestors (bulk insert skips Classification callbacks).
diff --git a/spree/core/app/services/spree/taxons/regenerate_products.rb b/spree/core/app/services/spree/taxons/regenerate_products.rb
deleted file mode 100644
index 98bb6e43f9..0000000000
--- a/spree/core/app/services/spree/taxons/regenerate_products.rb
+++ /dev/null
@@ -1,63 +0,0 @@
-module Spree
- module Taxons
- class RegenerateProducts
- prepend ::Spree::ServiceModule::Base
-
- def call(taxon:)
- return if taxon.nil?
- return if taxon.destroyed?
- return if taxon.manual?
-
- previous_products_ids = taxon.classifications.order(position: :asc).pluck(:product_id)
-
- # https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-delete_all
- # we don't want to run destroy_all here to avoid callbacks
- # default dependent value is nullify and that won't work for us
- taxon.classifications.delete_all(:delete_all)
-
- products_matching_rules = taxon.products_matching_rules
- product_ids_to_insert = products_matching_rules.ids
-
- previous_filtered_products_ids = previous_products_ids & product_ids_to_insert
- max_products_position = previous_filtered_products_ids.size || 0
-
- if product_ids_to_insert.any?
- records_to_insert = product_ids_to_insert.map do |product_id|
- position = previous_filtered_products_ids.index(product_id)
- position = position.present? ? position + 1 : max_products_position += 1
-
- classification_attributes(product_id, taxon, position)
- end
-
- Spree::Classification.insert_all(records_to_insert)
-
- # expire product cache
- products = Spree::Product.where(id: (previous_products_ids + product_ids_to_insert).uniq)
- products.touch_all
- products.each(&:enqueue_search_index)
- end
-
- # update counter caches
- # Check if taxon still exists (may have been destroyed)
- Spree::Taxon.reset_counters(taxon.id, :classifications)
- all_product_ids = (previous_products_ids + product_ids_to_insert).uniq
- existing_product_ids = Spree::Product.where(id: all_product_ids).pluck(:id)
- existing_product_ids.each { |id| Spree::Product.reset_counters(id, :classifications) }
-
- success(taxon)
- end
-
- private
-
- def classification_attributes(product_id, taxon, position)
- {
- product_id: product_id,
- taxon_id: taxon.id,
- position: position,
- created_at: Time.current,
- updated_at: Time.current
- }
- end
- end
- end
-end
diff --git a/spree/core/app/services/spree/taxons/remove_products.rb b/spree/core/app/services/spree/taxons/remove_products.rb
index f277e5b2e9..af03fbbb24 100644
--- a/spree/core/app/services/spree/taxons/remove_products.rb
+++ b/spree/core/app/services/spree/taxons/remove_products.rb
@@ -16,16 +16,16 @@ module Spree
ApplicationRecord.transaction do
taxon_ids.each do |taxon_id|
- Spree::Classification.where(taxon_id: taxon_id, product_id: product_ids).delete_all
+ Spree::Classification.where(category_id: taxon_id, product_id: product_ids).delete_all
end
classifications_params = taxon_ids.flat_map do |taxon_id|
position = 0
- existing_product_ids = Spree::Classification.where(taxon_id: taxon_id).pluck(:product_id)
+ existing_product_ids = Spree::Classification.where(category_id: taxon_id).pluck(:product_id)
existing_product_ids.map do |product_id|
{
- taxon_id: taxon_id,
+ category_id: taxon_id,
product_id: product_id,
position: (position += 1),
created_at: Time.current,
@@ -36,7 +36,7 @@ module Spree
if classifications_params.any?
opts = {}
- opts[:unique_by] = :index_spree_products_taxons_on_product_id_and_taxon_id unless mysql_adapter?
+ opts[:unique_by] = %i[product_id category_id] unless mysql_adapter?
Spree::Classification.upsert_all(
classifications_params,
@@ -46,7 +46,6 @@ module Spree
end
# update counter caches
- taxon_ids.each { |id| Spree::Taxon.reset_counters(id, :classifications) }
product_ids.each { |id| Spree::Product.reset_counters(id, :classifications) }
# Recompute the descendant-inclusive products_count for the taxons and
# their ancestors (delete_all skips Classification callbacks).
diff --git a/spree/core/db/migrate/20260710000001_rename_taxons_to_categories.rb b/spree/core/db/migrate/20260710000001_rename_taxons_to_categories.rb
new file mode 100644
index 0000000000..40f5dcb2a2
--- /dev/null
+++ b/spree/core/db/migrate/20260710000001_rename_taxons_to_categories.rb
@@ -0,0 +1,18 @@
+class RenameTaxonsToCategories < ActiveRecord::Migration[7.2]
+ def change
+ rename_table :spree_taxons, :spree_categories
+ rename_table :spree_products_taxons, :spree_product_categories
+ rename_column :spree_product_categories, :taxon_id, :category_id
+
+ # store_id already exists on spree_taxons (shipped 5.6) and carries over on the
+ # rename — do NOT re-add it. Backfill first:
+ # bundle exec rake spree:taxons:backfill_store_id
+
+ # Category counter caches: keep the descendant-inclusive products_count (already
+ # present); drop the direct classification_count (only the legacy Rails admin
+ # read it). Rename the product-side counter to categories_count
+ # (collections_count was added additively in Phase 1).
+ remove_column :spree_categories, :classification_count, :integer, default: 0, null: false
+ rename_column :spree_products, :classification_count, :categories_count
+ end
+end
diff --git a/spree/core/lib/spree/permitted_attributes.rb b/spree/core/lib/spree/permitted_attributes.rb
index 26302aa787..e859b13c6c 100644
--- a/spree/core/lib/spree/permitted_attributes.rb
+++ b/spree/core/lib/spree/permitted_attributes.rb
@@ -102,7 +102,7 @@ module Spree
]
@@classification_attributes = [
- :position, :taxon_id, :product_id
+ :position, :category_id, :product_id
]
@@cms_page_attributes = [:title, :meta_title, :content, :meta_description, :visible, :slug, :locale]
diff --git a/spree/core/lib/tasks/products.rake b/spree/core/lib/tasks/products.rake
index 70d902ccaa..2a6f708b57 100644
--- a/spree/core/lib/tasks/products.rake
+++ b/spree/core/lib/tasks/products.rake
@@ -1,6 +1,6 @@
namespace :spree do
namespace :products do
- desc 'Reset counter caches (variant_count, classification_count, media_count) on products'
+ desc 'Reset counter caches (variant_count, categories_count, media_count) on products'
task reset_counter_caches: :environment do |_t, _args|
puts 'Resetting product counter caches...'
@@ -9,7 +9,7 @@ namespace :spree do
product.update_columns(
variant_count: product.variants.count,
- classification_count: product.classifications.count,
+ categories_count: product.classifications.count,
media_count: total_media,
updated_at: Time.current
)
diff --git a/spree/core/lib/tasks/taxons.rake b/spree/core/lib/tasks/taxons.rake
index c010c9859b..b838443525 100644
--- a/spree/core/lib/tasks/taxons.rake
+++ b/spree/core/lib/tasks/taxons.rake
@@ -58,14 +58,13 @@ namespace :spree do
puts "\nDone!"
end
- desc 'Reset counter caches (children_count, classification_count, products_count) on taxons'
+ desc 'Reset counter caches (children_count, products_count) on taxons'
task reset_counter_caches: :environment do |_t, _args|
puts 'Resetting taxon counter caches...'
Spree::Taxon.find_each do |taxon|
taxon.update_columns(
children_count: taxon.children.count,
- classification_count: taxon.classifications.count,
updated_at: Time.current
)
print '.'
diff --git a/spree/core/spec/models/spree/category_spec.rb b/spree/core/spec/models/spree/category_spec.rb
index 424ece280a..318890e48a 100644
--- a/spree/core/spec/models/spree/category_spec.rb
+++ b/spree/core/spec/models/spree/category_spec.rb
@@ -170,7 +170,7 @@ RSpec.describe Spree::Category, type: :model do
add(create(:product, stores: [store]), phones)
expect(phones.reload.products_count).to eq(1)
- expect(phones.reload.classification_count).to eq(1)
+ expect(phones.classifications.count).to eq(1) # direct
end
it 'rolls subcategory products up to ancestors' do
@@ -178,7 +178,7 @@ RSpec.describe Spree::Category, type: :model do
add(create(:product, stores: [store]), laptops)
expect(electronics.reload.products_count).to eq(2) # inclusive
- expect(electronics.reload.classification_count).to eq(0) # nothing direct
+ expect(electronics.classifications.count).to eq(0) # nothing direct
expect(phones.reload.products_count).to eq(1)
expect(laptops.reload.products_count).to eq(1)
end
diff --git a/spree/core/spec/models/spree/classification_spec.rb b/spree/core/spec/models/spree/classification_spec.rb
index fe7b7b295d..2f8d0d9727 100644
--- a/spree/core/spec/models/spree/classification_spec.rb
+++ b/spree/core/spec/models/spree/classification_spec.rb
@@ -95,45 +95,49 @@ module Spree
let(:taxon) { create(:taxon) }
let(:product) { create(:product) }
- describe 'classification_count on taxon' do
+ # The direct taxon-side classification_count was dropped in 6.0; the taxon
+ # now tracks membership via the descendant-inclusive products_count, kept in
+ # sync by the Classification create/destroy callbacks (a leaf taxon's
+ # products_count equals its direct count).
+ describe 'products_count on taxon' do
it 'increments when a classification is created' do
expect {
create(:classification, taxon: taxon, product: product)
- }.to change { taxon.reload.classification_count }.from(0).to(1)
+ }.to change { taxon.reload.products_count }.from(0).to(1)
end
it 'decrements when a classification is destroyed' do
classification = create(:classification, taxon: taxon, product: product)
expect {
classification.destroy
- }.to change { taxon.reload.classification_count }.from(1).to(0)
+ }.to change { taxon.reload.products_count }.from(1).to(0)
end
it 'correctly counts multiple classifications' do
products = create_list(:product, 3)
products.each { |p| create(:classification, taxon: taxon, product: p) }
- expect(taxon.reload.classification_count).to eq(3)
+ expect(taxon.reload.products_count).to eq(3)
end
end
- describe 'classification_count on product' do
+ describe 'categories_count on product' do
it 'increments when a classification is created' do
expect {
create(:classification, taxon: taxon, product: product)
- }.to change { product.reload.classification_count }.from(0).to(1)
+ }.to change { product.reload.categories_count }.from(0).to(1)
end
it 'decrements when a classification is destroyed' do
classification = create(:classification, taxon: taxon, product: product)
expect {
classification.destroy
- }.to change { product.reload.classification_count }.from(1).to(0)
+ }.to change { product.reload.categories_count }.from(1).to(0)
end
it 'correctly counts multiple classifications' do
taxons = create_list(:taxon, 3)
taxons.each { |t| create(:classification, taxon: t, product: product) }
- expect(product.reload.classification_count).to eq(3)
+ expect(product.reload.categories_count).to eq(3)
end
end
end
diff --git a/spree/core/spec/models/spree/price_spec.rb b/spree/core/spec/models/spree/price_spec.rb
index 3309964b57..2abac2166c 100644
--- a/spree/core/spec/models/spree/price_spec.rb
+++ b/spree/core/spec/models/spree/price_spec.rb
@@ -23,13 +23,13 @@ describe Spree::Price, type: :model do
end
end
- describe 'after_commit :auto_match_taxons' do
+ describe 'after_commit :auto_match_collections' do
context 'when price is discounted' do
context 'on create' do
let(:price) { build(:price, amount: 10, compare_at_amount: 20, currency: 'GBP') }
- it 'auto matches taxons' do
- expect_any_instance_of(Spree::Product).to receive(:auto_match_taxons).at_least(:once)
+ it 'auto matches collections' do
+ expect_any_instance_of(Spree::Product).to receive(:auto_match_collections).at_least(:once)
price.save
end
end
@@ -38,15 +38,15 @@ describe Spree::Price, type: :model do
let!(:price) { create(:price, amount: 10, compare_at_amount: 20, currency: 'GBP') }
context 'and changed to not be discounted' do
- it 'auto matches taxons' do
- expect_any_instance_of(Spree::Product).to receive(:auto_match_taxons)
+ it 'auto matches collections' do
+ expect_any_instance_of(Spree::Product).to receive(:auto_match_collections)
price.reload.update(compare_at_amount: nil)
end
end
context 'and is still discounted' do
it 'does not touch shop product' do
- expect_any_instance_of(Spree::Product).not_to receive(:auto_match_taxons)
+ expect_any_instance_of(Spree::Product).not_to receive(:auto_match_collections)
price.reload.update(amount: 15)
end
end
@@ -57,8 +57,8 @@ describe Spree::Price, type: :model do
let(:price) { build(:price, amount: 10, compare_at_amount: nil, currency: 'GBP') }
context 'on create' do
- it 'auto matches taxons' do
- expect_any_instance_of(Spree::Product).to receive(:auto_match_taxons)
+ it 'auto matches collections' do
+ expect_any_instance_of(Spree::Product).to receive(:auto_match_collections)
price.save
end
end
@@ -67,15 +67,15 @@ describe Spree::Price, type: :model do
let!(:price) { create(:price, amount: 10, compare_at_amount: nil, currency: 'GBP') }
context 'and changed to be discounted' do
- it 'auto matches taxons' do
- expect_any_instance_of(Spree::Product).to receive(:auto_match_taxons)
+ it 'auto matches collections' do
+ expect_any_instance_of(Spree::Product).to receive(:auto_match_collections)
price.reload.update(compare_at_amount: 20)
end
end
context 'and is still not discounted' do
it 'does not touch shop product' do
- expect_any_instance_of(Spree::Product).not_to receive(:auto_match_taxons)
+ expect_any_instance_of(Spree::Product).not_to receive(:auto_match_collections)
price.reload.update(amount: 15)
end
end
diff --git a/spree/core/spec/models/spree/taxon_spec.rb b/spree/core/spec/models/spree/taxon_spec.rb
index f837a0cc26..392be732a3 100644
--- a/spree/core/spec/models/spree/taxon_spec.rb
+++ b/spree/core/spec/models/spree/taxon_spec.rb
@@ -162,52 +162,6 @@ describe Spree::Taxon, type: :model do
end
end
- describe 'callbacks' do
- describe 'regenerate_taxon_products' do
- let!(:taxon) { create(:automatic_taxon) }
-
- before { taxon.reload }
-
- context "when taxon's rules_match_policy changes" do
- it 'calls #regenerate_taxon_products' do
- expect(taxon).to receive(:regenerate_taxon_products).and_call_original
-
- taxon.update!(rules_match_policy: :any)
- end
- end
-
- context 'when taxon\'s rule changes' do
- let!(:tag_rule) { create(:tag_taxon_rule, taxon: taxon, value: 'tag') }
-
- it 'calls #regenerate_taxon_products' do
- tag_rule.reload
- expect(tag_rule).to receive(:regenerate_taxon_products).and_call_original
-
- tag_rule.update!(value: 'new tag')
- end
- end
-
- context 'when rule is destroyed' do
- let!(:tag_rule) { create(:tag_taxon_rule, taxon: taxon, value: 'tag') }
-
- it 'calls #regenerate_taxon_products' do
- tag_rule.reload
- expect(tag_rule).to receive(:regenerate_taxon_products).and_call_original
-
- tag_rule.destroy!
- end
- end
-
- context 'when rule is created' do
- it 'calls #regenerate_taxon_products' do
- expect(taxon).to receive(:regenerate_taxon_products).and_call_original
-
- create(:tag_taxon_rule, taxon: taxon, value: 'tag')
- end
- end
- end
- end
-
context 'when using another locale' do
before do
root_taxon = taxon.taxonomy.root
diff --git a/spree/core/spec/services/spree/products/auto_match_taxons_spec.rb b/spree/core/spec/services/spree/products/auto_match_taxons_spec.rb
deleted file mode 100644
index 33a63e190c..0000000000
--- a/spree/core/spec/services/spree/products/auto_match_taxons_spec.rb
+++ /dev/null
@@ -1,49 +0,0 @@
-require 'spec_helper'
-
-RSpec.describe Spree::Products::AutoMatchTaxons do
- subject { described_class.call(product: product) }
-
- let(:store) { @default_store }
- let(:product) { create(:product) }
-
- context 'when product matches new taxon' do
- let!(:taxon) { create(:automatic_taxon, taxonomy: store.taxonomies.first || create(:taxonomy, store: store)) }
-
- before do
- create(:tag_taxon_rule, taxon: taxon, value: 'cruelty-free')
-
- product.tag_list.add('cruelty-free')
- product.save
- end
-
- it 'should be added to the taxon' do
- subject
- expect(taxon.products.reload).to include(product)
- end
-
- it "doesn't do circular call" do
- product.tag_list.add('cruelty-free')
- product.save
-
- subject
-
- expect(product).not_to receive(:auto_match_taxons)
- end
- end
-
- context 'when product no longer matches taxon' do
- let(:taxon) { create(:automatic_taxon, taxonomy: store.taxonomies.first || create(:taxonomy, store: store)) }
-
- before do
- create(:tag_taxon_rule, taxon: taxon, value: 'cruelty-free')
- taxon.products << product
- end
-
- it 'should be removed from the taxon' do
- expect(taxon.products.reload).to include(product)
- subject
-
- expect(taxon.products.reload).not_to include(product)
- end
- end
-end
diff --git a/spree/core/spec/services/spree/taxons/add_products_spec.rb b/spree/core/spec/services/spree/taxons/add_products_spec.rb
index d1a0c55e32..0c883af258 100644
--- a/spree/core/spec/services/spree/taxons/add_products_spec.rb
+++ b/spree/core/spec/services/spree/taxons/add_products_spec.rb
@@ -38,14 +38,14 @@ module Spree
expect(subject.value).to eq true
end
- it 'updates classification_count on taxons' do
- expect { subject }.to change { taxons.first.reload.classification_count }.from(0).to(products.size)
- .and change { taxons.second.reload.classification_count }.from(0).to(products.size)
+ it 'updates products_count on taxons' do
+ expect { subject }.to change { taxons.first.reload.products_count }.from(0).to(products.size)
+ .and change { taxons.second.reload.products_count }.from(0).to(products.size)
end
- it 'updates classification_count on products' do
- expect { subject }.to change { products.first.reload.classification_count }.from(0).to(taxons.size)
- .and change { products.second.reload.classification_count }.from(0).to(taxons.size)
+ it 'updates categories_count on products' do
+ expect { subject }.to change { products.first.reload.categories_count }.from(0).to(taxons.size)
+ .and change { products.second.reload.categories_count }.from(0).to(taxons.size)
end
end
end
diff --git a/spree/core/spec/services/spree/taxons/regenerate_products_spec.rb b/spree/core/spec/services/spree/taxons/regenerate_products_spec.rb
deleted file mode 100644
index ca7ba94c05..0000000000
--- a/spree/core/spec/services/spree/taxons/regenerate_products_spec.rb
+++ /dev/null
@@ -1,114 +0,0 @@
-require 'spec_helper'
-
-RSpec.describe Spree::Taxons::RegenerateProducts do
- subject(:regenerate_products) { described_class.call(taxon: taxon) }
-
- let!(:taxon) { create(:automatic_taxon, :any_match_policy, products: tagged_products) }
- let!(:tag_taxon_rule) { create(:tag_taxon_rule, taxon: taxon, value: 'tag') }
-
- let(:tagged_products) { create_list(:product_in_stock, 2, tag_list: 'tag') }
-
- let(:all_products) { tagged_products }
-
- context 'after changing rules' do
- let!(:other_tag_taxon_rule) { create(:tag_taxon_rule, :is_equal_to, taxon: taxon, value: 'other') }
- let!(:other_tagged_product) { create(:product_in_stock, tag_list: 'other') }
-
- let(:all_products) { [*tagged_products, other_tagged_product] }
-
- before do
- tag_taxon_rule.destroy!
- taxon.reload
- end
-
- it 're-matches products for an automatic taxon' do
- expect { regenerate_products }.to change(taxon.products, :count).from(2).to(1)
- expect(taxon.reload.products).to contain_exactly(other_tagged_product)
- end
-
- it 'updates classification_count on taxon' do
- expect { regenerate_products }.to change { taxon.reload.classification_count }.from(2).to(1)
- end
-
- it 'updates classification_count on products' do
- removed_product = tagged_products.first
- expect { regenerate_products }.to change { removed_product.reload.classification_count }.from(1).to(0)
- end
- end
-
- context 'when nothing changed' do
- before do
- taxon.reload
- end
-
- it "doesn't change the taxon" do
- expect { regenerate_products }.to_not change(taxon.products, :count)
- expect(taxon.reload.products).to contain_exactly(*tagged_products)
- end
- end
-
- context 'with manual order' do
- context 'when nothing changed' do
- it 'keeps products positions' do
- regenerate_products
- expect(taxon.reload.classifications.order(:position).pluck(:position)).to eq([1, 2])
- end
- end
-
- context 'when products added' do
- let!(:new_product) { create(:product) }
-
- let(:matching_rules_products) { double(ids: ([new_product] + taxon.products).map(&:id)) }
-
- before do
- allow(taxon).to receive(:products_matching_rules).and_return(matching_rules_products)
- taxon.classifications.order(position: :asc).last.move_to_top
- end
-
- it 'keeps products positions' do
- old_positions = taxon.classifications.reload.order(position: :asc).pluck(:product_id, :position)
- regenerate_products
- expect(taxon.products.order(position: :asc).limit(2).pluck(:id, :position)).to eq(old_positions)
- expect(taxon.reload.classifications.pluck(:position)).to match_array([1, 2, 3])
- end
- end
-
- context 'when some one product removed and 2 added' do
- let(:tagged_products) { create_list(:product_in_stock, 3, tag_list: 'tag') }
- let(:new_tagged_products) { create_list(:product_in_stock, 2, tag_list: 'tag') }
-
- let(:matching_rules_products) { double(ids: new_tagged_products.map(&:id) + [tagged_products.first.id] ) }
-
- before do
- allow(taxon).to receive(:products_matching_rules).and_return(matching_rules_products)
- end
-
- it 'keeps products positions' do
- old_positions = taxon.classifications.reload.order(position: :asc).limit(1).pluck(:product_id, :position)
- regenerate_products
- expect(taxon.products.order(position: :asc).limit(1).pluck(:id, :position)).to eq(old_positions)
- expect(taxon.reload.classifications.pluck(:position)).to match_array([1, 2, 3])
- end
- end
-
- context 'when some one product removed from middle of list and 2 added' do
- let(:tagged_products) { create_list(:product_in_stock, 3, tag_list: 'tag') }
- let(:new_tagged_products) { create_list(:product_in_stock, 2, tag_list: 'tag') }
-
- let(:current_taxon_products) { taxon.classifications.reload.order(position: :asc) }
- let(:matching_rules_products) { double(ids: (new_tagged_products.map(&:id) + [current_taxon_products.first.product_id, current_taxon_products.last.product_id]).shuffle ) }
-
- before do
- allow(taxon).to receive(:products_matching_rules).and_return(matching_rules_products)
- end
-
- it 'keeps products positions' do
- product_1 = current_taxon_products.first.product
- product_3 = current_taxon_products.third.product
- regenerate_products
- expect(taxon.classifications.reload.order(position: :asc).where(product: [product_1, product_3]).pluck(:position)).to eq([1,2])
- expect(taxon.reload.classifications.pluck(:position)).to match_array([1, 2, 3, 4])
- end
- end
- end
-end
diff --git a/spree/core/spec/services/spree/taxons/remove_products_spec.rb b/spree/core/spec/services/spree/taxons/remove_products_spec.rb
index ce6c780147..fb1d3eb341 100644
--- a/spree/core/spec/services/spree/taxons/remove_products_spec.rb
+++ b/spree/core/spec/services/spree/taxons/remove_products_spec.rb
@@ -46,14 +46,14 @@ module Spree
expect(subject.value).to eq true
end
- it 'updates classification_count on taxons' do
- expect { subject }.to change { taxons.first.reload.classification_count }.from(products.size).to(0)
- .and change { taxons.second.reload.classification_count }.from(products.size).to(0)
+ it 'updates products_count on taxons' do
+ expect { subject }.to change { taxons.first.reload.products_count }.from(products.size).to(0)
+ .and change { taxons.second.reload.products_count }.from(products.size).to(0)
end
- it 'updates classification_count on products' do
- expect { subject }.to change { products.first.reload.classification_count }.from(taxons.size).to(0)
- .and change { products.second.reload.classification_count }.from(taxons.size).to(0)
+ it 'updates categories_count on products' do
+ expect { subject }.to change { products.first.reload.categories_count }.from(taxons.size).to(0)
+ .and change { products.second.reload.categories_count }.from(taxons.size).to(0)
end
end
end