Skip to content

Commit

Permalink
Complete tax category creation process
Browse files Browse the repository at this point in the history
The `#create` controller action allows the creation of a new tax
category. Failed creation re-renders the form with errors within
the turbo frame tag, while successful creation is handled via a
turbo stream refresh tag that reloads the page.

Scroll position is preserved thanks to the custom Turbo meta tag
that enables the feature globally on the new admin.

Integration specs are added to test the complete feature.
  • Loading branch information
spaghetticode committed Mar 7, 2024
1 parent 5b6a832 commit efc79a7
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
29 changes: 29 additions & 0 deletions admin/app/controllers/solidus_admin/tax_categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,35 @@ def new
end
end

def create
@tax_category = Spree::TaxCategory.new(tax_category_params)

if @tax_category.save
respond_to do |format|
flash[:notice] = t('.success')

format.html do
redirect_to solidus_admin.tax_categories_path, status: :see_other
end

format.turbo_stream do
# we need to explicitly write the refresh tag for now.
# See https://github.com/hotwired/turbo-rails/issues/579
render turbo_stream: '<turbo-stream action="refresh" />'
end
end
else
set_index_page

respond_to do |format|
format.html do
page_component = component('tax_categories/new').new(page: @page, tax_category: @tax_category)
render page_component, status: :unprocessable_entity
end
end
end
end

def index
set_index_page

Expand Down
1 change: 1 addition & 0 deletions admin/app/views/layouts/solidus_admin/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<%= stylesheet_link_tag SolidusAdmin::Config.theme_path(session[:admin_light_theme]), media: '(prefers-color-scheme: light)', "data-turbo-track": "reload" %>
<%= stylesheet_link_tag SolidusAdmin::Config.theme_path(session[:admin_dark_theme]), media: '(prefers-color-scheme: dark)', "data-turbo-track": "reload" %>
<%= javascript_importmap_tags "solidus_admin/application", shim: false, importmap: SolidusAdmin.importmap %>
<meta name="turbo-refresh-scroll", content="preserve">
</head>

<body class="bg-gray-15 font-sans">
Expand Down
2 changes: 2 additions & 0 deletions admin/config/locales/tax_categories.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ en:
title: "Tax Categories"
destroy:
success: "Tax categories were successfully removed."
create:
success: "Tax category was successfully created."
2 changes: 1 addition & 1 deletion admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
admin_resources :option_types, only: [:index, :destroy], sortable: true
admin_resources :taxonomies, only: [:index, :destroy], sortable: true
admin_resources :promotion_categories, only: [:index, :destroy]
admin_resources :tax_categories, only: [:new, :index, :destroy]
admin_resources :tax_categories, only: [:new, :index, :create, :destroy]
admin_resources :tax_rates, only: [:index, :destroy]
admin_resources :payment_methods, only: [:index, :destroy], sortable: true
admin_resources :stock_items, only: [:index, :edit, :update]
Expand Down
39 changes: 39 additions & 0 deletions admin/spec/features/tax_categories_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,43 @@
expect(Spree::TaxCategory.count).to eq(1)
expect(page).to be_axe_clean
end

context "when creating a new tax category" do
let(:query) { "?page=1&q%5Bname_or_description_cont%5D=Cloth" }

before do
visit "/admin/tax_categories#{query}"
click_on "Add new"
expect(page).to have_content("New Tax Category")
expect(page).to be_axe_clean
end

it "opens a modal" do
expect(page).to have_selector("dialog")
within("dialog") { click_on "Cancel" }
expect(page).not_to have_selector("dialog")
expect(page.current_url).to include(query)
end

context "with valid data" do
it "successfully creates a new tax category, keeping page and q params" do
fill_in "Name", with: "Clothing"

click_on "Add Tax Category"

expect(page).to have_content("Tax category was successfully created.")
expect(Spree::TaxCategory.find_by(name: "Clothing")).to be_present
expect(page.current_url).to include(query)
end
end

context "with invalid data" do
it "fails to create a new tax category, keeping page and q params" do
click_on "Add Tax Category"

expect(page).to have_content "can't be blank"
expect(page.current_url).to include(query)
end
end
end
end

0 comments on commit efc79a7

Please sign in to comment.