From 58fdc48b9f5927e3e0115446833b73532bf794b0 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 20 Nov 2014 12:28:26 +1100 Subject: [PATCH] Display products in table --- .../override_variants_controller.js.coffee | 4 +++- .../admin/products_controller_decorator.rb | 2 ++ app/helpers/admin/injection_helper.rb | 4 ++++ app/models/spree/product_decorator.rb | 3 +++ .../products/override_variants.html.haml | 2 ++ .../override_variants/_data.html.haml | 3 ++- .../override_variants/_products.html.haml | 13 +++++++++++ spec/features/admin/override_variants_spec.rb | 23 ++++++++++++++++--- ...verride_variants_controller_spec.js.coffee | 6 +++-- 9 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 app/views/spree/admin/products/override_variants/_products.html.haml 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 69bc5fb8cd..65426a0422 100644 --- a/app/assets/javascripts/admin/controllers/override_variants_controller.js.coffee +++ b/app/assets/javascripts/admin/controllers/override_variants_controller.js.coffee @@ -1,6 +1,8 @@ -angular.module("ofn.admin").controller "AdminOverrideVariantsCtrl", ($scope, hubs) -> +angular.module("ofn.admin").controller "AdminOverrideVariantsCtrl", ($scope, Indexer, hubs, producers, products) -> $scope.hubs = hubs $scope.hub = null + $scope.products = products + $scope.producers = Indexer.index producers $scope.selectHub = -> $scope.hub = (hub for hub in hubs when hub.id == $scope.hub_id)[0] \ No newline at end of file diff --git a/app/controllers/spree/admin/products_controller_decorator.rb b/app/controllers/spree/admin/products_controller_decorator.rb index 1bf90fb74a..cfd4649fc1 100644 --- a/app/controllers/spree/admin/products_controller_decorator.rb +++ b/app/controllers/spree/admin/products_controller_decorator.rb @@ -51,6 +51,8 @@ Spree::Admin::ProductsController.class_eval do def override_variants @hubs = order_cycle_hub_enterprises(without_validation: true) + @producers = order_cycle_producer_enterprises + @products = Spree::Product.not_deleted.where(supplier_id: @producers).by_producer.by_name end diff --git a/app/helpers/admin/injection_helper.rb b/app/helpers/admin/injection_helper.rb index 3d0b68f450..94ee23347a 100644 --- a/app/helpers/admin/injection_helper.rb +++ b/app/helpers/admin/injection_helper.rb @@ -33,6 +33,10 @@ module Admin admin_inject_json_ams_array "ofn.admin", "producers", @producers, Api::Admin::IdNameSerializer end + def admin_inject_products + admin_inject_json_ams_array "ofn.admin", "products", @products, Spree::Api::ProductSerializer + end + def admin_inject_taxons admin_inject_json_ams_array "ofn.admin", "taxons", @taxons, Api::Admin::TaxonSerializer end diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 5181bf776d..5b3460a073 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -87,6 +87,9 @@ Spree::Product.class_eval do merge(Exchange.outgoing). where('order_cycles.id IS NOT NULL') } + scope :by_producer, joins(:supplier).order('enterprises.name') + scope :by_name, order('name') + scope :managed_by, lambda { |user| if user.has_spree_role?('admin') scoped diff --git a/app/views/spree/admin/products/override_variants.html.haml b/app/views/spree/admin/products/override_variants.html.haml index 51c6e6d0d4..a3165f8850 100644 --- a/app/views/spree/admin/products/override_variants.html.haml +++ b/app/views/spree/admin/products/override_variants.html.haml @@ -5,3 +5,5 @@ = render 'spree/admin/products/override_variants/hub_choice' %h2{ng: {show: 'hub'}} {{ hub.name }} + + = render 'spree/admin/products/override_variants/products' diff --git a/app/views/spree/admin/products/override_variants/_data.html.haml b/app/views/spree/admin/products/override_variants/_data.html.haml index cc615dc3cd..09d8aca54e 100644 --- a/app/views/spree/admin/products/override_variants/_data.html.haml +++ b/app/views/spree/admin/products/override_variants/_data.html.haml @@ -1,2 +1,3 @@ = admin_inject_hubs - += admin_inject_producers += admin_inject_products diff --git a/app/views/spree/admin/products/override_variants/_products.html.haml b/app/views/spree/admin/products/override_variants/_products.html.haml new file mode 100644 index 0000000000..a5abae6ba1 --- /dev/null +++ b/app/views/spree/admin/products/override_variants/_products.html.haml @@ -0,0 +1,13 @@ +%table.index.bulk{ng: {show: 'hub'}} + %thead + %tr + %th Producer + %th Product + %th Price + %th On hand + %tbody + %tr{ng: {repeat: 'product in products'}} + %td {{ producers[product.producer].name }} + %td {{ product.name }} + %td {{ product.price }} + %td {{ product.on_hand }} diff --git a/spec/features/admin/override_variants_spec.rb b/spec/features/admin/override_variants_spec.rb index 0e84321a97..0372fed47d 100644 --- a/spec/features/admin/override_variants_spec.rb +++ b/spec/features/admin/override_variants_spec.rb @@ -15,10 +15,10 @@ feature %q{ use_short_wait - describe "selecting a hub" do - let!(:hub) { create(:distributor_enterprise) } - let!(:producer) { create(:supplier_enterprise) } + let!(:hub) { create(:distributor_enterprise) } + let!(:producer) { create(:supplier_enterprise) } + 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] @@ -32,4 +32,21 @@ feature %q{ page.should have_selector 'h2', text: hub.name end end + + context "when a hub is selected" do + let!(:product) { create(:simple_product, supplier: producer, 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 "products values are affected by overrides" + end end diff --git a/spec/javascripts/unit/admin/controllers/override_variants_controller_spec.js.coffee b/spec/javascripts/unit/admin/controllers/override_variants_controller_spec.js.coffee index 6a3d895256..8c5144454b 100644 --- a/spec/javascripts/unit/admin/controllers/override_variants_controller_spec.js.coffee +++ b/spec/javascripts/unit/admin/controllers/override_variants_controller_spec.js.coffee @@ -2,13 +2,15 @@ describe "OverrideVariantsCtrl", -> ctrl = null scope = null hubs = [{id: 1, name: 'Hub'}] + producers = [{id: 2, name: 'Producer'}] + products = [{id: 1, name: 'Product'}] beforeEach -> module 'ofn.admin' scope = {} - inject ($controller)-> - ctrl = $controller 'AdminOverrideVariantsCtrl', {$scope: scope, hubs: hubs} + inject ($controller, Indexer) -> + ctrl = $controller 'AdminOverrideVariantsCtrl', {$scope: scope, Indexer: Indexer, hubs: hubs, producers: producers, products: products} it "initialises the hub list and the chosen hub", -> expect(scope.hubs).toEqual hubs