Skip to content

Commit

Permalink
Create new refund reasons
Browse files Browse the repository at this point in the history
This commit completes the process for managing the creation of a new
refund reason. Failures update the form in the existing modal with
the appropriate error messages, while successful creations update the
current page with a Turbo stream refresh and remove the form modal.
  • Loading branch information
spaghetticode committed Mar 12, 2024
1 parent 4b346d0 commit ec4234d
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 8 deletions.
55 changes: 48 additions & 7 deletions admin/app/controllers/solidus_admin/refund_reasons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,50 @@ class RefundReasonsController < SolidusAdmin::BaseController
include SolidusAdmin::ControllerHelpers::Search

def index
refund_reasons = apply_search_to(
Spree::RefundReason.unscoped.order(id: :desc),
param: :q,
)

set_page_and_extract_portion_from(refund_reasons)
set_index_page

respond_to do |format|
format.html { render component('refund_reasons/index').new(page: @page) }
end
end

def new
@refund_reason = Spree::RefundReason.new

set_index_page

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

def create
@refund_reason = Spree::RefundReason.new(refund_reason_params)

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

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

format.turbo_stream do
render turbo_stream: '<turbo-stream action="refresh" />'
end
end
else
set_index_page

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

def destroy
@refund_reason = Spree::RefundReason.find_by!(id: params[:id])

Expand All @@ -34,7 +66,16 @@ def load_refund_reason
end

def refund_reason_params
params.require(:refund_reason).permit(:refund_reason_id, permitted_refund_reason_attributes)
params.require(:refund_reason).permit(:name, :code, :active)
end

def set_index_page
refund_reasons = apply_search_to(
Spree::RefundReason.unscoped.order(id: :desc),
param: :q,
)

set_page_and_extract_portion_from(refund_reasons)
end
end
end
2 changes: 2 additions & 0 deletions admin/config/locales/refund_reasons.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ en:
title: "Refund Reasons"
destroy:
success: "Refund Reasons were successfully removed."
create:
success: "Refund reason 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 @@ -60,7 +60,7 @@
admin_resources :stock_locations, only: [:index, :destroy]
admin_resources :stores, only: [:index, :destroy]
admin_resources :zones, only: [:index, :destroy]
admin_resources :refund_reasons, only: [:index, :new, :destroy]
admin_resources :refund_reasons, only: [:index, :new, :create, :destroy]
admin_resources :reimbursement_types, only: [:index]
admin_resources :return_reasons, only: [:index, :destroy]
admin_resources :adjustment_reasons, only: [:index, :destroy]
Expand Down
39 changes: 39 additions & 0 deletions admin/spec/features/refund_reasons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,43 @@
expect(Spree::RefundReason.count).to eq(0)
expect(page).to be_axe_clean
end

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

before do
visit "/admin/refund_reasons/#{query}"
click_on "Add new"
expect(page).to have_content("New Refund Reason")
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 refund reason, keeping page and q params" do
fill_in "Name", with: "Return process"

click_on "Add Refund Reason"

expect(page).to have_content("Refund reason was successfully created.")
expect(Spree::RefundReason.find_by(name: "Return process")).to be_present
expect(page.current_url).to include(query)
end
end

context "with invalid data" do
it "fails to create a new refund reason, keeping page and q params" do
click_on "Add Refund Reason"

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

0 comments on commit ec4234d

Please sign in to comment.