mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-23 20:26:49 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6f819bd13e | ||
|
|
d1b21b490a | ||
|
|
6a74ea304c | ||
|
|
d4cfa0463f | ||
|
|
0fe9d2ac09 |
@@ -63,8 +63,7 @@ Spree::Product.class_eval do
|
||||
scope :visible_for, lambda { |enterprise|
|
||||
joins('LEFT OUTER JOIN spree_variants AS o_spree_variants ON (o_spree_variants.product_id = spree_products.id)').
|
||||
joins('LEFT OUTER JOIN inventory_items AS o_inventory_items ON (o_spree_variants.id = o_inventory_items.variant_id)').
|
||||
where('o_inventory_items.enterprise_id = (?) AND visible = (?)', enterprise, true).
|
||||
select('DISTINCT spree_products.*')
|
||||
where('o_inventory_items.enterprise_id = (?) AND visible = (?)', enterprise, true)
|
||||
}
|
||||
|
||||
# -- Scopes
|
||||
|
||||
@@ -30,9 +30,11 @@ class Api::Admin::ForOrderCycle::EnterpriseSerializer < ActiveModel::Serializer
|
||||
def products_scope
|
||||
products_relation = object.supplied_products
|
||||
if order_cycle.prefers_product_selection_from_coordinator_inventory_only?
|
||||
products_relation = products_relation.visible_for(order_cycle.coordinator)
|
||||
products_relation = products_relation.
|
||||
visible_for(order_cycle.coordinator).
|
||||
select('DISTINCT spree_products.*')
|
||||
end
|
||||
products_relation.order(:name)
|
||||
products_relation
|
||||
end
|
||||
|
||||
def products
|
||||
|
||||
@@ -11,6 +11,7 @@ module PermittedAttributes
|
||||
|
||||
@params.require(:order_cycle).permit(
|
||||
:name, :orders_open_at, :orders_close_at, :coordinator_id,
|
||||
:preferred_product_selection_from_coordinator_inventory_only,
|
||||
incoming_exchanges: permitted_exchange_attributes,
|
||||
outgoing_exchanges: permitted_exchange_attributes,
|
||||
schedule_ids: [], coordinator_fee_ids: []
|
||||
|
||||
@@ -178,10 +178,22 @@ module Admin
|
||||
|
||||
it "returns an error message" do
|
||||
spree_put :update, params
|
||||
|
||||
json_response = JSON.parse(response.body)
|
||||
expect(json_response['errors']).to be
|
||||
end
|
||||
end
|
||||
|
||||
it "can update preference product_selection_from_coordinator_inventory_only" do
|
||||
expect(OrderCycleForm).to receive(:new).
|
||||
with(order_cycle,
|
||||
{ "preferred_product_selection_from_coordinator_inventory_only" => true },
|
||||
anything) { form_mock }
|
||||
allow(form_mock).to receive(:save) { true }
|
||||
|
||||
spree_put :update, params.
|
||||
merge(order_cycle: { preferred_product_selection_from_coordinator_inventory_only: true })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -7,8 +7,9 @@ describe ExchangeProductsRenderer do
|
||||
|
||||
describe "#exchange_products" do
|
||||
describe "for an incoming exchange" do
|
||||
let(:exchange) { order_cycle.exchanges.incoming.first }
|
||||
|
||||
it "loads products" do
|
||||
exchange = order_cycle.exchanges.incoming.first
|
||||
products = renderer.exchange_products(true, exchange.sender)
|
||||
|
||||
expect(products.first.supplier.name).to eq exchange.variants.first.product.supplier.name
|
||||
@@ -16,14 +17,34 @@ describe ExchangeProductsRenderer do
|
||||
end
|
||||
|
||||
describe "for an outgoing exchange" do
|
||||
let(:exchange) { order_cycle.exchanges.outgoing.first }
|
||||
|
||||
it "loads products" do
|
||||
exchange = order_cycle.exchanges.outgoing.first
|
||||
products = renderer.exchange_products(false, exchange.receiver)
|
||||
|
||||
suppliers = [exchange.variants[0].product.supplier.name, exchange.variants[1].product.supplier.name]
|
||||
expect(suppliers).to include products.first.supplier.name
|
||||
expect(suppliers).to include products.second.supplier.name
|
||||
end
|
||||
|
||||
context "showing products from coordinator inventory only" do
|
||||
before { order_cycle.update prefers_product_selection_from_coordinator_inventory_only: true }
|
||||
|
||||
it "loads no products if there are no products from the coordinator inventory" do
|
||||
products = renderer.exchange_products(false, exchange.receiver)
|
||||
|
||||
expect(products).to be_empty
|
||||
end
|
||||
|
||||
it "loads products from the coordinator inventory" do
|
||||
# Add variant already in the exchange to the coordinator's inventory
|
||||
exchange.variants.first.inventory_items = [create(:inventory_item, enterprise: order_cycle.coordinator)]
|
||||
|
||||
products = renderer.exchange_products(false, exchange.receiver)
|
||||
|
||||
expect(products).to eq [exchange.variants.first.product]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user