From aa910fd486fcf55185ea69d080ce34caee997ad3 Mon Sep 17 00:00:00 2001 From: Elia Schito Date: Wed, 29 Nov 2023 18:54:08 +0100 Subject: [PATCH] Add a tax rates index with scopes and batch deletion --- .../tax_categories/index/component.html.erb | 2 +- .../tax_rates/index/component.html.erb | 32 +++++++ .../tax_rates/index/component.rb | 84 +++++++++++++++++++ .../tax_rates/index/component.yml | 4 + .../solidus_admin/taxes/component.html.erb | 5 +- .../solidus_admin/taxes/component.rb | 19 +++++ .../solidus_admin/tax_rates_controller.rb | 40 +++++++++ admin/config/locales/tax_rates.en.yml | 6 ++ admin/config/routes.rb | 6 ++ 9 files changed, 195 insertions(+), 3 deletions(-) create mode 100644 admin/app/components/solidus_admin/tax_rates/index/component.html.erb create mode 100644 admin/app/components/solidus_admin/tax_rates/index/component.rb create mode 100644 admin/app/components/solidus_admin/tax_rates/index/component.yml create mode 100644 admin/app/controllers/solidus_admin/tax_rates_controller.rb create mode 100644 admin/config/locales/tax_rates.en.yml 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 index deba4262bd7..4d17aedb16e 100644 --- a/admin/app/components/solidus_admin/tax_categories/index/component.html.erb +++ b/admin/app/components/solidus_admin/tax_categories/index/component.html.erb @@ -1,4 +1,4 @@ -<%= render component('taxes').new do |layout| %> +<%= render component('taxes').new(model_class: Spree::TaxCategory) do |layout| %> <% layout.with_actions do %> <%= render component("ui/button").new( tag: :a, diff --git a/admin/app/components/solidus_admin/tax_rates/index/component.html.erb b/admin/app/components/solidus_admin/tax_rates/index/component.html.erb new file mode 100644 index 00000000000..f4149e0c475 --- /dev/null +++ b/admin/app/components/solidus_admin/tax_rates/index/component.html.erb @@ -0,0 +1,32 @@ +<%= render component('taxes').new(model_class: Spree::TaxRate) do |layout| %> + <% layout.with_actions do %> + <%= render component("ui/button").new( + tag: :a, + text: t('.add'), + href: spree.new_admin_tax_rate_path, + icon: "add-line", + class: "align-self-end w-full", + ) %> + <% end %> + + <%= render component('ui/table').new( + id: stimulus_id, + data: { + class: Spree::TaxRate, + rows: @page.records, + url: ->(tax_rate) { spree.edit_admin_tax_rate_path(tax_rate) }, + prev: prev_page_path, + next: next_page_path, + columns: columns, + batch_actions: batch_actions, + }, + search: { + name: :q, + value: params[:q], + url: solidus_admin.tax_rates_path, + searchbar_key: :name_or_description_cont, + filters: filters, + scopes: scopes, + }, + ) %> +<% end %> diff --git a/admin/app/components/solidus_admin/tax_rates/index/component.rb b/admin/app/components/solidus_admin/tax_rates/index/component.rb new file mode 100644 index 00000000000..d65f7130068 --- /dev/null +++ b/admin/app/components/solidus_admin/tax_rates/index/component.rb @@ -0,0 +1,84 @@ +# frozen_string_literal: true + +class SolidusAdmin::TaxRates::Index::Component < SolidusAdmin::BaseComponent + include SolidusAdmin::Layout::PageHelpers + + def initialize(page:) + @page = page + end + + def title + Spree::TaxRate.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_rates_path, + method: :delete, + icon: 'delete-bin-7-line', + }, + ] + end + + def filters + [ + { + presentation: Spree::Zone.model_name.human, + attribute: :zone_id, + predicate: :eq, + options: Spree::Zone.pluck(:name, :id), + }, + { + presentation: Spree::TaxCategory.model_name.human, + attribute: :tax_categories_id, + predicate: :in, + options: Spree::TaxCategory.pluck(:name, :id), + } + ] + end + + def scopes + [] + end + + def columns + [ + { + header: :zone, + data: -> { _1.zone&.name }, + }, + :name, + { + header: :tax_categories, + data: -> { _1.tax_categories.map(&:name).join(', ') }, + }, + { + header: :amount, + data: -> { _1.display_amount }, + }, + { + header: :included_in_price, + data: -> { _1.included_in_price? ? component('ui/badge').yes : component('ui/badge').no }, + }, + { + header: :show_rate_in_label, + data: -> { _1.show_rate_in_label? ? component('ui/badge').yes : component('ui/badge').no }, + }, + :expires_at, + { + header: Spree::Calculator.model_name.human, + data: -> { _1.calculator&.class&.model_name&.human } + }, + ] + end +end diff --git a/admin/app/components/solidus_admin/tax_rates/index/component.yml b/admin/app/components/solidus_admin/tax_rates/index/component.yml new file mode 100644 index 00000000000..54418702ad3 --- /dev/null +++ b/admin/app/components/solidus_admin/tax_rates/index/component.yml @@ -0,0 +1,4 @@ +en: + add: 'Add new' + batch_actions: + delete: 'Delete' diff --git a/admin/app/components/solidus_admin/taxes/component.html.erb b/admin/app/components/solidus_admin/taxes/component.html.erb index c78b28ca5c1..2dfa09155ba 100644 --- a/admin/app/components/solidus_admin/taxes/component.html.erb +++ b/admin/app/components/solidus_admin/taxes/component.html.erb @@ -8,8 +8,9 @@ <%= page_header do %> <% title = capture do %> - <%= render(component('ui/button').new(tag: :a, scheme: :ghost, text: Spree::TaxCategory.model_name.human.pluralize, href: solidus_admin.tax_categories_path, "aria-current": true)) %> - <%= render(component('ui/button').new(tag: :a, scheme: :ghost, text: Spree::TaxRate.model_name.human.pluralize, href: spree.admin_tax_rates_path, current: false)) %> + <% tabs.each do %> + <%= render(component('ui/button').new(tag: :a, scheme: :ghost, **_1)) %> + <% end %> <% end %> <%= page_header_title title %> diff --git a/admin/app/components/solidus_admin/taxes/component.rb b/admin/app/components/solidus_admin/taxes/component.rb index f0a4c0ddd84..789ea880858 100644 --- a/admin/app/components/solidus_admin/taxes/component.rb +++ b/admin/app/components/solidus_admin/taxes/component.rb @@ -3,4 +3,23 @@ class SolidusAdmin::Taxes::Component < SolidusAdmin::BaseComponent include SolidusAdmin::Layout::PageHelpers renders_one :actions + + def initialize(model_class:) + @model_class = model_class + end + + def tabs + [ + { + text: Spree::TaxCategory.model_name.human.pluralize, + href: solidus_admin.tax_categories_path, + "aria-current": @model_class == Spree::TaxCategory, + }, + { + text: Spree::TaxRate.model_name.human.pluralize, + href: solidus_admin.tax_rates_path, + "aria-current": @model_class == Spree::TaxRate, + }, + ] + end end diff --git a/admin/app/controllers/solidus_admin/tax_rates_controller.rb b/admin/app/controllers/solidus_admin/tax_rates_controller.rb new file mode 100644 index 00000000000..3a313fd9826 --- /dev/null +++ b/admin/app/controllers/solidus_admin/tax_rates_controller.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +module SolidusAdmin + class TaxRatesController < SolidusAdmin::BaseController + include SolidusAdmin::ControllerHelpers::Search + + def index + tax_rates = apply_search_to( + Spree::TaxRate.order(created_at: :desc, id: :desc), + param: :q, + ) + + set_page_and_extract_portion_from(tax_rates) + + respond_to do |format| + format.html { render component('tax_rates/index').new(page: @page) } + end + end + + def destroy + @tax_rates = Spree::TaxRate.where(id: params[:id]) + + Spree::TaxRate.transaction { @tax_rates.destroy_all } + + flash[:notice] = t('.success') + redirect_back_or_to tax_rates_path, status: :see_other + end + + private + + def load_tax_rate + @tax_rate = Spree::TaxRate.find_by!(number: params[:id]) + authorize! action_name, @tax_rate + end + + def tax_rate_params + params.require(:tax_rate).permit(:tax_rate_id, permitted_tax_rate_attributes) + end + end +end diff --git a/admin/config/locales/tax_rates.en.yml b/admin/config/locales/tax_rates.en.yml new file mode 100644 index 00000000000..b95ee13ed99 --- /dev/null +++ b/admin/config/locales/tax_rates.en.yml @@ -0,0 +1,6 @@ +en: + solidus_admin: + tax_rates: + title: "Tax Rates" + destroy: + success: "Tax rates were successfully removed." diff --git a/admin/config/routes.rb b/admin/config/routes.rb index 9cc307baa4a..f569ed796da 100644 --- a/admin/config/routes.rb +++ b/admin/config/routes.rb @@ -69,4 +69,10 @@ delete :destroy end end + + resources :tax_rates, only: [:index] do + collection do + delete :destroy + end + end end