mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-30 21:27:17 +00:00
Fix admin pages to work with refactored vouchers
This commit is contained in:
@@ -9,19 +9,33 @@ module Admin
|
||||
end
|
||||
|
||||
def create
|
||||
@voucher = Voucher.create(permitted_resource_params.merge(enterprise: @enterprise))
|
||||
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))
|
||||
else
|
||||
@voucher.errors.add(:type)
|
||||
return render_error
|
||||
end
|
||||
|
||||
if @voucher.save
|
||||
flash[:success] = flash_message_for(@voucher, :successfully_created)
|
||||
flash[:success] = I18n.t(:successfully_created, resource: "Voucher")
|
||||
redirect_to edit_admin_enterprise_path(@enterprise, anchor: :vouchers_panel)
|
||||
else
|
||||
flash[:error] = @voucher.errors.full_messages.to_sentence
|
||||
render :new
|
||||
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)
|
||||
@@ -30,7 +44,7 @@ module Admin
|
||||
end
|
||||
|
||||
def permitted_resource_params
|
||||
params.require(:voucher).permit(:code, :amount, :voucher_type)
|
||||
params.require(:vouchers_flat_rate).permit(:code, :amount)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
.alpha.four.columns
|
||||
= f.label :voucher_type, t('.voucher_type')
|
||||
.omega.eight.columns
|
||||
= f.select :voucher_type, options_for_select(Voucher::TYPES.map { |type| [t(".#{type}"), type] }, @voucher.voucher_type)
|
||||
= f.select :voucher_type, options_for_select(Voucher::TYPES.map { |type| [t(".#{type.demodulize.underscore}"), type] }, @voucher.class.to_s)
|
||||
.row
|
||||
.alpha.four.columns
|
||||
= f.label :amount, t('.voucher_amount')
|
||||
|
||||
@@ -1713,8 +1713,8 @@ en:
|
||||
voucher_code: Voucher Code
|
||||
voucher_amount: Amount
|
||||
voucher_type: Voucher Type
|
||||
flat: Flat
|
||||
percentage: Percentage (%)
|
||||
flat_rate: Flat
|
||||
percentage_rate: Percentage (%)
|
||||
|
||||
# Admin controllers
|
||||
controllers:
|
||||
|
||||
@@ -26,7 +26,7 @@ describe Admin::VouchersController, type: :request do
|
||||
|
||||
let(:params) do
|
||||
{
|
||||
voucher: {
|
||||
vouchers_flat_rate: {
|
||||
code: code,
|
||||
amount: amount,
|
||||
voucher_type: type
|
||||
@@ -35,15 +35,41 @@ describe Admin::VouchersController, type: :request do
|
||||
end
|
||||
let(:code) { "new_code" }
|
||||
let(:amount) { 15 }
|
||||
let(:type) { "percentage" }
|
||||
let(:type) { "Vouchers::PercentageRate" }
|
||||
|
||||
it "creates a new voucher" do
|
||||
expect { create_voucher }.to change(Voucher, :count).by(1)
|
||||
context "with a flat rate voucher" do
|
||||
let(:type) { "Vouchers::FlatRate" }
|
||||
|
||||
voucher = Voucher.last
|
||||
expect(voucher.code).to eq(code)
|
||||
expect(voucher.amount).to eq(amount)
|
||||
expect(voucher.voucher_type).to eq(type)
|
||||
it "creates a new voucher" do
|
||||
expect { create_voucher }.to change(Vouchers::FlatRate, :count).by(1)
|
||||
|
||||
voucher = Vouchers::FlatRate.last
|
||||
expect(voucher.code).to eq(code)
|
||||
expect(voucher.amount).to eq(amount)
|
||||
end
|
||||
end
|
||||
|
||||
context "with a percentage rate voucher" do
|
||||
let(:type) { "Vouchers::PercentageRate" }
|
||||
|
||||
it "creates a new voucher" do
|
||||
expect { create_voucher }.to change(Vouchers::PercentageRate, :count).by(1)
|
||||
|
||||
voucher = Vouchers::PercentageRate.last
|
||||
expect(voucher.code).to eq(code)
|
||||
expect(voucher.amount).to eq(amount)
|
||||
end
|
||||
end
|
||||
|
||||
context "with a wrong type" do
|
||||
let(:type) { "Random" }
|
||||
|
||||
it "render the new page with an error" do
|
||||
create_voucher
|
||||
|
||||
expect(response).to render_template("admin/vouchers/new")
|
||||
expect(flash[:error]).to eq("Type is invalid")
|
||||
end
|
||||
end
|
||||
|
||||
it "redirects to admin enterprise setting page, voucher panel" do
|
||||
|
||||
@@ -50,9 +50,9 @@ describe '
|
||||
context "with a flat rate voucher" do
|
||||
it 'creates a voucher' do
|
||||
# And I fill in the fields for a new voucher click save
|
||||
fill_in 'voucher_code', with: voucher_code
|
||||
select "Flat", from: "voucher_voucher_type"
|
||||
fill_in 'voucher_amount', with: amount
|
||||
fill_in 'vouchers_flat_rate_code', with: voucher_code
|
||||
select "Flat", from: "vouchers_flat_rate_voucher_type"
|
||||
fill_in 'vouchers_flat_rate_amount', with: amount
|
||||
click_button 'Save'
|
||||
|
||||
# Then I should get redirect to the entreprise voucher tab and see the created voucher
|
||||
@@ -64,9 +64,9 @@ describe '
|
||||
context "with a percentage rate voucher" do
|
||||
it 'creates a voucher' do
|
||||
# And I fill in the fields for a new voucher click save
|
||||
fill_in 'voucher_code', with: voucher_code
|
||||
select "Percentage (%)", from: "voucher_voucher_type"
|
||||
fill_in 'voucher_amount', with: amount
|
||||
fill_in 'vouchers_flat_rate_code', with: voucher_code
|
||||
select "Percentage (%)", from: "vouchers_flat_rate_voucher_type"
|
||||
fill_in 'vouchers_flat_rate_amount', with: amount
|
||||
click_button 'Save'
|
||||
|
||||
# Then I should get redirect to the entreprise voucher tab and see the created voucher
|
||||
|
||||
Reference in New Issue
Block a user