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] Add Refunds and Returns section with correlated index pages #5539

Merged
merged 8 commits into from
Dec 6, 2023
Prev Previous commit
Next Next commit
Add adjustment reasons index page with dedicated actions
  • Loading branch information
rainerdema committed Dec 6, 2023
commit 124ebbd80752d485ee37fd0b9bfd2a2581aaab6c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<%= render component('refunds_and_returns').new(current_class: Spree::AdjustmentReason) do |layout| %>
<% layout.with_actions do %>
<%= render component("ui/button").new(
tag: :a,
text: t('.add'),
href: spree.new_admin_adjustment_reason_path,
icon: "add-line",
class: "align-self-end w-full",
) %>
<% end %>
<%= render component('ui/table').new(
id: stimulus_id,
data: {
class: Spree::AdjustmentReason,
rows: @page.records,
url: ->(adjustment_reason) { spree.edit_admin_adjustment_reason_path(adjustment_reason) },
prev: prev_page_path,
next: next_page_path,
columns: columns,
batch_actions: batch_actions,
},
search: {
name: :q,
value: params[:q],
url: solidus_admin.adjustment_reasons_path,
searchbar_key: :name_or_code_cont,
filters: filters,
scopes: scopes,
},
) %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

class SolidusAdmin::AdjustmentReasons::Index::Component < SolidusAdmin::BaseComponent
include SolidusAdmin::Layout::PageHelpers

def initialize(page:)
@page = page
end

def title
Spree::AdjustmentReason.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.adjustment_reasons_path,
method: :delete,
icon: 'delete-bin-7-line',
},
]
end

def filters
[]
end

def scopes
[]
end

def columns
[
:name,
:code,
{
header: :active,
data: ->(adjustment_reason) do
adjustment_reason.active? ? component('ui/badge').yes : component('ui/badge').no
end
},
]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
add: 'Add new'
batch_actions:
delete: 'Delete'
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def tabs
Spree::RefundReason => solidus_admin.refund_reasons_path,
Spree::ReimbursementType => solidus_admin.reimbursement_types_path,
Spree::ReturnReason => solidus_admin.return_reasons_path,
Spree::AdjustmentReason => solidus_admin.adjustment_reasons_path,
}
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module SolidusAdmin
class AdjustmentReasonsController < SolidusAdmin::BaseController
include SolidusAdmin::ControllerHelpers::Search

def index
adjustment_reasons = apply_search_to(
Spree::AdjustmentReason.order(id: :desc),
param: :q,
)

set_page_and_extract_portion_from(adjustment_reasons)

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

def destroy
@adjustment_reason = Spree::AdjustmentReason.find_by!(id: params[:id])

Spree::AdjustmentReason.transaction { @adjustment_reason.destroy }

flash[:notice] = t('.success')
redirect_back_or_to adjustment_reasons_path, status: :see_other
end

private

def load_adjustment_reason
@adjustment_reason = Spree::AdjustmentReason.find_by!(id: params[:id])
authorize! action_name, @adjustment_reason
end

def adjustment_reason_params
params.require(:adjustment_reason).permit(:adjustment_reason_id, permitted_adjustment_reason_attributes)
end
end
end
6 changes: 6 additions & 0 deletions admin/config/locales/adjustment_reasons.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
en:
solidus_admin:
adjustment_reasons:
title: "Adjustment Reasons"
destroy:
success: "Adjustment Reasons were successfully removed."
1 change: 1 addition & 0 deletions admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@
admin_resources :refund_reasons, only: [:index, :destroy]
admin_resources :reimbursement_types, only: [:index]
admin_resources :return_reasons, only: [:index, :destroy]
admin_resources :adjustment_reasons, only: [:index, :destroy]
end
22 changes: 22 additions & 0 deletions admin/spec/features/adjustment_reasons_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require 'spec_helper'

describe "Adjustment Reasons", :js, type: :feature do
before { sign_in create(:admin_user, email: '[email protected]') }

it "lists adjustment reasons and allows deleting them" do
create(:adjustment_reason, name: "Default-adjustment-reason")

visit "/admin/adjustment_reasons"
expect(page).to have_content("Default-adjustment-reason")
expect(page).to be_axe_clean

select_row("Default-adjustment-reason")
click_on "Delete"
expect(page).to have_content("Adjustment Reasons were successfully removed.")
expect(page).not_to have_content("Default-adjustment-reason")
expect(Spree::AdjustmentReason.count).to eq(0)
expect(page).to be_axe_clean
end
end