Skip to content

Commit

Permalink
Add payment_methods/index component with dedicated actions
Browse files Browse the repository at this point in the history
  • Loading branch information
rainerdema committed Dec 1, 2023
1 parent 6b3fe71 commit dc968c6
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<%= page do %>
<%= page_header do %>
<%= page_header_title title %>
<%= page_header_actions do %>
<%= render component("ui/button").new(
tag: :a,
text: t('.add'),
href: spree.new_admin_payment_method_path,
icon: "add-line",
) %>
<% end %>
<% end %>
<%= render component('ui/table').new(
id: stimulus_id,
data: {
class: Spree::PaymentMethod,
rows: @payment_methods,
url: ->(payment_method) { spree.edit_admin_payment_method_path(payment_method) },
columns: columns,
batch_actions: batch_actions,
},
search: {
name: :q,
value: params[:q],
url: solidus_admin.payment_methods_path,
searchbar_key: :name_or_description_cont,
scopes: scopes,
},
sortable: {
url: ->(payment_method) { solidus_admin.move_payment_method_path(payment_method) },
param: 'position',
},
) %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

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

def initialize(payment_methods:)
@payment_methods = payment_methods
end

def title
Spree::PaymentMethod.model_name.human.pluralize
end

def batch_actions
[
{
display_name: t('.batch_actions.delete'),
action: solidus_admin.payment_methods_path,
method: :delete,
icon: 'delete-bin-7-line',
},
]
end

def scopes
[
{ name: :all, label: t('.scopes.all'), default: true },
{ name: :active, label: t('.scopes.active') },
{ name: :inactive, label: t('.scopes.inactive') },
{ name: :storefront, label: t('.scopes.storefront') },
{ name: :admin, label: t('.scopes.admin') },
]
end

def columns
[
{
header: :name,
data: ->(payment_method) do
content_tag :div, payment_method.name
end
},
{
header: :type,
data: ->(payment_method) do
content_tag :div, payment_method.model_name.human
end
},
{
header: :available_to_users,
data: ->(payment_method) do
if payment_method.available_to_users?
component('ui/badge').new(name: t('.yes'), color: :green)
else
component('ui/badge').new(name: t('.no'), color: :graphite_light)
end
end
},
{
header: :available_to_admin,
data: ->(payment_method) do
if payment_method.available_to_admin?
component('ui/badge').new(name: t('.yes'), color: :green)
else
component('ui/badge').new(name: t('.no'), color: :graphite_light)
end
end
},
{
header: :status,
data: ->(payment_method) do
if payment_method.active?
render component('ui/badge').new(name: t('.status.active'), color: :green)
else
render component('ui/badge').new(name: t('.status.inactive'), color: :graphite_light)
end
end
},
]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
en:
add: 'Add new'
"yes": "Yes"
"no": "No"
status:
active: Active
inactive: Inactive
scopes:
active: Active
inactive: Inactive
admin: Admin
storefront: Storefront
all: All
batch_actions:
delete: 'Delete'
50 changes: 50 additions & 0 deletions admin/app/controllers/solidus_admin/payment_methods_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

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

before_action :load_payment_method, only: [:move]

search_scope(:all)
search_scope(:active, default: true, &:active)
search_scope(:inactive) { _1.where.not(active: true) }
search_scope(:storefront, &:available_to_users)
search_scope(:admin, &:available_to_admin)

def index
@payment_methods = apply_search_to(
Spree::PaymentMethod.ordered_by_position,
param: :q,
)

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

def move
@payment_method.insert_at(params[:position].to_i)

respond_to do |format|
format.js { head :no_content }
end
end

def destroy
@payment_methods = Spree::PaymentMethod.where(id: params[:id])

Spree::PaymentMethod.transaction { @payment_methods.destroy_all }

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

private

def load_payment_method
@payment_method = Spree::PaymentMethod.find_by!(id: params[:id])
authorize! action_name, @payment_method
end
end
end
6 changes: 6 additions & 0 deletions admin/config/locales/payment_methods.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
en:
solidus_admin:
payment_methods:
title: "Payment Methods"
destroy:
success: "Payment Methods were successfully removed."
9 changes: 9 additions & 0 deletions admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,13 @@
delete :destroy
end
end

resources :payment_methods, only: [:index] do
collection do
delete :destroy
end
member do
patch :move
end
end
end
48 changes: 48 additions & 0 deletions admin/spec/features/payment_methods_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

require 'spec_helper'

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

it "lists users and allows deleting them" do
create(:check_payment_method, name: "Check", active: true)
create(:simple_credit_card_payment_method, name: "Credit Card", active: false)
create(:store_credit_payment_method, name: "Store Credit Users", available_to_users: true)
create(:store_credit_payment_method, name: "Store Credit Admins", available_to_admin: true)

visit "/admin/payment_methods"
expect(page).to have_content("Check")
expect(page).not_to have_content("Credit Card")
expect(page).to have_content("Store Credit Users")
expect(page).to have_content("Store Credit Admins")
click_on "Inactive"
expect(page).not_to have_content("Check")
expect(page).to have_content("Credit Card")
expect(page).not_to have_content("Store Credit Users")
expect(page).not_to have_content("Store Credit Admins")
click_on "Admin"
expect(page).to have_content("Check")
expect(page).to have_content("Credit Card")
expect(page).to have_content("Store Credit Admins")
expect(page).not_to have_content("Store Credit Users")
click_on "Storefront"
expect(page).to have_content("Check")
expect(page).to have_content("Credit Card")
expect(page).not_to have_content("Store Credit Admins")
expect(page).to have_content("Store Credit Users")
click_on "All"
expect(page).to have_content("Check")
expect(page).to have_content("Credit Card")
expect(page).to have_content("Store Credit Admins")
expect(page).to have_content("Store Credit Users")

expect(page).to be_axe_clean

select_row("Check")
click_on "Delete"
expect(page).to have_content("Payment Methods were successfully removed.")
expect(page).not_to have_content("Check")
expect(Spree::PaymentMethod.count).to eq(3)
end
end

0 comments on commit dc968c6

Please sign in to comment.