Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,26 @@ def actions
)
end

def page_actions
render component("ui/button").new(
tag: :a,
text: t('.add'),
href: solidus_admin.new_shipping_category_path, data: { turbo_frame: :new_shipping_category_modal },
icon: "add-line",
class: "align-self-end w-full",
)
end

def turbo_frames
%w[new_shipping_category_modal]
end

def row_url(shipping_category)
spree.edit_admin_shipping_category_path(shipping_category)
end

def search_key
:name_or_description_cont
:name_cont
end

def search_url
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%= turbo_frame_tag :new_shipping_category_modal do %>
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
<%= form_for @shipping_category, url: solidus_admin.shipping_categories_path(page: params[:page], q: params[:q]), html: { id: form_id } do |f| %>
<div class="flex flex-col gap-6 pb-4">
<%= render component("ui/forms/field").text_field(f, :name) %>
</div>
<% modal.with_actions do %>
<form method="dialog">
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
</form>
<%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %>
<% end %>
<% end %>
<% end %>
<% end %>

<%= render component("shipping_categories/index").new(page: @page) %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class SolidusAdmin::ShippingCategories::New::Component < SolidusAdmin::ShippingCategories::Index::Component
def initialize(page:, shipping_category:)
@page = page
@shipping_category = shipping_category
end

def form_id
dom_id(@shipping_category, "#{stimulus_id}_new_shipping_category_form")
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Add your component translations here.
# Use the translation in the example in your template with `t(".hello")`.
en:
title: "New Shipping Category"
cancel: "Cancel"
submit: "Add Shipping Category"
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,47 @@ module SolidusAdmin
class ShippingCategoriesController < SolidusAdmin::BaseController
include SolidusAdmin::ControllerHelpers::Search

def index
shipping_categories = apply_search_to(
Spree::ShippingCategory.order(id: :desc),
param: :q,
)
def new
@shipping_category = Spree::ShippingCategory.new

set_page_and_extract_portion_from(shipping_categories)
set_index_page

respond_to do |format|
format.html { render component('shipping_categories/new').new(page: @page, shipping_category: @shipping_category) }
end
end

def create
@shipping_category = Spree::ShippingCategory.new(shipping_category_params)

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

format.html do
redirect_to solidus_admin.shipping_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('shipping_categories/new').new(page: @page, shipping_category: @shipping_category)
render page_component, status: :unprocessable_entity
end
end
end
end

def index
set_index_page

respond_to do |format|
format.html { render component('shipping_categories/index').new(page: @page) }
Expand All @@ -34,7 +68,16 @@ def load_shipping_category
end

def shipping_category_params
params.require(:shipping_category).permit(:shipping_category_id, permitted_shipping_category_attributes)
params.require(:shipping_category).permit(:name)
end

def set_index_page
shipping_categories = apply_search_to(
Spree::ShippingCategory.order(id: :desc),
param: :q,
)

set_page_and_extract_portion_from(shipping_categories)
end
end
end
2 changes: 2 additions & 0 deletions admin/config/locales/shipping_categories.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ en:
title: "Shipping Categories"
destroy:
success: "Shipping categories were successfully removed."
create:
success: "Shipping 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 @@ -56,7 +56,7 @@
admin_resources :payment_methods, only: [:index, :destroy], sortable: true
admin_resources :stock_items, only: [:index, :edit, :update]
admin_resources :shipping_methods, only: [:index, :destroy]
admin_resources :shipping_categories, only: [:index, :destroy]
admin_resources :shipping_categories, only: [:new, :index, :create, :destroy]
admin_resources :stock_locations, only: [:index, :destroy]
admin_resources :stores, only: [:index, :destroy]
admin_resources :zones, only: [:index, :destroy]
Expand Down
39 changes: 39 additions & 0 deletions admin/spec/features/shipping_categories_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,43 @@
expect(Spree::ShippingCategory.count).to eq(0)
expect(page).to be_axe_clean
end

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

before do
visit "/admin/shipping_categories#{query}"
click_on "Add new"
expect(page).to have_content("New Shipping 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 shipping category, keeping page and q params" do
fill_in "Name", with: "Whatever"

click_on "Add Shipping Category"

expect(page).to have_content("Shipping category was successfully created.")
expect(Spree::ShippingCategory.find_by(name: "Whatever")).to be_present
expect(page.current_url).to include(query)
end
end

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

expect(page).to have_content "can't be blank"
expect(page.current_url).to include(query)
end
end
end
end
2 changes: 2 additions & 0 deletions core/app/models/spree/shipping_category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module Spree
class ShippingCategory < Spree::Base
self.allowed_ransackable_attributes = %w[name]

validates :name, presence: true
has_many :products, inverse_of: :shipping_category
has_many :shipping_method_categories, inverse_of: :shipping_category
Expand Down