From a3a3832c8dab56c14bd44c3ff4f068c52648ab6a Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 26 Nov 2014 11:51:52 +1100 Subject: [PATCH] Only show products that the chosen hub can add to an order cycle --- .../override_variants_controller.js.coffee | 4 +- .../filters/hub_permissions_filter.js.coffee | 4 + .../override_variants/_products.html.haml | 2 +- spec/features/admin/override_variants_spec.rb | 73 ++++++++++--------- 4 files changed, 44 insertions(+), 39 deletions(-) create mode 100644 app/assets/javascripts/admin/filters/hub_permissions_filter.js.coffee diff --git a/app/assets/javascripts/admin/controllers/override_variants_controller.js.coffee b/app/assets/javascripts/admin/controllers/override_variants_controller.js.coffee index 871cbb9b03..d4bd2d73ba 100644 --- a/app/assets/javascripts/admin/controllers/override_variants_controller.js.coffee +++ b/app/assets/javascripts/admin/controllers/override_variants_controller.js.coffee @@ -1,9 +1,9 @@ -angular.module("ofn.admin").controller "AdminOverrideVariantsCtrl", ($scope, Indexer, SpreeApiAuth, PagedFetcher, hubs, producers) -> +angular.module("ofn.admin").controller "AdminOverrideVariantsCtrl", ($scope, Indexer, SpreeApiAuth, PagedFetcher, hubs, producers, hubPermissions) -> $scope.hubs = hubs $scope.hub = null $scope.products = [] $scope.producers = Indexer.index producers - + $scope.hubPermissions = hubPermissions $scope.initialise = -> SpreeApiAuth.authorise() diff --git a/app/assets/javascripts/admin/filters/hub_permissions_filter.js.coffee b/app/assets/javascripts/admin/filters/hub_permissions_filter.js.coffee new file mode 100644 index 0000000000..5db7a6d40e --- /dev/null +++ b/app/assets/javascripts/admin/filters/hub_permissions_filter.js.coffee @@ -0,0 +1,4 @@ +angular.module("ofn.admin").filter "hubPermissions", ($filter) -> + return (products, hubPermissions, hub_id) -> + return [] if !hub_id + return $filter('filter')(products, ((product) -> hubPermissions[hub_id].indexOf(product.producer_id) > -1), true) diff --git a/app/views/spree/admin/products/override_variants/_products.html.haml b/app/views/spree/admin/products/override_variants/_products.html.haml index f5081e54bd..6388656654 100644 --- a/app/views/spree/admin/products/override_variants/_products.html.haml +++ b/app/views/spree/admin/products/override_variants/_products.html.haml @@ -6,7 +6,7 @@ %th Price %th On hand %tbody - %tr{ng: {repeat: 'product in products'}} + %tr{ng: {repeat: 'product in products | hubPermissions:hubPermissions:hub.id'}} %td {{ producers[product.producer_id].name }} %td {{ product.name }} %td {{ product.price }} diff --git a/spec/features/admin/override_variants_spec.rb b/spec/features/admin/override_variants_spec.rb index 1b6169a40f..1ae981a8aa 100644 --- a/spec/features/admin/override_variants_spec.rb +++ b/spec/features/admin/override_variants_spec.rb @@ -9,10 +9,6 @@ feature %q{ include AuthenticationWorkflow include WebHelper - before do - login_to_admin_section - end - use_short_wait let!(:hub) { create(:distributor_enterprise) } @@ -20,41 +16,46 @@ feature %q{ let!(:er) { create(:enterprise_relationship, parent: producer, child: hub, permissions_list: [:add_to_order_cycle]) } - describe "selecting a hub" do - it "displays a list of hub choices" do - visit '/admin/products/override_variants' - page.should have_select2 'hub_id', options: ['', hub.name] + context "as an enterprise user" do + let(:user) { create_enterprise_user enterprises: [hub, producer] } + before { quick_login_as user } + + describe "selecting a hub" do + it "displays a list of hub choices" do + visit '/admin/products/override_variants' + page.should have_select2 'hub_id', options: ['', hub.name] + end + + it "displays the hub" do + visit '/admin/products/override_variants' + select2_select hub.name, from: 'hub_id' + click_button 'Go' + + page.should have_selector 'h2', text: hub.name + end end - it "displays the hub" do - visit '/admin/products/override_variants' - select2_select hub.name, from: 'hub_id' - click_button 'Go' + context "when a hub is selected" do + let!(:product) { create(:simple_product, supplier: producer, price: 1.23, on_hand: 12) } + let!(:producer2) { create(:supplier_enterprise) } + let!(:product2) { create(:simple_product, supplier: producer2, price: 1.23, on_hand: 12) } - page.should have_selector 'h2', text: hub.name + before do + visit '/admin/products/override_variants' + select2_select hub.name, from: 'hub_id' + click_button 'Go' + end + + it "displays the list of products" do + page.should have_table_row ['PRODUCER', 'PRODUCT', 'PRICE', 'ON HAND'] + page.should have_table_row [producer.name, product.name, '1.23', '12'] + end + + it "filters the products to those the hub can add to an order cycle" do + page.should_not have_table_row [producer2.name, product2.name, '1.23', '12'] + end + + it "products values are affected by overrides" end end - - context "when a hub is selected" do - let!(:product) { create(:simple_product, supplier: producer, price: 1.23, on_hand: 12) } - let!(:producer2) { create(:supplier_enterprise) } - let!(:product2) { create(:simple_product, supplier: producer2, price: 1.23, on_hand: 12) } - - before do - visit '/admin/products/override_variants' - select2_select hub.name, from: 'hub_id' - click_button 'Go' - end - - it "displays the list of products" do - page.should have_table_row ['PRODUCER', 'PRODUCT', 'PRICE', 'ON HAND'] - page.should have_table_row [producer.name, product.name, '1.23', '12'] - end - - it "filters the products to those the hub can add to an order cycle" do - page.should_not have_table_row [producer2.name, product2.name, '1.23', '12'] - end - - it "products values are affected by overrides" - end end