From dc968c6641013ff16a8b01f67dbf76e915e5412f Mon Sep 17 00:00:00 2001 From: Rainer Dema Date: Fri, 1 Dec 2023 11:52:38 +0100 Subject: [PATCH] Add `payment_methods/index` component with dedicated actions --- .../payment_methods/index/component.html.erb | 35 ++++++++ .../payment_methods/index/component.rb | 81 +++++++++++++++++++ .../payment_methods/index/component.yml | 15 ++++ .../payment_methods_controller.rb | 50 ++++++++++++ admin/config/locales/payment_methods.en.yml | 6 ++ admin/config/routes.rb | 9 +++ admin/spec/features/payment_methods_spec.rb | 48 +++++++++++ 7 files changed, 244 insertions(+) create mode 100644 admin/app/components/solidus_admin/payment_methods/index/component.html.erb create mode 100644 admin/app/components/solidus_admin/payment_methods/index/component.rb create mode 100644 admin/app/components/solidus_admin/payment_methods/index/component.yml create mode 100644 admin/app/controllers/solidus_admin/payment_methods_controller.rb create mode 100644 admin/config/locales/payment_methods.en.yml create mode 100644 admin/spec/features/payment_methods_spec.rb diff --git a/admin/app/components/solidus_admin/payment_methods/index/component.html.erb b/admin/app/components/solidus_admin/payment_methods/index/component.html.erb new file mode 100644 index 00000000000..33b7c29e13c --- /dev/null +++ b/admin/app/components/solidus_admin/payment_methods/index/component.html.erb @@ -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 %> diff --git a/admin/app/components/solidus_admin/payment_methods/index/component.rb b/admin/app/components/solidus_admin/payment_methods/index/component.rb new file mode 100644 index 00000000000..230a128f8b5 --- /dev/null +++ b/admin/app/components/solidus_admin/payment_methods/index/component.rb @@ -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 diff --git a/admin/app/components/solidus_admin/payment_methods/index/component.yml b/admin/app/components/solidus_admin/payment_methods/index/component.yml new file mode 100644 index 00000000000..34acf86bda7 --- /dev/null +++ b/admin/app/components/solidus_admin/payment_methods/index/component.yml @@ -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' diff --git a/admin/app/controllers/solidus_admin/payment_methods_controller.rb b/admin/app/controllers/solidus_admin/payment_methods_controller.rb new file mode 100644 index 00000000000..23056b9e2c9 --- /dev/null +++ b/admin/app/controllers/solidus_admin/payment_methods_controller.rb @@ -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 diff --git a/admin/config/locales/payment_methods.en.yml b/admin/config/locales/payment_methods.en.yml new file mode 100644 index 00000000000..c9b1308e35e --- /dev/null +++ b/admin/config/locales/payment_methods.en.yml @@ -0,0 +1,6 @@ +en: + solidus_admin: + payment_methods: + title: "Payment Methods" + destroy: + success: "Payment Methods were successfully removed." diff --git a/admin/config/routes.rb b/admin/config/routes.rb index 11e68f6af5c..7513e01a28e 100644 --- a/admin/config/routes.rb +++ b/admin/config/routes.rb @@ -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 diff --git a/admin/spec/features/payment_methods_spec.rb b/admin/spec/features/payment_methods_spec.rb new file mode 100644 index 00000000000..55ebf3ec74b --- /dev/null +++ b/admin/spec/features/payment_methods_spec.rb @@ -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: 'admin@example.com') } + + 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