Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Admin] Introduce RMA reasons creation capability #5809

Closed
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 @@ -17,6 +17,21 @@ def row_url(return_reason)
spree.edit_admin_return_reason_path(return_reason)
end

def turbo_frames
%w[new_return_reason_modal]
end

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

def batch_actions
[
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<%= turbo_frame_tag :new_return_reason_modal do %>
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
<%= form_for @return_reason, url: solidus_admin.return_reasons_path, html: { id: form_id } do |f| %>
<div class="flex flex-col gap-6 pb-4">
<%= render component("ui/forms/field").text_field(f, :name, class: "required") %>
<label class="flex gap-2 items-center">
<%= render component("ui/forms/checkbox").new(
name: "#{f.object_name}[active]",
value: "1",
checked: f.object.active
) %>
<span class="font-semibold text-xs ml-2"><%= Spree::ReturnReason.human_attribute_name :active %></span>
<%= render component("ui/toggletip").new(text: t(".hints.active")) %>
</label>
</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("return_reasons/index").new(page: @page) %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class SolidusAdmin::ReturnReasons::New::Component < SolidusAdmin::BaseComponent
def initialize(page:, return_reason:)
@page = page
@return_reason = return_reason
end

def form_id
dom_id(@return_reason, "#{stimulus_id}_new_return_reason_form")
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
en:
title: "New Return Reason"
cancel: "Cancel"
submit: "Add Return Reason"
hints:
active: "When checked, this retun reason will be available for selection when creating a customer return for an order."
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,55 @@
include SolidusAdmin::ControllerHelpers::Search

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

set_page_and_extract_portion_from(return_reasons)
set_index_page

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

def new
@return_reason = Spree::ReturnReason.new

set_index_page

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

def create
@return_reason = Spree::ReturnReason.new(return_reason_params)

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

format.html do
redirect_to solidus_admin.return_reasons_path, status: :see_other

Check warning on line 36 in admin/app/controllers/solidus_admin/return_reasons_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/return_reasons_controller.rb#L36

Added line #L36 was not covered by tests
end

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

Check warning on line 44 in admin/app/controllers/solidus_admin/return_reasons_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/return_reasons_controller.rb#L44

Added line #L44 was not covered by tests

respond_to do |format|
format.html do
page_component = component('return_reasons/new')

Check warning on line 48 in admin/app/controllers/solidus_admin/return_reasons_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/return_reasons_controller.rb#L46-L48

Added lines #L46 - L48 were not covered by tests
.new(page: @page, return_reason: @return_reason)

render page_component, status: :unprocessable_entity

Check warning on line 51 in admin/app/controllers/solidus_admin/return_reasons_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/return_reasons_controller.rb#L51

Added line #L51 was not covered by tests
end
end
end
end

def destroy
@return_reason = Spree::ReturnReason.find_by!(id: params[:id])

Expand All @@ -28,13 +65,22 @@

private

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

set_page_and_extract_portion_from(return_reasons)
end

def load_return_reason
@return_reason = Spree::ReturnReason.find_by!(id: params[:id])
authorize! action_name, @return_reason
end

def return_reason_params
params.require(:return_reason).permit(:return_reason_id, permitted_return_reason_attributes)
params.require(:return_reason).permit(:name, :active)
end
end
end
2 changes: 2 additions & 0 deletions admin/config/locales/return_reasons.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ en:
solidus_admin:
return_reasons:
title: "Return Reasons"
create:
success: "Return reason has been successfully created!"
destroy:
success: "Return Reasons were successfully removed."
2 changes: 1 addition & 1 deletion admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
admin_resources :zones, only: [:index, :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 :return_reasons, only: [:index, :new, :create, :destroy]
admin_resources :adjustment_reasons, only: [:index, :destroy]
admin_resources :store_credit_reasons, only: [:index, :destroy]
end
16 changes: 16 additions & 0 deletions admin/spec/features/return_reasons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,20 @@
expect(Spree::ReturnReason.count).to eq(0)
expect(page).to be_axe_clean
end

it "allows an admin to create a new RMA reason" do
visit "/admin/return_reasons"

expect(page).to have_content("Add new")
click_on "Add new"

expect(page).to have_content("New Return Reason")
fill_in "Name", with: "Size too small"

expect(page).to have_css("input[type=checkbox][name='return_reason[active]'][checked='checked']")

click_on "Add Return Reason"

expect(page).to have_content("Return reason has been successfully created!")
end
end