From f38fbd67fdef378667c5a5a8a8524fb45a974536 Mon Sep 17 00:00:00 2001 From: andrea longhi Date: Tue, 12 Mar 2024 17:47:43 +0100 Subject: [PATCH] Add tax category edit and update actions This completes the feature for editing an existing tax category. --- .../tax_categories_controller.rb | 41 +++++++++++++++++++ admin/config/locales/tax_categories.en.yml | 2 + 2 files changed, 43 insertions(+) diff --git a/admin/app/controllers/solidus_admin/tax_categories_controller.rb b/admin/app/controllers/solidus_admin/tax_categories_controller.rb index cc9e833a2b..5bea523921 100644 --- a/admin/app/controllers/solidus_admin/tax_categories_controller.rb +++ b/admin/app/controllers/solidus_admin/tax_categories_controller.rb @@ -4,6 +4,8 @@ module SolidusAdmin class TaxCategoriesController < SolidusAdmin::BaseController include SolidusAdmin::ControllerHelpers::Search + before_action :find_tax_category, only: %i[edit update] + def new @tax_category = Spree::TaxCategory.new @@ -14,6 +16,16 @@ def new end end + def edit + @tax_category = Spree::TaxCategory.find(params[:id]) + + set_index_page + + respond_to do |format| + format.html { render component('tax_categories/edit').new(page: @page, tax_category: @tax_category) } + end + end + def create @tax_category = Spree::TaxCategory.new(tax_category_params) @@ -43,6 +55,31 @@ def create end end + def update + if @tax_category.update(tax_category_params) + respond_to do |format| + flash[:notice] = t('.success') + + format.html do + redirect_to solidus_admin.tax_categories_path, status: :see_other + end + + format.turbo_stream do + render turbo_stream: '' + end + end + else + set_index_page + + respond_to do |format| + format.html do + page_component = component('tax_categories/edit').new(page: @page, tax_category: @tax_category) + render page_component, status: :unprocessable_entity + end + end + end + end + def index set_index_page @@ -67,6 +104,10 @@ def load_tax_category authorize! action_name, @tax_category end + def find_tax_category + @tax_category = Spree::TaxCategory.find(params[:id]) + end + def tax_category_params params.require(:tax_category).permit(:name, :description, :is_default, :tax_code) end diff --git a/admin/config/locales/tax_categories.en.yml b/admin/config/locales/tax_categories.en.yml index c86f9400f4..b049ad67e0 100644 --- a/admin/config/locales/tax_categories.en.yml +++ b/admin/config/locales/tax_categories.en.yml @@ -6,3 +6,5 @@ en: success: "Tax categories were successfully removed." create: success: "Tax category was successfully created." + update: + success: "Tax category was successfully updated."