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.
This commit is contained in:
Maikel Linke
2023-08-14 11:39:59 +10:00
parent 06f986ff52
commit 4d5e1ffb3b
3 changed files with 5 additions and 10 deletions

View File

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

View File

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

View File

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