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 store credit reasons index page with dedicated actions
  • Loading branch information
rainerdema committed Dec 6, 2023
commit 2df44f1143f42ee004c00384c11338bb2265eb1d
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ def initialize(current_class:)
end

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,
Spree::StoreCreditReason => solidus_admin.store_credit_reasons_path,
}
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<%= render component('refunds_and_returns').new(current_class: Spree::StoreCreditReason) do |layout| %>
<% layout.with_actions do %>
<%= render component("ui/button").new(
tag: :a,
text: t('.add'),
href: spree.new_admin_store_credit_reason_path,
icon: "add-line",
class: "align-self-end w-full",
) %>
<% end %>
<%= render component('ui/table').new(
id: stimulus_id,
data: {
class: Spree::StoreCreditReason,
rows: @page.records,
url: ->(store_credit_reason) { spree.edit_admin_store_credit_reason_path(store_credit_reason) },
prev: prev_page_path,
next: next_page_path,
columns: columns,
batch_actions: batch_actions,
},
search: {
name: :q,
value: params[:q],
url: solidus_admin.store_credit_reasons_path,
searchbar_key: :name_cont,
filters: filters,
scopes: scopes,
},
) %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

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

def initialize(page:)
@page = page
end

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

def filters
[]
end

def scopes
[]
end

def columns
[
:name,
{
header: :active,
data: ->(store_credit_reason) do
store_credit_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
@@ -0,0 +1,40 @@
# frozen_string_literal: true

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

def index
store_credit_reasons = apply_search_to(
Spree::StoreCreditReason.order(id: :desc),
param: :q,
)

set_page_and_extract_portion_from(store_credit_reasons)

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

def destroy
@store_credit_reason = Spree::StoreCreditReason.find_by!(id: params[:id])

Spree::StoreCreditReason.transaction { @store_credit_reason.destroy }

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

private

def load_store_credit_reason
@store_credit_reason = Spree::StoreCreditReason.find_by!(id: params[:id])
authorize! action_name, @store_credit_reason
end

def store_credit_reason_params
params.require(:store_credit_reason).permit(:store_credit_reason_id, permitted_store_credit_reason_attributes)
end
end
end
6 changes: 6 additions & 0 deletions admin/config/locales/store_credit_reasons.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
en:
solidus_admin:
store_credit_reasons:
title: "Store Credit Reasons"
destroy:
success: "Store Credit 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 @@ -50,4 +50,5 @@
admin_resources :reimbursement_types, only: [:index]
admin_resources :return_reasons, only: [:index, :destroy]
admin_resources :adjustment_reasons, only: [:index, :destroy]
admin_resources :store_credit_reasons, only: [:index, :destroy]
end
22 changes: 22 additions & 0 deletions admin/spec/features/store_credit_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 "Store Credit Reasons", :js, type: :feature do
before { sign_in create(:admin_user, email: '[email protected]') }

it "lists Store Credit Reasons and allows deleting them" do
create(:store_credit_reason, name: "Default-store-credit-reason")

visit "/admin/store_credit_reasons"
expect(page).to have_content("Default-store-credit-reason")
expect(page).to be_axe_clean

select_row("Default-store-credit-reason")
click_on "Delete"
expect(page).to have_content("Store Credit Reasons were successfully removed.")
expect(page).not_to have_content("Default-store-credit-reason")
expect(Spree::StoreCreditReason.count).to eq(0)
expect(page).to be_axe_clean
end
end