-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a tax categories index with scopes and batch deletion
- Loading branch information
Showing
7 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
admin/app/components/solidus_admin/tax_categories/index/component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
58 changes: 58 additions & 0 deletions
58
admin/app/components/solidus_admin/tax_categories/index/component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
6 changes: 6 additions & 0 deletions
6
admin/app/components/solidus_admin/tax_categories/index/component.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
admin/app/controllers/solidus_admin/tax_categories_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |