From 5b3bae85ca7f01d0777973d1ea7377393f9e3a96 Mon Sep 17 00:00:00 2001 From: Tresor11 Date: Sat, 22 Mar 2025 00:35:27 +0300 Subject: [PATCH 1/3] Rescue Not Found Error for Enterprise for admin Currently when an admin tries to edit an no-existing enterprise, a NoMethodError is raised. This commit adds a set_enterprise setter method to the enterprises controller that sets the @enterprise instance variable to have the same value as the enterprise object defined in the the edit method; this method also rescues the NotFound error in case the enterprise is not found and redirects the user to the enterprises index page with a error message. --- .../admin/enterprises_controller.rb | 9 +++++++++ config/locales/en.yml | 19 ++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index e5b35473ac..1166f31376 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -15,6 +15,7 @@ module Admin prepend_before_action :override_sells, only: :create before_action :load_countries, except: [:index, :register, :check_permalink] + before_action :set_enterprise, only: [:edit, :update] before_action :load_methods_and_fees, only: [:edit, :update] before_action :load_groups, only: [:new, :edit, :update, :create] before_action :load_taxons, only: [:new, :edit, :update, :create] @@ -216,6 +217,14 @@ module Admin [:index, :for_order_cycle, :visible, :bulk_update] end + def set_enterprise + @enterprise = @object + return if @enterprise + + flash[:error] = I18n.t(:enterprise_not_found_error) + redirect_to admin_enterprises_path + end + def load_methods_and_fees enterprise_payment_methods = @enterprise.payment_methods.to_a enterprise_shipping_methods = @enterprise.shipping_methods.to_a diff --git a/config/locales/en.yml b/config/locales/en.yml index 1eedd00d20..d3917e1b1f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -629,7 +629,7 @@ en: price: Price producer: Producer category: Category - sku: SKU + sku: SKU on_hand: "On Hand" on_demand: "On Demand" tax_category: "Tax Category" @@ -641,7 +641,7 @@ en: price: Price producer: Producer category: Category - sku: SKU + sku: SKU on_hand: "On Hand" on_demand: "On Demand" tax_category: "Tax Category" @@ -652,7 +652,7 @@ en: clone: Clone delete: Delete remove: Remove - preview: Preview + preview: Preview image: edit: Edit product_preview: @@ -801,7 +801,7 @@ en: connected_apps_enabled: discover_regen: Discover Regenerative portal affiliate_sales_data: DFC anonymised orders API for research purposes - vine: Voucher Integration Engine (VINE) + vine: Voucher Integration Engine (VINE) update: resource: Connected app settings @@ -1429,7 +1429,7 @@ en: use_limit: Use/Limit customers: Customer net_value: Net Value - active: Active? + active: Active? add_new: Add New no_voucher_yet: No Vouchers yet white_label: @@ -2139,7 +2139,7 @@ en: voucher_not_found: Not found add_voucher_error: There was an error while adding the voucher create_voucher_error: "There was an error while creating the voucher: %{error}" - voucher_redeeming_error: There was an error while trying to redeem your voucher + voucher_redeeming_error: There was an error while trying to redeem your voucher shops: hubs: show_closed_shops: "Show closed shops" @@ -2218,7 +2218,7 @@ en: invoice_column_price_per_unit_without_taxes: "Price Per unit (Excl. tax)" invoice_column_tax_rate: "Tax rate" invoice_tax_total: "GST Total:" - invoice_cancel_and_replace_invoice: "cancels and replaces invoice" + invoice_cancel_and_replace_invoice: "cancels and replaces invoice" tax_invoice: "TAX INVOICE" tax_total: "Total tax (%{rate}):" invoice_shipping_category_delivery: "Delivery" @@ -3440,6 +3440,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using enterprise_register_package_error: "Please select a package" enterprise_register_error: "Could not complete registration for %{enterprise}" enterprise_register_success_notice: "Congratulations! Registration for %{enterprise} is complete!" + enterprise_not_found_error: "Enterprise not found" enterprise_bulk_update_success_notice: "Enterprises updated successfully" enterprise_bulk_update_error: 'Update failed' enterprise_shop_show_error: "The shop you are looking for doesn't exist or is inactive on OFN. Please check other shops." @@ -4733,7 +4734,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using title: "Edit Product Category" destroy: delete_taxon: - success: "Successfully deleted the product category" + success: "Successfully deleted the product category" error: "Unable to delete the product category due to assigned products." form: name: Name @@ -4741,7 +4742,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using meta_description: Meta Description meta_keywords: Meta Keywords description: Description - dfc_id: DFC URI + dfc_id: DFC URI general_settings: edit: legal_settings: "Legal Settings" From 507705a4eb2a22216c4b9b2c5883973924c7955b Mon Sep 17 00:00:00 2001 From: Tresor11 Date: Mon, 31 Mar 2025 22:13:45 +0300 Subject: [PATCH 2/3] Update method name to require This commit updates the method name to be called required_enterprise since we only expect it to raise an error when the enterprise is not found. --- .../admin/enterprises_controller.rb | 10 +++------ config/locales/en.yml | 1 - .../admin/enterprises_controller_spec.rb | 22 +++++++++++++++++++ 3 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 spec/requests/admin/enterprises_controller_spec.rb diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index 1166f31376..0081dfda35 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -15,7 +15,7 @@ module Admin prepend_before_action :override_sells, only: :create before_action :load_countries, except: [:index, :register, :check_permalink] - before_action :set_enterprise, only: [:edit, :update] + before_action :require_enterprise, only: [:edit, :update] before_action :load_methods_and_fees, only: [:edit, :update] before_action :load_groups, only: [:new, :edit, :update, :create] before_action :load_taxons, only: [:new, :edit, :update, :create] @@ -217,12 +217,8 @@ module Admin [:index, :for_order_cycle, :visible, :bulk_update] end - def set_enterprise - @enterprise = @object - return if @enterprise - - flash[:error] = I18n.t(:enterprise_not_found_error) - redirect_to admin_enterprises_path + def require_enterprise + raise ActiveRecord::RecordNotFound if @enterprise.blank? end def load_methods_and_fees diff --git a/config/locales/en.yml b/config/locales/en.yml index d3917e1b1f..1f0aa5c52b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3440,7 +3440,6 @@ See the %{link} to find out more about %{sitename}'s features and to start using enterprise_register_package_error: "Please select a package" enterprise_register_error: "Could not complete registration for %{enterprise}" enterprise_register_success_notice: "Congratulations! Registration for %{enterprise} is complete!" - enterprise_not_found_error: "Enterprise not found" enterprise_bulk_update_success_notice: "Enterprises updated successfully" enterprise_bulk_update_error: 'Update failed' enterprise_shop_show_error: "The shop you are looking for doesn't exist or is inactive on OFN. Please check other shops." diff --git a/spec/requests/admin/enterprises_controller_spec.rb b/spec/requests/admin/enterprises_controller_spec.rb new file mode 100644 index 0000000000..111eedca52 --- /dev/null +++ b/spec/requests/admin/enterprises_controller_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +RSpec.describe Admin::EnterprisesController, type: :request do + let(:admin) { create(:admin_user) } + let(:enterprise) { create(:enterprise) } + + before do + sign_in admin + end + + describe 'GET #show' do + it 'returns a successful response' do + get edit_admin_enterprise_path(enterprise) + expect(response).to have_http_status(:success) + end + + it "redirect to the enterprises page for non-existing enterprise" do + get edit_admin_enterprise_path(id: 'non-existing') + expect(response).to redirect_to(admin_enterprises_path) + end + end +end From 176e33d6bf4a0432ef64cf7dd88fb91e04c24e75 Mon Sep 17 00:00:00 2001 From: David Cook Date: Tue, 1 Apr 2025 09:29:27 +1100 Subject: [PATCH 3/3] Rubocop fixes --- spec/requests/admin/enterprises_controller_spec.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/requests/admin/enterprises_controller_spec.rb b/spec/requests/admin/enterprises_controller_spec.rb index 111eedca52..16e3ec42bf 100644 --- a/spec/requests/admin/enterprises_controller_spec.rb +++ b/spec/requests/admin/enterprises_controller_spec.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + require 'spec_helper' -RSpec.describe Admin::EnterprisesController, type: :request do +RSpec.describe Admin::EnterprisesController do let(:admin) { create(:admin_user) } let(:enterprise) { create(:enterprise) }