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
Add collection model + rule + Meilisearch specs; keep legacy sorter out of the diff
Łukasz Adamczyk90f59df8e7f5
diff --git a/spree/core/app/sorters/spree/classifications/sort.rb b/spree/core/app/sorters/spree/classifications/sort.rbindex b4149e9254..c768ac76ce 100644--- a/spree/core/app/sorters/spree/classifications/sort.rb+++ b/spree/core/app/sorters/spree/classifications/sort.rb@@ -53,11 +53,11 @@ module Spreereturn scope unless (value = sort_by?('price'))scope.joins(product: { variants: :prices }).- select("#{Spree::ProductCategory.table_name}.*, min(#{Spree::Price.table_name}.amount)").+ select("#{Spree::Classification.table_name}.*, min(#{Spree::Price.table_name}.amount)").distinct.where(spree_prices: { currency: currency }).order("min(#{Spree::Price.table_name}.amount) #{value[1]}").- group("#{Spree::ProductCategory.table_name}.id")+ group("#{Spree::Classification.table_name}.id")enddef sort_by?(field)diff --git a/spree/core/spec/models/spree/collection_rule_spec.rb b/spree/core/spec/models/spree/collection_rule_spec.rbindex e044fc58d3..ba9ff225da 100644--- a/spree/core/spec/models/spree/collection_rule_spec.rb+++ b/spree/core/spec/models/spree/collection_rule_spec.rb@@ -45,22 +45,4 @@ RSpec.describe Spree::CollectionRule, type: :model dorule.destroy!endend-- # Tag and Sale rule application is covered via Collection#products_matching_rules;- # AvailableOn is exercised directly here.- describe Spree::CollectionRules::AvailableOn do- let!(:recent_product) { create(:product).tap { |p| p.update_columns(available_on: 1.day.ago) } }- let!(:old_product) do- create(:product).tap { |p| p.update_columns(available_on: 90.days.ago, created_at: 90.days.ago) }- end-- it 'matches products created or available within the window' do- rule = build(:available_on_collection_rule, :is_equal_to, collection: collection, value: 30)-- result = rule.apply(store.products.not_archived)-- expect(result).to include(recent_product)- expect(result).not_to include(old_product)- end- endenddiff --git a/spree/core/spec/models/spree/collection_rules/available_on_spec.rb b/spree/core/spec/models/spree/collection_rules/available_on_spec.rbnew file mode 100644index 0000000000..31291712a1--- /dev/null+++ b/spree/core/spec/models/spree/collection_rules/available_on_spec.rb@@ -0,0 +1,22 @@+require 'spec_helper'++RSpec.describe Spree::CollectionRules::AvailableOn, type: :model do+ let(:store) { @default_store }+ let(:collection) { create(:automatic_collection, store: store) }++ describe '#apply' do+ let!(:recent_product) { create(:product).tap { |p| p.update_columns(available_on: 1.day.ago) } }+ let!(:old_product) do+ create(:product).tap { |p| p.update_columns(available_on: 90.days.ago, created_at: 90.days.ago) }+ end++ it 'matches products created or available within the window' do+ rule = build(:available_on_collection_rule, :is_equal_to, collection: collection, value: 30)++ result = rule.apply(store.products.not_archived)++ expect(result).to include(recent_product)+ expect(result).not_to include(old_product)+ end+ end+enddiff --git a/spree/core/spec/models/spree/collection_rules/tag_spec.rb b/spree/core/spec/models/spree/collection_rules/tag_spec.rbnew file mode 100644index 0000000000..8fc6205ccf--- /dev/null+++ b/spree/core/spec/models/spree/collection_rules/tag_spec.rb@@ -0,0 +1,39 @@+require 'spec_helper'++RSpec.describe Spree::CollectionRules::Tag, type: :model do+ let(:store) { @default_store }+ let(:collection) { create(:automatic_collection, store: store) }++ # #apply covers each tag match policy directly (build, not create, to bypass the+ # regeneration callback and the value/match_policy validations for the else branch).+ describe '#apply' do+ let!(:sale) { create(:product, tag_list: 'sale') }+ let!(:new_arrival) { create(:product, tag_list: 'new') }+ let!(:both) { create(:product, tag_list: 'sale, new') }+ let(:scope) { store.products.where(id: [sale.id, new_arrival.id, both.id]) }++ def tag_rule(match_policy)+ build(:tag_collection_rule, collection: collection, value: 'sale', match_policy: match_policy)+ end++ it 'is_equal_to matches products carrying the tag' do+ expect(tag_rule('is_equal_to').apply(scope)).to contain_exactly(sale, both)+ end++ it 'is_not_equal_to excludes products carrying the tag' do+ expect(tag_rule('is_not_equal_to').apply(scope)).to contain_exactly(new_arrival)+ end++ it 'contains matches products carrying any of the tag' do+ expect(tag_rule('contains').apply(scope)).to contain_exactly(sale, both)+ end++ it 'does_not_contain excludes products carrying any of the tag' do+ expect(tag_rule('does_not_contain').apply(scope)).to contain_exactly(new_arrival)+ end++ it 'returns the scope unchanged for an unknown match policy' do+ expect(tag_rule('bogus').apply(scope)).to contain_exactly(sale, new_arrival, both)+ end+ end+enddiff --git a/spree/core/spec/models/spree/collection_spec.rb b/spree/core/spec/models/spree/collection_spec.rbindex 3fd1b6296a..838afa8fdd 100644--- a/spree/core/spec/models/spree/collection_spec.rb+++ b/spree/core/spec/models/spree/collection_spec.rb@@ -188,4 +188,114 @@ RSpec.describe Spree::Collection, type: :model doendendend++ describe '#slug / #slug=' do+ it 'reads the permalink' do+ expect(create(:collection, name: 'Summer Sale').slug).to eq('summer-sale')+ end++ it 'writes through to the permalink' do+ collection = build(:collection)+ collection.slug = 'custom-slug'++ expect(collection.permalink).to eq('custom-slug')+ end+ end++ describe '#regenerate_products' do+ let(:collection) { create(:automatic_collection) }+ let!(:sale_product) { create(:product, tag_list: 'sale') }++ before do+ create(:tag_collection_rule, :is_equal_to, collection: collection, value: 'sale')+ Spree::ProductCollection.where(collection_id: collection.id).delete_all+ end++ subject(:reloaded_collection) { Spree::Collection.find(collection.id) }++ def membership_product_ids+ Spree::ProductCollection.where(collection_id: collection.id).pluck(:product_id)+ end++ it 'rebuilds the materialized membership from the rules' do+ reloaded_collection.regenerate_products++ expect(membership_product_ids).to contain_exactly(sale_product.id)+ end++ it 'clears the flag and no-ops on a second only_once call' do+ reloaded_collection.regenerate_products(only_once: true)++ expect(reloaded_collection.marked_for_regenerate_products?).to be(false)+ expect(membership_product_ids).to contain_exactly(sale_product.id)++ Spree::ProductCollection.where(collection_id: collection.id).delete_all+ reloaded_collection.regenerate_products(only_once: true)++ expect(membership_product_ids).to be_empty+ end++ it 'does nothing when not marked for regeneration' do+ reloaded_collection.marked_for_regenerate_products = false+ reloaded_collection.regenerate_products++ expect(membership_product_ids).to be_empty+ end+ end++ # The Admin API sends the full desired rule set; #rules= syncs it (build/update/destroy)+ # like Spree::OptionType#option_values=. Operate on a freshly-loaded record (as the+ # controller does via find_resource + scope_includes [:rules]) so the in-memory rule+ # set reflects current DB state.+ describe '#rules= (sync setter)' do+ let(:collection) { create(:automatic_collection) }++ subject(:reloaded_collection) { Spree::Collection.find(collection.id) }++ it 'builds new rules from attribute hashes, selecting the STI subclass by type' do+ reloaded_collection.rules = [{ type: 'Spree::CollectionRules::Tag', value: 'sale', match_policy: 'is_equal_to' }]+ reloaded_collection.save!++ rule = reloaded_collection.reload.rules.first+ expect(reloaded_collection.rules.size).to eq(1)+ expect(rule).to be_a(Spree::CollectionRules::Tag)+ expect(rule.value).to eq('sale')+ expect(rule.match_policy).to eq('is_equal_to')+ end++ it 'updates an existing rule matched by its prefixed id' do+ rule = create(:tag_collection_rule, :is_equal_to, collection: collection, value: 'sale')++ reloaded_collection.rules = [{ id: rule.prefixed_id, value: 'clearance', match_policy: 'contains' }]+ reloaded_collection.save!++ expect(rule.reload.value).to eq('clearance')+ expect(rule.match_policy).to eq('contains')+ end++ it 'destroys rules omitted from the payload' do+ keep = create(:tag_collection_rule, :is_equal_to, collection: collection, value: 'sale')+ drop = create(:tag_collection_rule, :is_equal_to, collection: collection, value: 'new')++ reloaded_collection.rules = [{ id: keep.prefixed_id, value: 'sale', match_policy: 'is_equal_to' }]+ reloaded_collection.save!++ expect(reloaded_collection.reload.rules).to contain_exactly(keep)+ expect { drop.reload }.to raise_error(ActiveRecord::RecordNotFound)+ end++ it 'raises RecordNotFound for an unknown rule id' do+ expect {+ reloaded_collection.rules = [{ id: 'crule_nonexistent', value: 'x', match_policy: 'is_equal_to' }]+ }.to raise_error(ActiveRecord::RecordNotFound)+ end++ it 'falls back to the association writer when given CollectionRule records' do+ rule = build(:tag_collection_rule, :is_equal_to, value: 'sale')++ reloaded_collection.rules = [rule]++ expect(reloaded_collection.rules).to contain_exactly(rule)+ end+ endenddiff --git a/spree/core/spec/models/spree/search_provider/meilisearch_spec.rb b/spree/core/spec/models/spree/search_provider/meilisearch_spec.rbindex e2a73d2976..23037447ea 100644--- a/spree/core/spec/models/spree/search_provider/meilisearch_spec.rb+++ b/spree/core/spec/models/spree/search_provider/meilisearch_spec.rb@@ -93,6 +93,26 @@ module Spreeendend+ context 'with in_collection filter' do+ let(:collection) { create(:collection) }++ it 'passes the collection_ids filter to Meilisearch' do+ expect(mock_index).to receive(:search).with(anything, hash_including(+ filter: include("collection_ids = '#{collection.prefixed_id}'")+ )).and_return(ms_response)++ provider.search_and_filter(scope: store.products, filters: { 'in_collection' => collection.prefixed_id })+ end++ it 'ignores a value that is not a prefixed id' do+ expect(mock_index).to receive(:search).with(anything, satisfy { |params|+ params[:filter].none? { |c| c.to_s.include?('collection_ids') }+ }).and_return(ms_response)++ provider.search_and_filter(scope: store.products, filters: { 'in_collection' => '123' })+ end+ end+context 'with sort' doit 'passes price sort to Meilisearch' doexpect(mock_index).to receive(:search).with(anything, hash_including(@@ -279,6 +299,29 @@ module Spreeexpect(result.total_count).to eq(0)endend++ # default_sort comes from the collection's sort_order (a collection PLP page),+ # rendered in API format; it falls back to 'manual' with no collection.+ context 'default_sort' do+ it 'defaults to manual when no collection is provided' do+ result = provider.filters(scope: store.products, query: 'shirt')+ expect(result.default_sort).to eq('manual')+ end++ it "reflects the collection's sort_order (space format rendered as API format)" do+ collection = create(:collection, sort_order: 'price asc')++ result = provider.filters(scope: store.products, filters: { '_collection' => collection })+ expect(result.default_sort).to eq('price')+ end++ it 'stays manual for a manual-sorted collection' do+ collection = create(:collection, sort_order: 'manual')++ result = provider.filters(scope: store.products, filters: { '_collection' => collection })+ expect(result.default_sort).to eq('manual')+ end+ endenddescribe '#index' do