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
This commit is contained in:
cyrillefr
2019-12-18 09:54:23 +01:00
parent 25ded0d23c
commit e59077e63e
8 changed files with 42 additions and 5 deletions

View File

@@ -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

View File

@@ -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'

View File

@@ -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'

View File

@@ -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

View File

@@ -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'

View File

@@ -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'

View File

@@ -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

View File

@@ -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