From 7eadf76fa41c13e4e2493c6444a7cb074cc08165 Mon Sep 17 00:00:00 2001 From: Elia Schito Date: Mon, 27 Nov 2023 16:29:10 +0100 Subject: [PATCH] Add a tax categories index with scopes and batch deletion --- .../tax_categories/index/component.html.erb | 34 +++++++++++ .../tax_categories/index/component.rb | 58 +++++++++++++++++++ .../tax_categories/index/component.yml | 6 ++ .../tax_categories_controller.rb | 44 ++++++++++++++ admin/config/locales/tax_categories.en.yml | 6 ++ admin/config/routes.rb | 6 ++ admin/spec/features/tax_categories_spec.rb | 24 ++++++++ 7 files changed, 178 insertions(+) create mode 100644 admin/app/components/solidus_admin/tax_categories/index/component.html.erb create mode 100644 admin/app/components/solidus_admin/tax_categories/index/component.rb create mode 100644 admin/app/components/solidus_admin/tax_categories/index/component.yml create mode 100644 admin/app/controllers/solidus_admin/tax_categories_controller.rb create mode 100644 admin/config/locales/tax_categories.en.yml create mode 100644 admin/spec/features/tax_categories_spec.rb diff --git a/admin/app/components/solidus_admin/tax_categories/index/component.html.erb b/admin/app/components/solidus_admin/tax_categories/index/component.html.erb new file mode 100644 index 00000000000..7553a9264f6 --- /dev/null +++ b/admin/app/components/solidus_admin/tax_categories/index/component.html.erb @@ -0,0 +1,34 @@ +<%= page do %> + <%= page_header do %> + <%= page_header_title title %> + <%= page_header_actions do %> + <%= render component("ui/button").new( + tag: :a, + text: t('.add'), + href: spree.new_admin_tax_category_path, + icon: "add-line", + ) %> + <% end %> + <% end %> + + <%= render component('ui/table').new( + id: stimulus_id, + data: { + class: Spree::TaxCategory, + rows: @page.records, + url: ->(tax_category) { spree.edit_admin_tax_category_path(tax_category) }, + prev: prev_page_path, + next: next_page_path, + columns: columns, + batch_actions: batch_actions, + }, + search: { + name: :q, + value: params[:q], + url: solidus_admin.tax_categories_path, + searchbar_key: :name_or_description_cont, + filters: filters, + scopes: scopes, + }, + ) %> +<% end %> diff --git a/admin/app/components/solidus_admin/tax_categories/index/component.rb b/admin/app/components/solidus_admin/tax_categories/index/component.rb new file mode 100644 index 00000000000..252f146b01c --- /dev/null +++ b/admin/app/components/solidus_admin/tax_categories/index/component.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +class SolidusAdmin::TaxCategories::Index::Component < SolidusAdmin::BaseComponent + include SolidusAdmin::Layout::PageHelpers + + def initialize(page:) + @page = page + end + + def title + Spree::TaxCategory.model_name.human.pluralize + end + + def prev_page_path + solidus_admin.url_for(**request.params, page: @page.number - 1, only_path: true) unless @page.first? + end + + def next_page_path + solidus_admin.url_for(**request.params, page: @page.next_param, only_path: true) unless @page.last? + end + + def batch_actions + [ + { + display_name: t('.batch_actions.delete'), + action: solidus_admin.tax_categories_path, + method: :delete, + icon: 'delete-bin-7-line', + }, + ] + end + + def filters + [] + end + + def scopes + [] + end + + def columns + [ + :name, + :tax_code, + :description, + { + header: :is_default, + data: ->(tax_category) { + if tax_category.is_default? + component('ui/badge').new(name: t('.yes'), color: :green) + else + component('ui/badge').new(name: t('.no'), color: :graphite_light) + end + }, + }, + ] + end +end diff --git a/admin/app/components/solidus_admin/tax_categories/index/component.yml b/admin/app/components/solidus_admin/tax_categories/index/component.yml new file mode 100644 index 00000000000..f85b0196574 --- /dev/null +++ b/admin/app/components/solidus_admin/tax_categories/index/component.yml @@ -0,0 +1,6 @@ +en: + "yes": "Yes" + "no": "No" + add: 'Add Tax Category' + batch_actions: + delete: 'Delete' diff --git a/admin/app/controllers/solidus_admin/tax_categories_controller.rb b/admin/app/controllers/solidus_admin/tax_categories_controller.rb new file mode 100644 index 00000000000..b328f688e6c --- /dev/null +++ b/admin/app/controllers/solidus_admin/tax_categories_controller.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +module SolidusAdmin + class TaxCategoriesController < SolidusAdmin::BaseController + include SolidusAdmin::ControllerHelpers::Search + + def index + tax_categories = apply_search_to( + Spree::TaxCategory.order(created_at: :desc, id: :desc), + param: :q, + ) + + set_page_and_extract_portion_from(tax_categories) + + respond_to do |format| + format.html { render component('tax_categories/index').new(page: @page) } + end + end + + def destroy + @tax_categories = Spree::TaxCategory.where(id: params[:id]) + + Spree::TaxCategory.transaction { @tax_categories.destroy_all } + + flash[:notice] = t('.success') + redirect_back_or_to tax_categories_path, status: :see_other + end + + private + + def load_tax_category + @tax_category = Spree::TaxCategory.find_by!(number: params[:id]) + authorize! action_name, @tax_category + end + + def tax_category_params + params.require(:tax_category).permit(:tax_category_id, permitted_tax_category_attributes) + end + + # def authorization_subject + # Spree::TaxCategory + # end + end +end diff --git a/admin/config/locales/tax_categories.en.yml b/admin/config/locales/tax_categories.en.yml new file mode 100644 index 00000000000..9162bb44069 --- /dev/null +++ b/admin/config/locales/tax_categories.en.yml @@ -0,0 +1,6 @@ +en: + solidus_admin: + tax_categories: + title: "Tax Categories" + destroy: + success: "Tax categories were successfully removed." diff --git a/admin/config/routes.rb b/admin/config/routes.rb index 587c5599468..ead7616e76c 100644 --- a/admin/config/routes.rb +++ b/admin/config/routes.rb @@ -30,4 +30,10 @@ get :customers_for end end + + resources :tax_categories, only: [:index] do + collection do + delete :destroy + end + end end diff --git a/admin/spec/features/tax_categories_spec.rb b/admin/spec/features/tax_categories_spec.rb new file mode 100644 index 00000000000..8909ca5e74a --- /dev/null +++ b/admin/spec/features/tax_categories_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe "Tax categories", :js, type: :feature do + before { sign_in create(:admin_user, email: 'admin@example.com') } + + it "lists tax categories and allows deleting them" do + create(:tax_category, name: "Clothing") + create(:tax_category, name: "Food") + + visit "/admin/tax_categories" + expect(page).to have_content("Clothing") + expect(page).to have_content("Food") + expect(page).to be_axe_clean + + select_row("Clothing") + click_on "Delete" + expect(page).to have_content("Tax categories were successfully removed.") + expect(page).not_to have_content("Clothing") + expect(Spree.user_class.count).to eq(1) + expect(page).to be_axe_clean + end +end