Files
openfoodnetwork/app/controllers/admin/vouchers_controller.rb
Gaetan Craig-Riou 06f986ff52 Allow for creating a voucher with either flat or percentage rate
In the scenario when you get an error when trying to create a
percentage voucher, on the subsequent try we would be dealing with
a "percentage rate voucher". The code now handle any type of voucher
2023-08-11 16:09:48 +10:00

59 lines
1.8 KiB
Ruby

# frozen_string_literal: true
module Admin
class VouchersController < ResourceController
before_action :load_enterprise
def new
@voucher = Voucher.new
end
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]
# 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
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
end
if @voucher.save
flash[:success] = I18n.t(:successfully_created, resource: "Voucher")
redirect_to edit_admin_enterprise_path(@enterprise, anchor: :vouchers_panel)
else
render_error
end
end
private
def render_error
flash[:error] = @voucher.errors.full_messages.to_sentence
render :new
end
def load_enterprise
@enterprise = OpenFoodNetwork::Permissions
.new(spree_current_user)
.editable_enterprises
.find_by(permalink: params[:enterprise_id])
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)
end
end
end