diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index 775d00840b..d7ad6d48c1 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -270,7 +270,7 @@ module Admin end def update_vouchers - return if params[:enterprise][:voucher_ids].blank? + params[:enterprise][:voucher_ids] = [] if params[:enterprise][:voucher_ids].blank? params_voucher_ids = params[:enterprise][:voucher_ids].map(&:to_i) voucher_ids = @enterprise.vouchers.map(&:id) diff --git a/spec/controllers/admin/enterprises_controller_spec.rb b/spec/controllers/admin/enterprises_controller_spec.rb index c68c04b82d..484901f83c 100644 --- a/spec/controllers/admin/enterprises_controller_spec.rb +++ b/spec/controllers/admin/enterprises_controller_spec.rb @@ -319,6 +319,19 @@ describe Admin::EnterprisesController, type: :controller do expect(voucher_a.reload.deleted?).to be(true) end + context "when desactivating the only existing voucher" do + it "deactivates the voucher" do + voucher_b.really_destroy! + + # Rails won't populate vouchers_id params if no voucher selected + spree_put :update, + id: enterprise, + enterprise: {} + + expect(voucher_a.reload.deleted?).to be(true) + end + end + context "when activating and deactivating voucher at the same time" do it "deactivates and activates accordingly" do voucher_c = create(:voucher, enterprise: enterprise, code: "voucher 3")