mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Remove old shop/products route, action, and spec
This commit is contained in:
@@ -9,19 +9,6 @@ class ShopController < BaseController
|
||||
redirect_to main_app.enterprise_shop_path(current_distributor)
|
||||
end
|
||||
|
||||
def products
|
||||
renderer = OpenFoodNetwork::CachedProductsRenderer.new(current_distributor,
|
||||
current_order_cycle)
|
||||
|
||||
# If we add any more filtering logic, we should probably
|
||||
# move it all to a lib class like 'CachedProductsFilterer'
|
||||
products_json = filter(renderer.products_json)
|
||||
|
||||
render json: products_json
|
||||
rescue OpenFoodNetwork::CachedProductsRenderer::NoProducts
|
||||
render status: :not_found, json: ''
|
||||
end
|
||||
|
||||
def order_cycle
|
||||
if request.post?
|
||||
if oc = OrderCycle.with_distributor(@distributor).active.find_by_id(params[:order_cycle_id])
|
||||
@@ -39,27 +26,4 @@ class ShopController < BaseController
|
||||
def changeable_orders_alert
|
||||
render layout: false
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def filtered_json(products_json)
|
||||
if applicator.rules.any?
|
||||
filter(products_json)
|
||||
else
|
||||
products_json
|
||||
end
|
||||
end
|
||||
|
||||
def filter(products_json)
|
||||
products_hash = JSON.parse(products_json)
|
||||
applicator.filter!(products_hash)
|
||||
JSON.unparse(products_hash)
|
||||
end
|
||||
|
||||
def applicator
|
||||
return @applicator unless @applicator.nil?
|
||||
@applicator = OpenFoodNetwork::TagRuleApplicator.new(current_distributor,
|
||||
"FilterProducts",
|
||||
current_customer.andand.tag_list)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -33,7 +33,6 @@ Openfoodnetwork::Application.routes.draw do
|
||||
end
|
||||
|
||||
resource :shop, controller: "shop" do
|
||||
get :products
|
||||
post :order_cycle
|
||||
get :order_cycle
|
||||
get :changeable_orders_alert
|
||||
|
||||
@@ -82,167 +82,5 @@ describe ShopController, type: :controller do
|
||||
expect(controller.current_order_cycle).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "returning products" do
|
||||
let(:order_cycle) { create(:simple_order_cycle, distributors: [distributor]) }
|
||||
let(:exchange) { order_cycle.exchanges.to_enterprises(distributor).outgoing.first }
|
||||
|
||||
describe "requests and responses" do
|
||||
let(:product) { create(:product) }
|
||||
|
||||
before do
|
||||
exchange.variants << product.variants.first
|
||||
end
|
||||
|
||||
it "returns products via JSON" do
|
||||
allow(controller).to receive(:current_order_cycle).and_return order_cycle
|
||||
xhr :get, :products
|
||||
expect(response).to be_success
|
||||
end
|
||||
|
||||
it "does not return products if no order cycle is selected" do
|
||||
allow(controller).to receive(:current_order_cycle).and_return nil
|
||||
xhr :get, :products
|
||||
expect(response.status).to eq(404)
|
||||
expect(response.body).to be_empty
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "determining rule relevance" do
|
||||
let(:products_json) { double(:products_json) }
|
||||
let(:applicator) { double(:applicator) }
|
||||
|
||||
before do
|
||||
allow(applicator).to receive(:rules) { tag_rules }
|
||||
allow(controller).to receive(:applicator) { applicator }
|
||||
allow(controller).to receive(:filter) { "some filtered json" }
|
||||
end
|
||||
|
||||
context "when no relevant rules exist" do
|
||||
let(:tag_rules) { [] }
|
||||
|
||||
it "does not attempt to apply any rules" do
|
||||
controller.send(:filtered_json, products_json)
|
||||
expect(expect(controller).to_not(have_received(:filter)))
|
||||
end
|
||||
|
||||
it "returns products as JSON" do
|
||||
expect(controller.send(:filtered_json, products_json)).to eq products_json
|
||||
end
|
||||
end
|
||||
|
||||
context "when relevant rules exist" do
|
||||
let(:tag_rule) { create(:filter_products_tag_rule, preferred_customer_tags: "tag1", preferred_variant_tags: "tag1", preferred_matched_variants_visibility: "hidden" ) }
|
||||
let(:tag_rules) { [tag_rule] }
|
||||
|
||||
it "attempts to apply any rules" do
|
||||
controller.send(:filtered_json, products_json)
|
||||
expect(controller).to have_received(:filter).with(products_json)
|
||||
end
|
||||
|
||||
it "returns filtered JSON" do
|
||||
expect(controller.send(:filtered_json, products_json)).to eq "some filtered json"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "loading available order cycles" do
|
||||
let(:user) { create(:user) }
|
||||
before { allow(controller).to receive(:spree_current_user) { user } }
|
||||
|
||||
context "when FilterProducts tag rules are in effect" do
|
||||
let(:customer) { create(:customer, user: user, enterprise: distributor) }
|
||||
let!(:tag_rule) {
|
||||
create(:filter_products_tag_rule,
|
||||
enterprise: distributor,
|
||||
preferred_customer_tags: "member",
|
||||
preferred_variant_tags: "members-only")
|
||||
}
|
||||
let!(:default_tag_rule) {
|
||||
create(:filter_products_tag_rule,
|
||||
enterprise: distributor,
|
||||
is_default: true,
|
||||
preferred_variant_tags: "members-only")
|
||||
}
|
||||
let(:product1) { { "id" => 1, "name" => 'product 1', "variants" => [{ "id" => 4, "tag_list" => ["members-only"] }] } }
|
||||
let(:product2) { { "id" => 2, "name" => 'product 2', "variants" => [{ "id" => 5, "tag_list" => ["members-only"] }, { "id" => 9, "tag_list" => ["something"] }] } }
|
||||
let(:product3) { { "id" => 3, "name" => 'product 3', "variants" => [{ "id" => 6, "tag_list" => ["something-else"] }] } }
|
||||
let(:product2_without_v5) { { "id" => 2, "name" => 'product 2', "variants" => [{ "id" => 9, "tag_list" => ["something"] }] } }
|
||||
let!(:products_array) { [product1, product2, product3] }
|
||||
let!(:products_json) { JSON.unparse( products_array ) }
|
||||
|
||||
before do
|
||||
allow(controller).to receive(:current_order) { order }
|
||||
end
|
||||
|
||||
context "with a preferred visiblity of 'visible', default visibility of 'hidden'" do
|
||||
before { tag_rule.update_attribute(:preferred_matched_variants_visibility, 'visible') }
|
||||
before { default_tag_rule.update_attribute(:preferred_matched_variants_visibility, 'hidden') }
|
||||
|
||||
let(:filtered_products) { JSON.parse(controller.send(:filter, products_json)) }
|
||||
|
||||
context "when the customer is nil" do
|
||||
it "applies default action (hide)" do
|
||||
expect(controller.current_customer).to be nil
|
||||
expect(filtered_products).to include product2_without_v5, product3
|
||||
expect(filtered_products).to_not include product1, product2
|
||||
end
|
||||
end
|
||||
|
||||
context "when the customer's tags match" do
|
||||
before { customer.update_attribute(:tag_list, 'member') }
|
||||
|
||||
it "applies the action (show)" do
|
||||
expect(controller.current_customer).to eq customer
|
||||
expect(filtered_products).to include product1, product2, product3
|
||||
end
|
||||
end
|
||||
|
||||
context "when the customer's tags don't match" do
|
||||
before { customer.update_attribute(:tag_list, 'something') }
|
||||
|
||||
it "applies the default action (hide)" do
|
||||
expect(controller.current_customer).to eq customer
|
||||
expect(filtered_products).to include product2_without_v5, product3
|
||||
expect(filtered_products).to_not include product1, product2
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "with a preferred visiblity of 'hidden', default visibility of 'visible'" do
|
||||
before { tag_rule.update_attribute(:preferred_matched_variants_visibility, 'hidden') }
|
||||
before { default_tag_rule.update_attribute(:preferred_matched_variants_visibility, 'visible') }
|
||||
|
||||
let(:filtered_products) { JSON.parse(controller.send(:filter, products_json)) }
|
||||
|
||||
context "when the customer is nil" do
|
||||
it "applies default action (show)" do
|
||||
expect(controller.current_customer).to be nil
|
||||
expect(filtered_products).to include product1, product2, product3
|
||||
end
|
||||
end
|
||||
|
||||
context "when the customer's tags match" do
|
||||
before { customer.update_attribute(:tag_list, 'member') }
|
||||
|
||||
it "applies the action (hide)" do
|
||||
expect(controller.current_customer).to eq customer
|
||||
expect(filtered_products).to include product2_without_v5, product3
|
||||
expect(filtered_products).to_not include product1, product2
|
||||
end
|
||||
end
|
||||
|
||||
context "when the customer's tags don't match" do
|
||||
before { customer.update_attribute(:tag_list, 'something') }
|
||||
|
||||
it "applies the default action (show)" do
|
||||
expect(controller.current_customer).to eq customer
|
||||
expect(filtered_products).to include product1, product2, product3
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe "Shop API", type: :request do
|
||||
include ShopWorkflow
|
||||
|
||||
describe "filtering products" do
|
||||
let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) }
|
||||
let(:supplier) { create(:supplier_enterprise) }
|
||||
let(:oc1) { create(:simple_order_cycle, distributors: [distributor], coordinator: create(:distributor_enterprise), orders_close_at: 2.days.from_now) }
|
||||
let(:p4) { create(:simple_product, on_demand: false) }
|
||||
let(:p5) { create(:simple_product, on_demand: false) }
|
||||
let(:p6) { create(:simple_product, on_demand: false) }
|
||||
let(:p7) { create(:simple_product, on_demand: false) }
|
||||
let(:v41) { p4.variants.first }
|
||||
let(:v42) { create(:variant, product: p4, unit_value: 3, on_demand: false) }
|
||||
let(:v43) { create(:variant, product: p4, unit_value: 4, on_demand: true) }
|
||||
let(:v51) { p5.variants.first }
|
||||
let(:v52) { create(:variant, product: p5) }
|
||||
let(:v61) { p6.variants.first }
|
||||
let(:v71) { p7.variants.first }
|
||||
let(:order) { create(:order, distributor: distributor, order_cycle: oc1) }
|
||||
|
||||
before do
|
||||
set_order order
|
||||
|
||||
v61.update_attribute(:on_hand, 1)
|
||||
p6.destroy
|
||||
v71.update_attribute(:on_hand, 1)
|
||||
v41.update_attribute(:on_hand, 1)
|
||||
v42.update_attribute(:on_hand, 0)
|
||||
v43.update_attribute(:on_hand, 0)
|
||||
v51.update_attribute(:on_hand, 1)
|
||||
v52.update_attribute(:on_hand, 0)
|
||||
v71.update_attribute(:on_hand, 1)
|
||||
v71.update_attribute(:deleted_at, Time.zone.now)
|
||||
exchange = Exchange.find(oc1.exchanges.to_enterprises(distributor).outgoing.first.id)
|
||||
exchange.update_attribute :pickup_time, "frogs"
|
||||
exchange.variants << v61
|
||||
exchange.variants << v41
|
||||
exchange.variants << v42
|
||||
exchange.variants << v43
|
||||
# v51 is in stock but not in distribution
|
||||
# v52 is out of stock and in the distribution
|
||||
# Neither should display, nor should their product, p5
|
||||
exchange.variants << v52
|
||||
exchange.variants << v71
|
||||
get products_shop_path
|
||||
end
|
||||
|
||||
it "filters products based on availability" do
|
||||
# It shows on demand variants
|
||||
expect(response.body).to include v43.options_text
|
||||
# It does not show variants that are neither on hand or on demand
|
||||
expect(response.body).not_to include v42.options_text
|
||||
# It does not show products that have no available variants in this distribution
|
||||
expect(response.body).not_to include p5.name
|
||||
# It does not show deleted products
|
||||
expect(response.body).not_to include p6.name
|
||||
# It does not show deleted variants
|
||||
expect(response.body).not_to include v71.name
|
||||
expect(response.body).not_to include p7.name
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user