Skip to content

Commit

Permalink
Add a tax categories index with scopes and batch deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Nov 27, 2023
1 parent 66475ff commit 7eadf76
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 %>
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
en:
"yes": "Yes"
"no": "No"
add: 'Add Tax Category'
batch_actions:
delete: 'Delete'
44 changes: 44 additions & 0 deletions admin/app/controllers/solidus_admin/tax_categories_controller.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions admin/config/locales/tax_categories.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
en:
solidus_admin:
tax_categories:
title: "Tax Categories"
destroy:
success: "Tax categories 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 @@ -30,4 +30,10 @@
get :customers_for
end
end

resources :tax_categories, only: [:index] do
collection do
delete :destroy
end
end
end
24 changes: 24 additions & 0 deletions admin/spec/features/tax_categories_spec.rb
Original file line number Diff line number Diff line change
@@ -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: '[email protected]') }

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

0 comments on commit 7eadf76

Please sign in to comment.