From 9a291e28ecd72f25ccd7eeac7c79ee11bb161ab3 Mon Sep 17 00:00:00 2001 From: Rob H Date: Wed, 12 Mar 2014 18:25:51 +1100 Subject: [PATCH] Add bulk action checkboxes to BOM --- .../admin/bulk_order_management.js.coffee | 11 ++++++++ .../admin/orders/bulk_management.html.haml | 4 +++ .../admin/bulk_order_management_spec.rb | 25 ++++++++++++++++- .../unit/bulk_order_management_spec.js.coffee | 27 +++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/admin/bulk_order_management.js.coffee b/app/assets/javascripts/admin/bulk_order_management.js.coffee index 693d68d236..b2544cd0ad 100644 --- a/app/assets/javascripts/admin/bulk_order_management.js.coffee +++ b/app/assets/javascripts/admin/bulk_order_management.js.coffee @@ -157,6 +157,7 @@ orderManagementModule.controller "AdminOrderMgmtCtrl", [ $scope.lineItems = $scope.orders.reduce (lineItems,order) -> orderWithoutLineItems = $scope.lineItemOrder order for i,line_item of order.line_items + line_item.checked = false line_item.supplier = $scope.matchObject $scope.suppliers, line_item.supplier, null line_item.order = orderWithoutLineItems lineItems.concat order.line_items @@ -190,6 +191,16 @@ orderManagementModule.controller "AdminOrderMgmtCtrl", [ ).success (data) -> $scope.lineItems.splice $scope.lineItems.indexOf(lineItem), 1 lineItem.order.line_items.splice lineItem.order.line_items.indexOf(lineItem), 1 + + $scope.allBoxesChecked = -> + checkedCount = $scope.lineItems.reduce (count,lineItem) -> + count + (if lineItem.checked then 1 else 0 ) + , 0 + checkedCount == $scope.lineItems.length + + $scope.toggleAllCheckboxes = -> + changeTo = !$scope.allBoxesChecked() + lineItem.checked = changeTo for lineItem in $scope.lineItems ] orderManagementModule.filter "selectFilter", [ diff --git a/app/views/spree/admin/orders/bulk_management.html.haml b/app/views/spree/admin/orders/bulk_management.html.haml index e79a7a7f5d..a273ffef2a 100644 --- a/app/views/spree/admin/orders/bulk_management.html.haml +++ b/app/views/spree/admin/orders/bulk_management.html.haml @@ -45,6 +45,8 @@ %table.index#listing_orders.bulk %thead %tr + %th.bulk + %input{ :type => "checkbox", :name => 'toggle_bulk', 'ng-click' => 'toggleAllCheckboxes()', 'ng-checked' => "allBoxesChecked()" } %th.full_name Name %th.date Order Date %th.producer Producer @@ -55,6 +57,8 @@ Ask?  %input{ :type => 'checkbox', 'ng-model' => "confirmDelete" } %tr.line_item{ 'ng-repeat' => 'line_item in lineItems | filter:quickSearch | selectFilter:supplierFilter:distributorFilter:orderCycleFilter:quickSearch', 'ng-class-even' => "'even'", 'ng-class-odd' => "'odd'", :id => "li_{{line_item.id}}" } + %td.bulk + %input.bulk{ :type => "checkbox", 'ng-model' => 'line_item.checked' } %td.full_name {{ line_item.order.full_name }} %td.date {{ line_item.order.completed_at }} %td.producer {{ line_item.supplier.name }} diff --git a/spec/features/admin/bulk_order_management_spec.rb b/spec/features/admin/bulk_order_management_spec.rb index 17a8257c83..3b398ed52d 100644 --- a/spec/features/admin/bulk_order_management_spec.rb +++ b/spec/features/admin/bulk_order_management_spec.rb @@ -86,7 +86,7 @@ feature %q{ end it "displays a column for variant description, which shows only product name when options text is blank" do - page.should have_selector "th.variant", text: "PRODUCT (UNIT): VAR", :visible => true + page.should have_selector "th.variant", text: "PRODUCT: UNIT", :visible => true page.should have_selector "td.variant", text: li1.product.name, :visible => true page.should have_selector "td.variant", text: (li2.product.name + ": " + li2.variant.options_text), :visible => true end @@ -349,6 +349,29 @@ feature %q{ end end + context "bulk actions" do + let!(:o1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) } + let!(:o2) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) } + let!(:li1) { FactoryGirl.create(:line_item, order: o1 ) } + let!(:li2) { FactoryGirl.create(:line_item, order: o2 ) } + + before :each do + visit '/admin/orders/bulk_management' + end + + it "displays a checkbox for each line item in the list" do + page.should have_selector "tr#li_#{li1.id} input[type='checkbox'].bulk" + page.should have_selector "tr#li_#{li2.id} input[type='checkbox'].bulk" + end + + it "displays a checkbox to which toggles the 'checked' state of all checkboxes" do + check "toggle_bulk" + page.all("input[type='checkbox'].bulk").each{ |checkbox| checkbox.checked?.should == true } + uncheck "toggle_bulk" + page.all("input[type='checkbox'].bulk").each{ |checkbox| checkbox.checked?.should == false } + end + end + context "using action buttons" do context "using delete buttons" do let!(:o1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) } diff --git a/spec/javascripts/unit/bulk_order_management_spec.js.coffee b/spec/javascripts/unit/bulk_order_management_spec.js.coffee index 1c55777e9a..9f3ab0d398 100644 --- a/spec/javascripts/unit/bulk_order_management_spec.js.coffee +++ b/spec/javascripts/unit/bulk_order_management_spec.js.coffee @@ -209,6 +209,33 @@ describe "AdminOrderMgmtCtrl", -> httpBackend.flush() expect(order.line_items).toEqual [line_item1, line_item2] + describe "check boxes for line items", -> + line_item1 = line_item2 = null + + beforeEach -> + line_item1 = { name: "line item 1", checked: false } + line_item2 = { name: "line item 2", checked: false } + scope.lineItems = [ line_item1, line_item2 ] + + it "keeps track of whether all lines items are 'checked' or not", -> + expect(scope.allBoxesChecked()).toEqual false + line_item1.checked = true + expect(scope.allBoxesChecked()).toEqual false + line_item2.checked = true + expect(scope.allBoxesChecked()).toEqual true + line_item1.checked = false + expect(scope.allBoxesChecked()).toEqual false + + it "toggles the 'checked' attribute of all line items based to the value of allBoxesChecked", -> + scope.toggleAllCheckboxes() + expect(scope.allBoxesChecked()).toEqual true + line_item1.checked = false + expect(scope.allBoxesChecked()).toEqual false + scope.toggleAllCheckboxes() + expect(scope.allBoxesChecked()).toEqual true + scope.toggleAllCheckboxes() + expect(scope.allBoxesChecked()).toEqual false + describe "managing pending changes", -> dataSubmitter = pendingChangesService = null