Add search_variants_as parameter to variant search functionality

This commit is contained in:
Ahmed Ejaz
2025-06-15 18:10:55 +05:00
parent a37e08c2fd
commit 8e8878e43a
6 changed files with 12 additions and 3 deletions

View File

@@ -5,6 +5,7 @@ angular.module("admin.orders").controller "orderCtrl", ($scope, shops, orderCycl
$scope.distributor_id = parseInt($attrs.ofnDistributorId)
$scope.order_cycle_id = parseInt($attrs.ofnOrderCycleId)
$scope.search_variants_as = $attrs.ofnSearchVariantsAs
$scope.validOrderCycle = (oc) ->
$scope.orderCycleHasDistributor oc, parseInt($scope.distributor_id)

View File

@@ -26,6 +26,7 @@ angular.module("admin.utils").directive "variantAutocomplete", ($timeout) ->
order_cycle_id: scope.order_cycle_id
eligible_for_subscriptions: scope.eligible_for_subscriptions
include_out_of_stock: scope.include_out_of_stock
search_variants_as: scope.search_variants_as
results: (data, page) ->
window.variants = data # this is how spree auto complete JS code picks up variants
results: data

View File

@@ -117,7 +117,7 @@ module Spree
def variant_search_params
params.permit(
:q, :distributor_id, :order_cycle_id, :schedule_id, :eligible_for_subscriptions,
:include_out_of_stock
:include_out_of_stock, :search_variants_as,
).to_h.with_indifferent_access
end

View File

@@ -21,7 +21,7 @@ module OpenFoodNetwork
scope_to_schedule if params[:schedule_id]
scope_to_order_cycle if params[:order_cycle_id]
scope_to_distributor if params[:distributor_id]
scope_to_supplier if spree_current_user.can_manage_line_items_in_orders_only?
scope_to_supplier if params[:search_variants_as] == 'supplier'
@variants
end

View File

@@ -137,6 +137,13 @@ RSpec.describe Spree::Admin::VariantsController do
expect(variant).to have_received(:destroy)
end
describe "#search" do
it "filters by distributor and supplier1 products" do
spree_get :search, q: 'Prod', distributor_id: d.id.to_s, search_variants_as: 'supplier'
expect(assigns(:variants)).to eq([v1])
end
end
it 'shows a success flash message' do
spree_delete :destroy, id: variant.id, product_id: variant.product.id,
format: 'html'

View File

@@ -185,7 +185,7 @@ RSpec.describe OpenFoodNetwork::ScopeVariantsForSearch do
end
context "when search is done by the producer allowing to edit orders" do
let(:params) { { q: "product" } }
let(:params) { { q: "product", search_variants_as: 'supplier' } }
let(:producer) { create(:supplier_enterprise) }
let!(:spree_current_user) {
instance_double('Spree::User', enterprises: Enterprise.where(id: producer.id),