From 229ae91f3f947e825386d1d6063bbcb73ebbd700 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Tue, 18 Jul 2023 13:24:41 +1000 Subject: [PATCH] Per review , Simplify creation of Voucher We now check against known type to instanciate the correct voucher instead of using a case --- app/controllers/admin/vouchers_controller.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/app/controllers/admin/vouchers_controller.rb b/app/controllers/admin/vouchers_controller.rb index 4ec300f3cc3..38a4203fd56 100644 --- a/app/controllers/admin/vouchers_controller.rb +++ b/app/controllers/admin/vouchers_controller.rb @@ -9,13 +9,11 @@ def new end def create - case params[:vouchers_flat_rate][:voucher_type] - when "Vouchers::FlatRate" - @voucher = - Vouchers::FlatRate.create(permitted_resource_params.merge(enterprise: @enterprise)) - when "Vouchers::PercentageRate" - @voucher = - Vouchers::PercentageRate.create(permitted_resource_params.merge(enterprise: @enterprise)) + voucher_type = params[:vouchers_flat_rate][:voucher_type] + if Voucher::TYPES.include?(voucher_type) + @voucher = voucher_type.safe_constantize.create( + permitted_resource_params.merge(enterprise: @enterprise) + ) else @voucher.errors.add(:type) return render_error