From 4d5e1ffb3be52f23d366389c14928c6491cc9172 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Mon, 14 Aug 2023 11:39:59 +1000 Subject: [PATCH] Simplify VouchersController with single param name The Voucher form now deals with a generic Voucher instead of a subclass which makes the naming easier and is less confusing when changing types. --- app/controllers/admin/vouchers_controller.rb | 9 ++------- app/views/admin/vouchers/new.html.haml | 2 +- spec/requests/admin/vouchers_controller_spec.rb | 4 ++-- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/app/controllers/admin/vouchers_controller.rb b/app/controllers/admin/vouchers_controller.rb index cad08ab833..6a73bd7787 100644 --- a/app/controllers/admin/vouchers_controller.rb +++ b/app/controllers/admin/vouchers_controller.rb @@ -11,8 +11,7 @@ module Admin def create # In the scenario where you get an error when trying to create a percentage voucher, we'll # now have percentage rate voucher instanciated. Hence why we check for both params type - voucher_type = params.dig(:vouchers_flat_rate, :voucher_type) || - params[:vouchers_percentage_rate][:voucher_type] + voucher_type = params.dig(:voucher, :voucher_type) # The use of "safe_constantize" here will trigger a Brakeman error, it can safely be ignored # as it's a false positive : https://github.com/openfoodfoundation/openfoodnetwork/pull/10821 @@ -48,11 +47,7 @@ module Admin end def permitted_resource_params - if params[:vouchers_flat_rate].present? - return params.require(:vouchers_flat_rate).permit(:code, :amount) - end - - params.require(:vouchers_percentage_rate).permit(:code, :amount) + params.require(:voucher).permit(:code, :amount) end end end diff --git a/app/views/admin/vouchers/new.html.haml b/app/views/admin/vouchers/new.html.haml index a27ea65d7a..77eff6d903 100644 --- a/app/views/admin/vouchers/new.html.haml +++ b/app/views/admin/vouchers/new.html.haml @@ -1,4 +1,4 @@ -= form_with model: @voucher, url: admin_enterprise_vouchers_path(@enterprise), html: { name: "voucher_form" } do |f| += form_with model: @voucher.becomes(Voucher), url: admin_enterprise_vouchers_path(@enterprise), html: { name: "voucher_form" } do |f| .row .sixteen.columns.alpha .four.columns.alpha.text-right diff --git a/spec/requests/admin/vouchers_controller_spec.rb b/spec/requests/admin/vouchers_controller_spec.rb index 935255fff7..04e4c001c5 100644 --- a/spec/requests/admin/vouchers_controller_spec.rb +++ b/spec/requests/admin/vouchers_controller_spec.rb @@ -26,7 +26,7 @@ describe Admin::VouchersController, type: :request do let(:params) do { - vouchers_flat_rate: { + voucher: { code: code, amount: amount, voucher_type: type @@ -52,7 +52,7 @@ describe Admin::VouchersController, type: :request do context "with a percentage rate voucher" do let(:params) do { - vouchers_percentage_rate: { + voucher: { code: code, amount: amount, voucher_type: type