Skip to content

Commit

Permalink
Add a tax rates index with scopes and batch deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Nov 30, 2023
1 parent 40346a2 commit aa910fd
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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 %>
84 changes: 84 additions & 0 deletions admin/app/components/solidus_admin/tax_rates/index/component.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
add: 'Add new'
batch_actions:
delete: 'Delete'
5 changes: 3 additions & 2 deletions admin/app/components/solidus_admin/taxes/component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
Expand Down
19 changes: 19 additions & 0 deletions admin/app/components/solidus_admin/taxes/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 40 additions & 0 deletions admin/app/controllers/solidus_admin/tax_rates_controller.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions admin/config/locales/tax_rates.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
en:
solidus_admin:
tax_rates:
title: "Tax Rates"
destroy:
success: "Tax rates were successfully removed."
6 changes: 6 additions & 0 deletions admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,10 @@
delete :destroy
end
end

resources :tax_rates, only: [:index] do
collection do
delete :destroy
end
end
end

0 comments on commit aa910fd

Please sign in to comment.