From e59077e63e6daff67fd5c6a235a4cbb76e59dfcf Mon Sep 17 00:00:00 2001 From: cyrillefr Date: Wed, 18 Dec 2019 09:54:23 +0100 Subject: [PATCH] Select by default single Hub/Shop option on creation of payment/shipping method - added a helper - added mode(new/edit) in payment/shipping views - updated checkbox creation - added tests --- app/helpers/spree/admin/base_helper_decorator.rb | 8 ++++++++ .../spree/admin/payment_methods/edit.html.haml | 2 +- .../spree/admin/payment_methods/new.html.haml | 2 +- .../spree/admin/shared/_hubs_sidebar.html.haml | 2 +- .../spree/admin/shipping_methods/edit.html.haml | 2 +- .../spree/admin/shipping_methods/new.html.haml | 2 +- spec/features/admin/payment_method_spec.rb | 15 +++++++++++++++ spec/features/admin/shipping_methods_spec.rb | 14 ++++++++++++++ 8 files changed, 42 insertions(+), 5 deletions(-) diff --git a/app/helpers/spree/admin/base_helper_decorator.rb b/app/helpers/spree/admin/base_helper_decorator.rb index 95016b3c88..88a835bfa0 100644 --- a/app/helpers/spree/admin/base_helper_decorator.rb +++ b/app/helpers/spree/admin/base_helper_decorator.rb @@ -21,6 +21,14 @@ module Spree link_to_with_icon('icon-trash', name, '#', html_options) + f.hidden_field(:_destroy) end + + def add_check_if_single(mode, count) + if mode == :new && count == 1 + { checked: true } + else + {} + end + end end end end diff --git a/app/views/spree/admin/payment_methods/edit.html.haml b/app/views/spree/admin/payment_methods/edit.html.haml index 9d8f6b147d..7ddac37d93 100644 --- a/app/views/spree/admin/payment_methods/edit.html.haml +++ b/app/views/spree/admin/payment_methods/edit.html.haml @@ -12,7 +12,7 @@ %fieldset.no-border-top = render partial: 'form', locals: { f: f } .one.column   - = render partial: 'spree/admin/shared/hubs_sidebar', locals: { f: f, klass: :payment_method } + = render partial: 'spree/admin/shared/hubs_sidebar', locals: { f: f, klass: :payment_method, mode: :edit } .clear .filter-actions.actions = button t(:update), 'icon-refresh' diff --git a/app/views/spree/admin/payment_methods/new.html.haml b/app/views/spree/admin/payment_methods/new.html.haml index c3039cc169..a9752371af 100644 --- a/app/views/spree/admin/payment_methods/new.html.haml +++ b/app/views/spree/admin/payment_methods/new.html.haml @@ -8,7 +8,7 @@ %fieldset.no-border-top = render partial: 'form', locals: { f: f } .one.column   - = render partial: 'spree/admin/shared/hubs_sidebar', locals: { f: f, klass: :payment_method } + = render partial: 'spree/admin/shared/hubs_sidebar', locals: { f: f, klass: :payment_method, mode: :new } .clear .filter-actions.actions = button t(:create), 'icon-ok' diff --git a/app/views/spree/admin/shared/_hubs_sidebar.html.haml b/app/views/spree/admin/shared/_hubs_sidebar.html.haml index 75ef9bb32f..306827ce6a 100644 --- a/app/views/spree/admin/shared/_hubs_sidebar.html.haml +++ b/app/views/spree/admin/shared/_hubs_sidebar.html.haml @@ -12,7 +12,7 @@ %span.four.columns %span.three.columns.alpha %label - = check_box klass, :distributor_ids, { multiple: true }, hub.id, nil + = check_box klass, :distributor_ids, { multiple: true }.merge(add_check_if_single(mode, @hubs.count)), hub.id, nil = hub.name %a.one.column.omega{ href: "#{main_app.edit_admin_enterprise_path(hub)}" } %span.icon-arrow-right diff --git a/app/views/spree/admin/shipping_methods/edit.html.haml b/app/views/spree/admin/shipping_methods/edit.html.haml index dc6d879e45..26eadb3afd 100644 --- a/app/views/spree/admin/shipping_methods/edit.html.haml +++ b/app/views/spree/admin/shipping_methods/edit.html.haml @@ -12,7 +12,7 @@ %fieldset.no-border-top = render partial: 'form', locals: { f: f } .one.column   - = render partial: 'spree/admin/shared/hubs_sidebar', locals: { f: f, klass: :shipping_method } + = render partial: 'spree/admin/shared/hubs_sidebar', locals: { f: f, klass: :shipping_method, mode: :edit } .clear %div = render partial: 'spree/admin/shared/edit_resource_links' diff --git a/app/views/spree/admin/shipping_methods/new.html.haml b/app/views/spree/admin/shipping_methods/new.html.haml index ab7c444fda..935ee463fb 100644 --- a/app/views/spree/admin/shipping_methods/new.html.haml +++ b/app/views/spree/admin/shipping_methods/new.html.haml @@ -10,7 +10,7 @@ %fieldset.no-border-top = render partial: 'form', locals: { f: f } .one.column   - = render partial: 'spree/admin/shared/hubs_sidebar', locals: { f: f, klass: :shipping_method } + = render partial: 'spree/admin/shared/hubs_sidebar', locals: { f: f, klass: :shipping_method, mode: :new } .clear %div = render partial: 'spree/admin/shared/new_resource_links' diff --git a/spec/features/admin/payment_method_spec.rb b/spec/features/admin/payment_method_spec.rb index 439d9ea0db..0d765f7b59 100644 --- a/spec/features/admin/payment_method_spec.rb +++ b/spec/features/admin/payment_method_spec.rb @@ -67,6 +67,21 @@ feature ' expect(page).to have_selector "#stripe-account-status .charges_enabled", text: "Charges Enabled: Yes" end end + + scenario "checking a single distributor is checked by default" do + 2.times.each { Enterprise.last.destroy } + quick_login_as_admin + visit spree.new_admin_payment_method_path + expect(page).to have_field "payment_method_distributor_ids_#{@distributors[0].id}", checked: true + end + + scenario "checking more than a distributor displays no default choice" do + quick_login_as_admin + visit spree.new_admin_payment_method_path + expect(page).to have_field "payment_method_distributor_ids_#{@distributors[0].id}", checked: false + expect(page).to have_field "payment_method_distributor_ids_#{@distributors[1].id}", checked: false + expect(page).to have_field "payment_method_distributor_ids_#{@distributors[2].id}", checked: false + end end scenario "updating a payment method", js: true do diff --git a/spec/features/admin/shipping_methods_spec.rb b/spec/features/admin/shipping_methods_spec.rb index ca00af3457..9f413a0935 100644 --- a/spec/features/admin/shipping_methods_spec.rb +++ b/spec/features/admin/shipping_methods_spec.rb @@ -65,6 +65,20 @@ feature 'shipping methods' do expect(page).to have_content "That shipping method cannot be deleted as it is referenced by an order: #{o.number}." expect(Spree::ShippingMethod.find(@sm.id)).not_to be_nil end + + scenario "checking a single distributor is checked by default" do + d1 = Enterprise.first + visit spree.new_admin_shipping_method_path + expect(page).to have_field "shipping_method_distributor_ids_#{d1.id}", checked: true + end + + scenario "checking more than a distributor displays no default choice" do + d1 = create(:distributor_enterprise, name: 'Aeronautical Adventures') + d2 = create(:distributor_enterprise, name: 'Nautical Travels') + visit spree.new_admin_shipping_method_path + expect(page).to have_field "shipping_method_distributor_ids_#{d1.id}", checked: false + expect(page).to have_field "shipping_method_distributor_ids_#{d2.id}", checked: false + end end context "as an enterprise user", js: true do