From 29a38467d256cbf93b596d935b4b9eade37a151a Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Mon, 3 Jul 2023 15:32:44 +1000 Subject: [PATCH] Fix admin pages to work with refactored vouchers --- app/controllers/admin/vouchers_controller.rb | 24 ++++++++--- app/views/admin/vouchers/new.html.haml | 2 +- config/locales/en.yml | 4 +- .../admin/vouchers_controller_spec.rb | 42 +++++++++++++++---- spec/system/admin/vouchers_spec.rb | 12 +++--- 5 files changed, 62 insertions(+), 22 deletions(-) diff --git a/app/controllers/admin/vouchers_controller.rb b/app/controllers/admin/vouchers_controller.rb index 4ce186d2c5..4ec300f3cc 100644 --- a/app/controllers/admin/vouchers_controller.rb +++ b/app/controllers/admin/vouchers_controller.rb @@ -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 diff --git a/app/views/admin/vouchers/new.html.haml b/app/views/admin/vouchers/new.html.haml index 537d857f66..a27ea65d7a 100644 --- a/app/views/admin/vouchers/new.html.haml +++ b/app/views/admin/vouchers/new.html.haml @@ -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') diff --git a/config/locales/en.yml b/config/locales/en.yml index d343a6d151..d3e147cf25 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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: diff --git a/spec/requests/admin/vouchers_controller_spec.rb b/spec/requests/admin/vouchers_controller_spec.rb index 6268d6250e..60b0f34e17 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 { - 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 diff --git a/spec/system/admin/vouchers_spec.rb b/spec/system/admin/vouchers_spec.rb index 1ba4fa86ed..68b7fe83f3 100644 --- a/spec/system/admin/vouchers_spec.rb +++ b/spec/system/admin/vouchers_spec.rb @@ -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