From fce7714994acf7fe0d8e00b452748ce82b7e1bbf Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 4 Feb 2015 11:20:04 +1100 Subject: [PATCH] Load correct set of variant override producers, use variant_override_enterprises_per_hub, tighten specs --- .../admin/variant_overrides_controller.rb | 11 ++++-- spec/features/admin/variant_overrides_spec.rb | 38 ++++++++++++------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/app/controllers/admin/variant_overrides_controller.rb b/app/controllers/admin/variant_overrides_controller.rb index e46d5b1a6b..234c2779c4 100644 --- a/app/controllers/admin/variant_overrides_controller.rb +++ b/app/controllers/admin/variant_overrides_controller.rb @@ -2,15 +2,20 @@ require 'open_food_network/spree_api_key_loader' module Admin class VariantOverridesController < ResourceController - include OpenFoodNetwork::SpreeApiKeyLoader include OrderCyclesHelper + include OpenFoodNetwork::SpreeApiKeyLoader + before_filter :load_spree_api_key, only: :index def index @hubs = order_cycle_hub_enterprises(without_validation: true) - @producers = order_cycle_producer_enterprises + + # Used in JS to look up the name of the producer of each product + @producers = OpenFoodNetwork::Permissions.new(spree_current_user). + variant_override_producers + @hub_permissions = OpenFoodNetwork::Permissions.new(spree_current_user). - order_cycle_enterprises_per_hub + variant_override_enterprises_per_hub @variant_overrides = VariantOverride.for_hubs(@hubs) end diff --git a/spec/features/admin/variant_overrides_spec.rb b/spec/features/admin/variant_overrides_spec.rb index aa2338a219..886f7d7e5b 100644 --- a/spec/features/admin/variant_overrides_spec.rb +++ b/spec/features/admin/variant_overrides_spec.rb @@ -12,16 +12,17 @@ feature %q{ let!(:hub) { create(:distributor_enterprise) } let!(:hub2) { create(:distributor_enterprise) } let!(:producer) { create(:supplier_enterprise) } - let!(:er1) { create(:enterprise_relationship, parent: producer, child: hub, - permissions_list: [:add_to_order_cycle]) } + let!(:er1) { create(:enterprise_relationship, parent: hub, child: producer, + permissions_list: [:add_to_order_cycle]) } context "as an enterprise user" do - let(:user) { create_enterprise_user enterprises: [hub, hub2, producer] } + let(:user) { create_enterprise_user enterprises: [hub2, producer] } before { quick_login_as user } describe "selecting a hub" do it "displays a list of hub choices" do visit '/admin/variant_overrides' + page.should have_select2 'hub_id', options: ['', hub.name, hub2.name] end @@ -37,10 +38,16 @@ feature %q{ context "when a hub is selected" do let!(:product) { create(:simple_product, supplier: producer, variant_unit: 'weight', variant_unit_scale: 1) } let!(:variant) { create(:variant, product: product, unit_value: 1, price: 1.23, on_hand: 12) } - let!(:producer2) { create(:supplier_enterprise) } - let!(:product2) { create(:simple_product, supplier: producer2) } - let!(:er2) { create(:enterprise_relationship, parent: producer2, child: hub2, - permissions_list: [:add_to_order_cycle]) } + + let!(:producer_related) { create(:supplier_enterprise) } + let!(:product_related) { create(:simple_product, supplier: producer_related) } + let!(:variant_related) { create(:variant, product: product_related, unit_value: 2, price: 2.34, on_hand: 23) } + let!(:er2) { create(:enterprise_relationship, parent: producer_related, child: hub, + permissions_list: [:create_variant_overrides]) } + + let!(:producer_unrelated) { create(:supplier_enterprise) } + let!(:product_unrelated) { create(:simple_product, supplier: producer_unrelated) } + before do # Remove 'S' option value @@ -56,14 +63,19 @@ feature %q{ it "displays the list of products with variants" do page.should have_table_row ['PRODUCER', 'PRODUCT', 'PRICE', 'ON HAND'] + page.should have_table_row [producer.name, product.name, '', ''] page.should have_input "variant-overrides-#{variant.id}-price", placeholder: '1.23' page.should have_input "variant-overrides-#{variant.id}-count-on-hand", placeholder: '12' + + page.should have_table_row [producer_related.name, product_related.name, '', ''] + page.should have_input "variant-overrides-#{variant_related.id}-price", placeholder: '2.34' + page.should have_input "variant-overrides-#{variant_related.id}-count-on-hand", placeholder: '23' end - it "filters the products to those the hub can add to an order cycle" do - page.should_not have_content producer2.name - page.should_not have_content product2.name + it "filters the products to those the hub can override" do + page.should_not have_content producer_unrelated.name + page.should_not have_content product_unrelated.name end it "creates new overrides" do @@ -129,12 +141,10 @@ feature %q{ end it "displays an error when unauthorised to update a particular override" do - fill_in "variant-overrides-#{variant.id}-price", with: '777.77' - fill_in "variant-overrides-#{variant.id}-count-on-hand", with: '123' + fill_in "variant-overrides-#{variant_related.id}-price", with: '777.77' + fill_in "variant-overrides-#{variant_related.id}-count-on-hand", with: '123' page.should have_content "Changes to one override remain unsaved." - EnterpriseRole.where(user_id: user).where('enterprise_id != ?', producer).destroy_all - er1.destroy er2.destroy expect do