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.

Fixed `by_taxon_id` scope for SQLIte (#12367)

Damian Legawiec6aa0e4366dd3
diff --git a/core/app/models/spree/page_sections/featured_taxon.rb b/core/app/models/spree/page_sections/featured_taxon.rb
index 718e8fdf0e..fce99cdd53 100644
--- a/core/app/models/spree/page_sections/featured_taxon.rb
+++ b/core/app/models/spree/page_sections/featured_taxon.rb
@@ -2,8 +2,11 @@ module Spree
module PageSections
class FeaturedTaxon < Spree::PageSection
scope :by_taxon_id, lambda { |taxon_ids|
- regexp = [*taxon_ids].map { |taxon_id| "taxon_id: '#{taxon_id}'" }.join('|')
- where("#{Spree::PageSections::FeaturedTaxon.table_name}.preferences ~ ?", regexp)
+ queries = [*taxon_ids].map do |taxon_id|
+ where("#{Spree::PageSections::FeaturedTaxon.table_name}.preferences LIKE ?", "%taxon_id: '#{taxon_id}'%")
+ end
+
+ queries.reduce(:or)
}
has_rich_text :description
diff --git a/core/spec/models/spree/page_sections/featured_taxon_spec.rb b/core/spec/models/spree/page_sections/featured_taxon_spec.rb
new file mode 100644
index 0000000000..33c5e16a31
--- /dev/null
+++ b/core/spec/models/spree/page_sections/featured_taxon_spec.rb
@@ -0,0 +1,18 @@
+require 'spec_helper'
+
+RSpec.describe Spree::PageSections::FeaturedTaxon do
+ describe '.by_taxon_id' do
+ let!(:taxons) { create_list(:taxon, 3) }
+
+ let!(:featured_taxon_1) { create(:featured_taxon, preferred_taxon_id: taxons[0].id) }
+ let!(:featured_taxon_2) { create(:featured_taxon, preferred_taxon_id: taxons[1].id) }
+
+ it 'returns featured taxons by the taxon id' do
+ expect(described_class.by_taxon_id(taxons[0].id)).to contain_exactly(featured_taxon_1)
+ expect(described_class.by_taxon_id(taxons[2].id)).to be_empty
+
+ expect(described_class.by_taxon_id([taxons[0].id, taxons[1].id])).to contain_exactly(featured_taxon_1, featured_taxon_2)
+ expect(described_class.by_taxon_id([taxons[1].id, taxons[2].id])).to contain_exactly(featured_taxon_2)
+ end
+ end
+end