From 76d4fbccf990cdc8ff4b2e583a7cc2ae93902ed6 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 11 Dec 2015 07:38:53 +1100 Subject: [PATCH] Use select2 for order cycle selection --- .../admin/orders_controller_decorator.rb | 7 +++- .../add_distribution_fields.html.haml.deface | 4 +- .../spree/admin/line_items/create.js.erb | 1 + spec/features/admin/orders_spec.rb | 16 ++++---- spec/support/matchers/select2_matchers.rb | 40 ++++++++++++++++++- 5 files changed, 55 insertions(+), 13 deletions(-) diff --git a/app/controllers/spree/admin/orders_controller_decorator.rb b/app/controllers/spree/admin/orders_controller_decorator.rb index 4d336dfb4e..b5a2bb4f95 100644 --- a/app/controllers/spree/admin/orders_controller_decorator.rb +++ b/app/controllers/spree/admin/orders_controller_decorator.rb @@ -118,6 +118,11 @@ Spree::Admin::OrdersController.class_eval do def load_distribution_choices @shops = Enterprise.is_distributor.managed_by(spree_current_user).by_name - @order_cycles = OrderCycle.managed_by(spree_current_user).by_name + + ocs = OrderCycle.managed_by(spree_current_user) + @order_cycles = ocs.soonest_closing + + ocs.soonest_opening + + ocs.closed + + ocs.undated end end diff --git a/app/overrides/spree/admin/orders/_form/add_distribution_fields.html.haml.deface b/app/overrides/spree/admin/orders/_form/add_distribution_fields.html.haml.deface index af5013f040..08670edc55 100644 --- a/app/overrides/spree/admin/orders/_form/add_distribution_fields.html.haml.deface +++ b/app/overrides/spree/admin/orders/_form/add_distribution_fields.html.haml.deface @@ -19,11 +19,11 @@ .alpha.six.columns .field %label{for: "order_distributor_id"} Distributor - %select{id: "order_distributor_id", name: "order[distributor_id]", 'ng-model' => 'distributor_id'} + %select.fullwidth{id: "order_distributor_id", name: "order[distributor_id]", 'ng-model' => 'distributor_id'} %option{"ng-repeat" => "shop in shops", "ng-value" => "shop.id", "ng-selected" => "distributor_id == shop.id", "ng-disabled" => "!distributorHasOrderCycles(shop)", "ng-bind" => "shop.name"} .omega.six.columns .field{"ng-show" => "distributor_id"} %label{for: "order_order_cycle_id"} Order Cycle - %select{id: "order_order_cycle_id", name: "order[order_cycle_id]", 'ng-model' => 'order_cycle_id'} + %select.select2.fullwidth{id: "order_order_cycle_id", name: "order[order_cycle_id]", 'ng-model' => 'order_cycle_id'} %option{"ng-repeat" => "oc in orderCycles | filter:validOrderCycle", "ng-value" => "oc.id", "ng-selected" => "order_cycle_id == oc.id", "ng-bind" => "oc.name"} diff --git a/app/views/spree/admin/line_items/create.js.erb b/app/views/spree/admin/line_items/create.js.erb index 58afe5c8ef..f1644d2203 100644 --- a/app/views/spree/admin/line_items/create.js.erb +++ b/app/views/spree/admin/line_items/create.js.erb @@ -3,3 +3,4 @@ var scope = angular.element("#order-form-wrapper").scope(); $("#order-form-wrapper").html(scope.$compile('<%= escape_javascript(render :partial => "spree/admin/orders/form") %>')(scope)); scope.$apply(); +$('select.select2').select2({allowClear: true}); diff --git a/spec/features/admin/orders_spec.rb b/spec/features/admin/orders_spec.rb index 6554c614a6..d8dfa469a1 100644 --- a/spec/features/admin/orders_spec.rb +++ b/spec/features/admin/orders_spec.rb @@ -34,10 +34,10 @@ feature %q{ page.should have_selector "option[value='#{distributor_disabled.id}'][disabled='disabled']" # When we select a distributor, it should limit order cycle selection to those for that distributor - page.should_not have_select 'order_order_cycle_id' + page.should_not have_select2 'order_order_cycle_id' select @distributor.name, from: 'order_distributor_id' - page.should have_select 'order_order_cycle_id', options: ['', 'One'] - select @order_cycle.name, from: 'order_order_cycle_id' + page.should have_select2 'order_order_cycle_id', options: ['', 'One'] + select2_select @order_cycle.name, from: 'order_order_cycle_id' page.should have_content 'ADD PRODUCT' targetted_select2_search @product.name, from: '#add_variant_id', dropdown_css: '.select2-drop' @@ -81,7 +81,7 @@ feature %q{ click_edit select d.name, from: 'order_distributor_id' - select oc.name, from: 'order_order_cycle_id' + select2_select oc.name, from: 'order_order_cycle_id' click_button 'Update And Recalculate Fees' @@ -107,7 +107,7 @@ feature %q{ page.find('td.actions a.icon-edit').click page.should have_no_select 'order_distributor_id' - page.should have_no_select 'order_order_cycle_id' + page.should_not have_select2 'order_order_cycle_id' page.should have_selector 'p', text: "Distributor: #{@order.distributor.name}" page.should have_selector 'p', text: "Order cycle: None" @@ -160,7 +160,7 @@ feature %q{ click_link 'New Order' select distributor1.name, from: 'order_distributor_id' - select order_cycle1.name, from: 'order_order_cycle_id' + select2_select order_cycle1.name, from: 'order_order_cycle_id' expect(page).to have_content 'ADD PRODUCT' targetted_select2_search product.name, from: '#add_variant_id', dropdown_css: '.select2-drop' @@ -172,8 +172,8 @@ feature %q{ expect(page).to have_select 'order_distributor_id', with_options: [distributor1.name] expect(page).to_not have_select 'order_distributor_id', with_options: [distributor2.name] - expect(page).to have_select 'order_order_cycle_id', with_options: [order_cycle1.name] - expect(page).to_not have_select 'order_order_cycle_id', with_options: [order_cycle2.name] + expect(page).to have_select2 'order_order_cycle_id', with_options: [order_cycle1.name] + expect(page).to_not have_select2 'order_order_cycle_id', with_options: [order_cycle2.name] click_button 'Update' diff --git a/spec/support/matchers/select2_matchers.rb b/spec/support/matchers/select2_matchers.rb index cf67c7c7cb..e1f821753d 100644 --- a/spec/support/matchers/select2_matchers.rb +++ b/spec/support/matchers/select2_matchers.rb @@ -28,15 +28,43 @@ RSpec::Matchers.define :have_select2 do |id, options={}| end failure_message_for_should do |actual| - message = "expected to find select2 ##{@id}" + message = "expected to find select2 ##{@id}" message += " with #{@options.inspect}" if @options.any? message end match_for_should_not do |node| - raise "Not yet implemented" + @id, @options, @node = id, options, node + + #id = find_label_by_text(locator) + from = "#s2id_#{id}" + + results = [] + + results << node.has_no_selector?(from, wait: 1) + + # if results.all? + # results << selected_option_is(from, options[:selected]) if options.key? :selected + # end + + if results.none? + results << all_options_absent(from, options[:with_options]) if options.key? :with_options + #results << exact_options_present(from, options[:options]) if options.key? :options + #results << no_options_present(from, options[:without_options]) if options.key? :without_options + end + + if (options.keys & %i(selected options without_options)).any? + raise "Not yet implemented" + end + + results.any? end + failure_message_for_should_not do |actual| + message = "expected not to find select2 ##{@id}" + message += " with #{@options.inspect}" if @options.any? + message + end def all_options_present(from, options) with_select2_open(from) do @@ -46,6 +74,14 @@ RSpec::Matchers.define :have_select2 do |id, options={}| end end + def all_options_absent(from, options) + with_select2_open(from) do + options.all? do |option| + @node.has_no_selector? "div.select2-drop-active ul.select2-results li", text: option + end + end + end + def exact_options_present(from, options) with_select2_open(from) do @node.all("div.select2-drop-active ul.select2-results li").map(&:text) == options