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 Product Filters API scope (#13607)

Damian Legawiecc459b4831fed
diff --git a/CLAUDE.md b/CLAUDE.md
index 4d21af11da..d3d131f5c4 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -4,15 +4,19 @@
### Framework & Architecture
-- Spree is built on Ruby on Rails and follows MVC architecture
-- All Spree code must be namespaced under `Spree::` module
+- Spree backend is built on Ruby on Rails and follows MVC architecture
+- All Spree backend code must be namespaced under `Spree::` module
- Spree is distributed as Rails engines with separate packages:
- - **Core packages (required):** `spree_core` (models, services, business logic, events engine), `spree_api` (Store API, Admin API (soon!), HTTP Webhooks)
+ - **Core packages (required):** `spree_core` (models, services, business logic, events engine, pricing engine), `spree_api` (Store API, Admin API (soon!), HTTP Webhooks)
- **Optional packages:** `spree_admin` (admin dashboard), `spree_emails` (transactional emails)
- Most users run Spree in headless mode with custom frontends using the Store API
- Follow Rails conventions and the Rails Security Guide
- Prefer Rails idioms and standard patterns over custom solutions
- Use RESTful action names
+- Enforce CanCanCan permission checks, for listings always use `accessible_by(current_ability, :show)` scope, for other actions always perform `authorize!` check
+- For better security always use scope fetching, eg. `current_user.orders` instead of `Spree::Order`
+- Use Ransack for filtering and searching https://activerecord-hackery.github.io/ransack/getting-started/search-matches/
+- Use Pagy for pagination https://github.com/ddnexus/pagy
### Code Organization
@@ -28,6 +32,7 @@
- Group related functionality into concerns when appropriate
- Do not call `Spree::User` directly, use `Spree.user_class` instead
- Do not call `Spree::AdminUser` directly, use `Spree.admin_user_class` instead
+- Split big models in modules/concerns, keep them organized by topic
### Spree::Current class
diff --git a/backend/engines/api/app/controllers/spree/api/v3/store/products/filters_controller.rb b/backend/engines/api/app/controllers/spree/api/v3/store/products/filters_controller.rb
index 163f599489..a087e88301 100644
--- a/backend/engines/api/app/controllers/spree/api/v3/store/products/filters_controller.rb
+++ b/backend/engines/api/app/controllers/spree/api/v3/store/products/filters_controller.rb
@@ -16,13 +16,17 @@ module Spree
private
# Build scope from taxon and/or ransack params
+ # @return [ActiveRecord::Relation]
def filters_scope
- scope = current_store.products.available(Time.current, current_currency)
+ scope = current_store.products.active(current_currency)
scope = scope.in_taxon(taxon) if taxon.present?
scope = scope.ransack(params[:q]).result if params[:q].present?
scope.accessible_by(current_ability, :show)
end
+ # Fetches taxon from params
+ # @param [String] taxon_id
+ # @return [Spree::Taxon]
def taxon
@taxon ||= params[:taxon_id].present? ? current_store.taxons.find_by_param(params[:taxon_id]) : nil
end
diff --git a/backend/engines/api/app/controllers/spree/api/v3/store/products_controller.rb b/backend/engines/api/app/controllers/spree/api/v3/store/products_controller.rb
index 897fe90420..c73a7f4926 100644
--- a/backend/engines/api/app/controllers/spree/api/v3/store/products_controller.rb
+++ b/backend/engines/api/app/controllers/spree/api/v3/store/products_controller.rb
@@ -21,6 +21,7 @@ module Spree
# Find product by slug or prefixed ID with i18n scope for SEO-friendly URLs
# Falls back to default locale if product is not found in the current locale
+ # @return [Spree::Product]
def find_resource
id = params[:id]
if id.to_s.start_with?('prod_')
diff --git a/backend/engines/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb b/backend/engines/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
index 70b9780d12..9725b6525a 100644
--- a/backend/engines/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
+++ b/backend/engines/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
@@ -112,6 +112,40 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller do
expect(json_response['data'].size).to eq(1)
expect(json_response['data'].first['id']).to eq(product.prefixed_id)
end
+
+ context 'filtering by option values' do
+ let(:option_type) { create(:option_type, :color) }
+ let(:option_value_red) { create(:option_value, option_type: option_type, name: 'red', presentation: 'Red') }
+ let(:option_value_blue) { create(:option_value, option_type: option_type, name: 'blue', presentation: 'Blue') }
+ let!(:product_with_red) do
+ create(:product, stores: [store], status: 'active', option_types: [option_type]).tap do |p|
+ create(:variant, product: p, option_values: [option_value_red], price: 25.0)
+ end
+ end
+ let!(:product_with_blue) do
+ create(:product, stores: [store], status: 'active', option_types: [option_type]).tap do |p|
+ create(:variant, product: p, option_values: [option_value_blue], price: 75.0)
+ end
+ end
+
+ it 'filters products by option value prefixed IDs' do
+ get :index, params: { q: { with_option_value_ids: [option_value_red.prefixed_id] } }
+
+ expect(response).to have_http_status(:ok)
+ ids = json_response['data'].map { |p| p['id'] }
+ expect(ids).to include(product_with_red.prefixed_id)
+ expect(ids).not_to include(product_with_blue.prefixed_id)
+ end
+
+ it 'filters products by price range and option values combined' do
+ get :index, params: { q: { with_option_value_ids: [option_value_red.prefixed_id, option_value_blue.prefixed_id], price_between: [50, 100] } }
+
+ expect(response).to have_http_status(:ok)
+ ids = json_response['data'].map { |p| p['id'] }
+ expect(ids).to include(product_with_blue.prefixed_id)
+ expect(ids).not_to include(product_with_red.prefixed_id)
+ end
+ end
end
context 'sorting' do
diff --git a/backend/engines/api/spec/controllers/spree/api/v3/store/taxons/products_controller_spec.rb b/backend/engines/api/spec/controllers/spree/api/v3/store/taxons/products_controller_spec.rb
index d1dd552532..c5ed629135 100644
--- a/backend/engines/api/spec/controllers/spree/api/v3/store/taxons/products_controller_spec.rb
+++ b/backend/engines/api/spec/controllers/spree/api/v3/store/taxons/products_controller_spec.rb
@@ -100,6 +100,49 @@ RSpec.describe Spree::Api::V3::Store::Taxons::ProductsController, type: :control
end
end
+ context 'filtering by option values' do
+ let(:option_type) { create(:option_type, :color) }
+ let(:option_value_red) { create(:option_value, option_type: option_type, name: 'red', presentation: 'Red') }
+ let(:option_value_blue) { create(:option_value, option_type: option_type, name: 'blue', presentation: 'Blue') }
+ let!(:product_with_red) do
+ create(:product, stores: [store], taxons: [taxon], status: 'active', option_types: [option_type]).tap do |p|
+ create(:variant, product: p, option_values: [option_value_red], price: 25.0)
+ end
+ end
+ let!(:product_with_blue) do
+ create(:product, stores: [store], taxons: [taxon], status: 'active', option_types: [option_type]).tap do |p|
+ create(:variant, product: p, option_values: [option_value_blue], price: 75.0)
+ end
+ end
+
+ it 'filters products by option value prefixed IDs within taxon' do
+ get :index, params: { taxon_id: taxon.permalink, q: { with_option_value_ids: [option_value_red.prefixed_id] } }
+
+ expect(response).to have_http_status(:ok)
+ ids = json_response['data'].map { |p| p['id'] }
+ expect(ids).to include(product_with_red.prefixed_id)
+ expect(ids).not_to include(product_with_blue.prefixed_id)
+ end
+
+ it 'filters products by price range within taxon' do
+ get :index, params: { taxon_id: taxon.permalink, q: { price_between: [50, 100] } }
+
+ expect(response).to have_http_status(:ok)
+ ids = json_response['data'].map { |p| p['id'] }
+ expect(ids).to include(product_with_blue.prefixed_id)
+ expect(ids).not_to include(product_with_red.prefixed_id)
+ end
+
+ it 'filters products by option values and price range combined within taxon' do
+ get :index, params: { taxon_id: taxon.permalink, q: { with_option_value_ids: [option_value_red.prefixed_id, option_value_blue.prefixed_id], price_between: [50, 100] } }
+
+ expect(response).to have_http_status(:ok)
+ ids = json_response['data'].map { |p| p['id'] }
+ expect(ids).to include(product_with_blue.prefixed_id)
+ expect(ids).not_to include(product_with_red.prefixed_id)
+ end
+ end
+
context 'sorting' do
let!(:cheap_product) do
create(:product, stores: [store], taxons: [taxon], status: 'active', name: 'Cheap').tap do |p|
diff --git a/backend/engines/core/app/models/concerns/spree/product_scopes.rb b/backend/engines/core/app/models/concerns/spree/product_scopes.rb
index 0d895b6f58..d8d9010185 100644
--- a/backend/engines/core/app/models/concerns/spree/product_scopes.rb
+++ b/backend/engines/core/app/models/concerns/spree/product_scopes.rb
@@ -126,9 +126,8 @@ module Spree
#
# SELECT COUNT(*) ...
add_search_scope :in_taxon do |taxon|
- includes(:classifications).
- where('spree_products_taxons.taxon_id' => taxon.cached_self_and_descendants_ids).
- order('spree_products_taxons.position ASC')
+ joins(:classifications).
+ where("#{Classification.table_name}.taxon_id" => taxon.cached_self_and_descendants_ids).distinct
end
# This scope selects products in all taxons AND all its descendants
diff --git a/backend/engines/core/spec/models/spree/product/scopes_spec.rb b/backend/engines/core/spec/models/spree/product/scopes_spec.rb
index 1108e2de71..6dc578e254 100644
--- a/backend/engines/core/spec/models/spree/product/scopes_spec.rb
+++ b/backend/engines/core/spec/models/spree/product/scopes_spec.rb
@@ -84,20 +84,13 @@ describe 'Product scopes', type: :model do
expect(Spree::Product.in_taxon(@parent_taxon).to_a.size).to eq(1)
end
- context 'orders products based on their ordering within the classifications' do
+ context 'returns correct products for taxon' do
let(:other_taxon) { create(:taxon, products: [product]) }
let!(:product_2) { create(:product, taxons: [@child_taxon, other_taxon], stores: [store]) }
- it 'by initial ordering' do
- expect(Spree::Product.in_taxon(@child_taxon)).to eq([product, product_2])
- expect(Spree::Product.in_taxon(other_taxon)).to eq([product, product_2])
- end
-
- it 'after ordering changed' do
- [@child_taxon, other_taxon].each do |taxon|
- Spree::Classification.find_by(taxon: taxon, product: product).insert_at(2)
- expect(Spree::Product.in_taxon(taxon)).to eq([product_2, product])
- end
+ it 'includes all products in the taxon' do
+ expect(Spree::Product.in_taxon(@child_taxon)).to include(product, product_2)
+ expect(Spree::Product.in_taxon(other_taxon)).to include(product, product_2)
end
end
end