From c8ac5b4abd62ee09560057f8d1faa3e4a2a19c7b Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Thu, 19 Jun 2025 18:37:32 +0200 Subject: [PATCH 01/16] Create custom form builder This will allow to clean up existing forms and write more concise forms in the future. Custom form builder encapsulates all the calls to "render", "component()" initializations, and assumes some defaults, e.g. if "checked" is not passed to "f.checkbox" we get it by calling @object.send(method) --- .../solidus_admin/base_component.rb | 1 + .../solidus_admin/solidus_form_helper.rb | 9 ++++ admin/app/lib/solidus_admin/form_builder.rb | 51 +++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 admin/app/helpers/solidus_admin/solidus_form_helper.rb create mode 100644 admin/app/lib/solidus_admin/form_builder.rb diff --git a/admin/app/components/solidus_admin/base_component.rb b/admin/app/components/solidus_admin/base_component.rb index e39c1707a4b..37daa9fea48 100644 --- a/admin/app/components/solidus_admin/base_component.rb +++ b/admin/app/components/solidus_admin/base_component.rb @@ -9,6 +9,7 @@ class BaseComponent < ViewComponent::Base include SolidusAdmin::ComponentsHelper include SolidusAdmin::StimulusHelper include SolidusAdmin::VoidElementsHelper + include SolidusAdmin::SolidusFormHelper include Turbo::FramesHelper def icon_tag(name, **attrs) diff --git a/admin/app/helpers/solidus_admin/solidus_form_helper.rb b/admin/app/helpers/solidus_admin/solidus_form_helper.rb new file mode 100644 index 00000000000..c4a335ba930 --- /dev/null +++ b/admin/app/helpers/solidus_admin/solidus_form_helper.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module SolidusAdmin + module SolidusFormHelper + def solidus_form_for(*args, **kwargs, &block) + form_for(*args, **kwargs, builder: SolidusAdmin::FormBuilder, &block) + end + end +end diff --git a/admin/app/lib/solidus_admin/form_builder.rb b/admin/app/lib/solidus_admin/form_builder.rb new file mode 100644 index 00000000000..f09281504b1 --- /dev/null +++ b/admin/app/lib/solidus_admin/form_builder.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +class SolidusAdmin::FormBuilder < ActionView::Helpers::FormBuilder + include SolidusAdmin::ComponentsHelper + + delegate :render, to: :@template + + def text_field(method, **options) + render component("ui/forms/field").text_field(self, method, **options) + end + + def text_area(method, **options) + render component("ui/forms/field").text_area(self, method, **options) + end + + def select(method, choices, **options) + render component("ui/forms/field").select(self, method, choices, **options) + end + + def checkbox(method, checked: nil, **options, &block) + checked = @object.public_send(method) if checked.nil? + component_instance = component("ui/forms/checkbox").new(object_name: @object_name, checked:, method:, **options) + render component_instance, &block + end + + def checkbox_row(method, options:, row_title:, **attrs) + render component("ui/checkbox_row").new(form: self, method:, options:, row_title:, **attrs) + end + + def input(method, **options) + name = "#{@object_name}[#{method}]" + value = @object.public_send(method) if options[:value].nil? + render component("ui/forms/input").new(name:, value:, **options) + end + + def hidden_field(method, **options) + input(method, type: :hidden, autocomplete: "off", **options) + end + + def switch_field(method, label: nil, include_hidden: true, **options) + label = @object.class.human_attribute_name(method) if label.nil? + name = "#{@object_name}[#{method}]" + error = @object.errors[method] + checked = @object.public_send(method) + render component("ui/forms/switch_field").new(label:, name:, error:, checked:, include_hidden:, **options) + end + + def submit(text, **options) + render component("ui/button").new(type: :submit, text:, form: id, **options) + end +end From 7b3c7d481cb1a373d268b8505dfc42da0f921f93 Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Mon, 30 Jun 2025 16:22:08 +0200 Subject: [PATCH 02/16] Update payment_methods_controller.rb Inherit from resources controller, define necessary overriding methods. --- .../payment_methods_controller.rb | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/admin/app/controllers/solidus_admin/payment_methods_controller.rb b/admin/app/controllers/solidus_admin/payment_methods_controller.rb index fc3ce0316dd..1d7c0d2d3ff 100644 --- a/admin/app/controllers/solidus_admin/payment_methods_controller.rb +++ b/admin/app/controllers/solidus_admin/payment_methods_controller.rb @@ -1,8 +1,7 @@ # frozen_string_literal: true module SolidusAdmin - class PaymentMethodsController < SolidusAdmin::BaseController - include SolidusAdmin::ControllerHelpers::Search + class PaymentMethodsController < SolidusAdmin::ResourcesController include SolidusAdmin::Moveable search_scope(:all) @@ -11,26 +10,17 @@ class PaymentMethodsController < SolidusAdmin::BaseController 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, - ) + private - set_page_and_extract_portion_from(payment_methods) + def resource_class = Spree::PaymentMethod - respond_to do |format| - format.html { render component('payment_methods/index').new(page: @page) } - end - end - - def destroy - @payment_methods = Spree::PaymentMethod.where(id: params[:id]) + def resources_collection = resource_class.all - Spree::PaymentMethod.transaction { @payment_methods.destroy_all } + def resources_sorting_options = { position: :asc } - flash[:notice] = t('.success') - redirect_back_or_to payment_methods_path, status: :see_other + def permitted_resource_params + params.require(:payment_method).permit(:name, :description, :auto_capture, :type, :preference_source, + :preferred_server, :preferred_test_mode, :active, :available_to_admin, :available_to_users, store_ids: []) end end end From e3b87c540881d87eb10c1eca310ad82a9613fd73 Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Mon, 30 Jun 2025 16:27:20 +0200 Subject: [PATCH 03/16] Update payment methods index component Render links in the table. --- .../solidus_admin/payment_methods/index/component.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/admin/app/components/solidus_admin/payment_methods/index/component.rb b/admin/app/components/solidus_admin/payment_methods/index/component.rb index 7bf64ffa63f..66f0e55d55c 100644 --- a/admin/app/components/solidus_admin/payment_methods/index/component.rb +++ b/admin/app/components/solidus_admin/payment_methods/index/component.rb @@ -59,13 +59,13 @@ def columns { header: :name, data: ->(payment_method) do - content_tag :div, payment_method.name + link_to payment_method.name, row_url(payment_method), class: "body-link" end }, { header: :type, data: ->(payment_method) do - content_tag :div, payment_method.model_name.human + link_to payment_method.model_name.human, row_url(payment_method), class: "body-link" end }, { From 1daa6e099edf3e9beda3d381d1ae645dcf8101bf Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Mon, 30 Jun 2025 16:31:25 +0200 Subject: [PATCH 04/16] Request confirmation when deleting payment methods --- .../components/solidus_admin/payment_methods/index/component.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/admin/app/components/solidus_admin/payment_methods/index/component.rb b/admin/app/components/solidus_admin/payment_methods/index/component.rb index 66f0e55d55c..0daa753f0d5 100644 --- a/admin/app/components/solidus_admin/payment_methods/index/component.rb +++ b/admin/app/components/solidus_admin/payment_methods/index/component.rb @@ -40,6 +40,7 @@ def batch_actions action: solidus_admin.payment_methods_path, method: :delete, icon: 'delete-bin-7-line', + require_confirmation: true }, ] end From 1823cd34c011e70fc7ae4a90387db5b65d983a81 Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Mon, 30 Jun 2025 20:39:28 +0200 Subject: [PATCH 05/16] Put checkbox label and hint definition in builder --- admin/app/lib/solidus_admin/form_builder.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/admin/app/lib/solidus_admin/form_builder.rb b/admin/app/lib/solidus_admin/form_builder.rb index f09281504b1..81c0763163e 100644 --- a/admin/app/lib/solidus_admin/form_builder.rb +++ b/admin/app/lib/solidus_admin/form_builder.rb @@ -17,10 +17,17 @@ def select(method, choices, **options) render component("ui/forms/field").select(self, method, choices, **options) end - def checkbox(method, checked: nil, **options, &block) + def checkbox(method, label: nil, checked: nil, hint: nil, **options) + label = @object.class.human_attribute_name(method) if label.nil? + label_options = options.delete(:label_options) || {} checked = @object.public_send(method) if checked.nil? + hint_options = options.delete(:hint_options) || {} + component_instance = component("ui/forms/checkbox").new(object_name: @object_name, checked:, method:, **options) - render component_instance, &block + render component_instance do |checkbox| + checkbox.with_label(text: label, **label_options) + checkbox.with_hint(text: hint, **hint_options) if hint + end end def checkbox_row(method, options:, row_title:, **attrs) From a9e9e8559fb2db9eb51748aa8e4681769a2a1bf0 Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Fri, 4 Jul 2025 19:22:45 +0200 Subject: [PATCH 06/16] Create payment method form component --- .../payment_methods/form/component.html.erb | 27 +++++++++++++++++++ .../payment_methods/form/component.rb | 11 ++++++++ .../payment_methods/form/component.yml | 11 ++++++++ 3 files changed, 49 insertions(+) create mode 100644 admin/app/components/solidus_admin/payment_methods/form/component.html.erb create mode 100644 admin/app/components/solidus_admin/payment_methods/form/component.rb create mode 100644 admin/app/components/solidus_admin/payment_methods/form/component.yml diff --git a/admin/app/components/solidus_admin/payment_methods/form/component.html.erb b/admin/app/components/solidus_admin/payment_methods/form/component.html.erb new file mode 100644 index 00000000000..fdc7f1ea1db --- /dev/null +++ b/admin/app/components/solidus_admin/payment_methods/form/component.html.erb @@ -0,0 +1,27 @@ +<%= solidus_form_for @payment_method, as: "payment_method", url: @url, html: { id: @form_id } do |f| %> + <%= page_with_sidebar do %> + <%= page_with_sidebar_main do %> + <%= render component("ui/panel").new do %> + <%= f.text_field :name %> + <%= f.text_field :description %> + <%= f.switch_field :auto_capture, hint: t(".hints.autocapture").html_safe %> + <% end %> + + <%= render component("ui/panel").new(title: t(".deployment")) do %> + <%= f.select :type, Rails.application.config.spree.payment_methods.map { [_1.model_name.human, _1.to_s] } %> + <%= f.select :preference_source, Spree::PaymentMethod.available_preference_sources, include_blank: t(".preference_source_none") %> + <%= f.text_field :preferred_server %> + <%= f.switch_field :preferred_test_mode, hint: t(".hints.test_mode").html_safe %> + <% end %> + <% end %> + + <%= page_with_sidebar_aside do %> + <%= render component("ui/panel").new(title: t(".availability")) do %> + <%= f.checkbox :active %> + <%= f.select :store_ids, Spree::Store.pluck(:name, :id), multiple: true %> + <%= f.checkbox :available_to_admin %> + <%= f.checkbox :available_to_users %> + <% end %> + <% end %> + <% end %> +<% end %> diff --git a/admin/app/components/solidus_admin/payment_methods/form/component.rb b/admin/app/components/solidus_admin/payment_methods/form/component.rb new file mode 100644 index 00000000000..114647b8966 --- /dev/null +++ b/admin/app/components/solidus_admin/payment_methods/form/component.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class SolidusAdmin::PaymentMethods::Form::Component < SolidusAdmin::BaseComponent + include SolidusAdmin::Layout::PageHelpers + + def initialize(payment_method:, url:, form_id:) + @payment_method = payment_method + @url = url + @form_id = form_id + end +end diff --git a/admin/app/components/solidus_admin/payment_methods/form/component.yml b/admin/app/components/solidus_admin/payment_methods/form/component.yml new file mode 100644 index 00000000000..97e8ed8b578 --- /dev/null +++ b/admin/app/components/solidus_admin/payment_methods/form/component.yml @@ -0,0 +1,11 @@ +en: + availability: "Availability" + deployment: "Deployment" + hints: + autocapture: >- +

Auto-capture setting charges customer's account upon transaction authorization.

+

Enable to reduce manual intervention and streamline the payment process.

+ test_mode: >- +

Payment methods test mode allows users to simulate transactions using dummy data, ensuring the payment gateway's functionality without real transactions.

+

In test mode, users can check if payment methods, such as credit cards or digital wallets, are functioning correctly before going live with real transactions.

+ preference_source_none: "Custom" From a6ba288abcdefd9bd65ce7f31f6ffb79ba4d7761 Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Fri, 4 Jul 2025 19:23:15 +0200 Subject: [PATCH 07/16] Create component for new payment methods --- .../payment_methods/new/component.html.erb | 19 +++++++++++++++++++ .../payment_methods/new/component.rb | 5 +++++ .../payment_methods/new/component.yml | 5 +++++ 3 files changed, 29 insertions(+) create mode 100644 admin/app/components/solidus_admin/payment_methods/new/component.html.erb create mode 100644 admin/app/components/solidus_admin/payment_methods/new/component.rb create mode 100644 admin/app/components/solidus_admin/payment_methods/new/component.yml diff --git a/admin/app/components/solidus_admin/payment_methods/new/component.html.erb b/admin/app/components/solidus_admin/payment_methods/new/component.html.erb new file mode 100644 index 00000000000..6c2e2c9ecb8 --- /dev/null +++ b/admin/app/components/solidus_admin/payment_methods/new/component.html.erb @@ -0,0 +1,19 @@ + + +<%= page id: :resource_modal do %> + <%= page_header do %> + <%= page_header_back(solidus_admin.payment_methods_path) %> + <%= page_header_title(t(".title")) %> + <%= page_header_actions do %> + <%= render component("ui/button").new( + tag: :a, + text: t(".discard"), + href: solidus_admin.payment_methods_path, + scheme: :secondary + ) %> + <%= render component("ui/button").new(tag: :button, text: t(".save"), form: form_id) %> + <% end %> + <% end %> + + <%= render component("payment_methods/form").new(payment_method: @resource, url: solidus_admin.payment_methods_path, form_id:) %> +<% end %> diff --git a/admin/app/components/solidus_admin/payment_methods/new/component.rb b/admin/app/components/solidus_admin/payment_methods/new/component.rb new file mode 100644 index 00000000000..f384cd56b11 --- /dev/null +++ b/admin/app/components/solidus_admin/payment_methods/new/component.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class SolidusAdmin::PaymentMethods::New::Component < SolidusAdmin::Resources::New::Component + include SolidusAdmin::Layout::PageHelpers +end diff --git a/admin/app/components/solidus_admin/payment_methods/new/component.yml b/admin/app/components/solidus_admin/payment_methods/new/component.yml new file mode 100644 index 00000000000..57197ddaac4 --- /dev/null +++ b/admin/app/components/solidus_admin/payment_methods/new/component.yml @@ -0,0 +1,5 @@ +en: + back: "Back" + discard: "Discard" + save: "Save" + title: "New Payment Method" From 7f72dc969a56afe97ecfe22a397d9cad3d7ddd76 Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Fri, 4 Jul 2025 19:23:31 +0200 Subject: [PATCH 08/16] Create component for payment methods edits --- .../payment_methods/edit/component.html.erb | 19 +++++++++++++++++++ .../payment_methods/edit/component.rb | 5 +++++ .../payment_methods/edit/component.yml | 5 +++++ 3 files changed, 29 insertions(+) create mode 100644 admin/app/components/solidus_admin/payment_methods/edit/component.html.erb create mode 100644 admin/app/components/solidus_admin/payment_methods/edit/component.rb create mode 100644 admin/app/components/solidus_admin/payment_methods/edit/component.yml diff --git a/admin/app/components/solidus_admin/payment_methods/edit/component.html.erb b/admin/app/components/solidus_admin/payment_methods/edit/component.html.erb new file mode 100644 index 00000000000..5224b1e50e2 --- /dev/null +++ b/admin/app/components/solidus_admin/payment_methods/edit/component.html.erb @@ -0,0 +1,19 @@ + + +<%= page id: :resource_modal do %> + <%= page_header do %> + <%= page_header_back(solidus_admin.payment_methods_path) %> + <%= page_header_title(t(".title")) %> + <%= page_header_actions do %> + <%= render component("ui/button").new( + tag: :a, + text: t(".discard"), + href: solidus_admin.payment_methods_path, + scheme: :secondary + ) %> + <%= render component("ui/button").new(tag: :button, text: t(".save"), form: form_id) %> + <% end %> + <% end %> + + <%= render component("payment_methods/form").new(payment_method: @resource, url: solidus_admin.payment_method_path(@resource), form_id:) %> +<% end %> diff --git a/admin/app/components/solidus_admin/payment_methods/edit/component.rb b/admin/app/components/solidus_admin/payment_methods/edit/component.rb new file mode 100644 index 00000000000..08034e8db8e --- /dev/null +++ b/admin/app/components/solidus_admin/payment_methods/edit/component.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class SolidusAdmin::PaymentMethods::Edit::Component < SolidusAdmin::Resources::Edit::Component + include SolidusAdmin::Layout::PageHelpers +end diff --git a/admin/app/components/solidus_admin/payment_methods/edit/component.yml b/admin/app/components/solidus_admin/payment_methods/edit/component.yml new file mode 100644 index 00000000000..4a30ee3de6a --- /dev/null +++ b/admin/app/components/solidus_admin/payment_methods/edit/component.yml @@ -0,0 +1,5 @@ +en: + back: "Back" + discard: "Discard" + save: "Save" + title: "Edit Payment Method" From b8b8f93e495ba31698e92a8ef0b75817412106a1 Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Fri, 4 Jul 2025 19:23:52 +0200 Subject: [PATCH 09/16] Update payment method translation strings --- admin/config/locales/payment_methods.en.yml | 6 +++++- core/config/locales/en.yml | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/admin/config/locales/payment_methods.en.yml b/admin/config/locales/payment_methods.en.yml index c9b1308e35e..53ef2fc2c00 100644 --- a/admin/config/locales/payment_methods.en.yml +++ b/admin/config/locales/payment_methods.en.yml @@ -2,5 +2,9 @@ en: solidus_admin: payment_methods: title: "Payment Methods" + create: + success: "Payment method was successfully created." destroy: - success: "Payment Methods were successfully removed." + success: "Payment methods were successfully removed." + update: + success: "Payment method was successfully updated." diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml index ad8164abb08..99957eb3a4b 100644 --- a/core/config/locales/en.yml +++ b/core/config/locales/en.yml @@ -158,6 +158,9 @@ en: display_on: Display name: Name preference_source: Preference Source + preferred_server: Server + preferred_test_mode: Test Mode + store_ids: Stores type: Type spree/price: amount: Price From c25258580bf0fc2e8c90c50c239d680107566af2 Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Fri, 4 Jul 2025 19:24:26 +0200 Subject: [PATCH 10/16] Update payment method routes to direct to solidus admin --- admin/config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/config/routes.rb b/admin/config/routes.rb index ec215522965..3b1c9ce2b5d 100644 --- a/admin/config/routes.rb +++ b/admin/config/routes.rb @@ -74,7 +74,7 @@ admin_resources :promotion_categories, only: [:index, :destroy] admin_resources :tax_categories, except: [:show] admin_resources :tax_rates, only: [:index, :destroy] - admin_resources :payment_methods, only: [:index, :destroy], sortable: true + admin_resources :payment_methods, except: [:show], sortable: true admin_resources :stock_items, only: [:index, :edit, :update] admin_resources :shipping_methods, only: [:index, :destroy] admin_resources :shipping_categories, except: [:show] From 3d85ab1b7699bc7063c52f8f17e07059bb3c1dd0 Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Fri, 4 Jul 2025 19:25:06 +0200 Subject: [PATCH 11/16] Update payment methods index component Removes row_url and replaces it with edit_path and usages of new routing paths. --- .../solidus_admin/payment_methods/index/component.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/admin/app/components/solidus_admin/payment_methods/index/component.rb b/admin/app/components/solidus_admin/payment_methods/index/component.rb index 0daa753f0d5..9a7f34f6fd4 100644 --- a/admin/app/components/solidus_admin/payment_methods/index/component.rb +++ b/admin/app/components/solidus_admin/payment_methods/index/component.rb @@ -13,8 +13,8 @@ def search_url solidus_admin.payment_methods_path end - def row_url(payment_method) - spree.edit_admin_payment_method_path(payment_method) + def edit_path(payment_method) + solidus_admin.edit_payment_method_path(payment_method) end def sortable_options @@ -28,7 +28,7 @@ def page_actions render component("ui/button").new( tag: :a, text: t('.add'), - href: spree.new_admin_payment_method_path, + href: solidus_admin.new_payment_method_path, icon: "add-line", ) end @@ -60,13 +60,13 @@ def columns { header: :name, data: ->(payment_method) do - link_to payment_method.name, row_url(payment_method), class: "body-link" + link_to payment_method.name, edit_path(payment_method), class: "body-link" end }, { header: :type, data: ->(payment_method) do - link_to payment_method.model_name.human, row_url(payment_method), class: "body-link" + link_to payment_method.model_name.human, edit_path(payment_method), class: "body-link" end }, { From 0deee30168bdd7f49a70f4f2a4e7fada0c8c939a Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Fri, 4 Jul 2025 20:31:23 +0200 Subject: [PATCH 12/16] Update page with form identifier For validation errors to be shown correctly we need a proper identifier for turbo stream to replace, default one used by resources controller is :resource_modal but this name does not fit for our page so we use a custom name. --- .../solidus_admin/payment_methods/edit/component.html.erb | 4 +--- .../solidus_admin/payment_methods/new/component.html.erb | 4 +--- .../controllers/solidus_admin/payment_methods_controller.rb | 4 ++++ admin/app/controllers/solidus_admin/resources_controller.rb | 6 +++++- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/admin/app/components/solidus_admin/payment_methods/edit/component.html.erb b/admin/app/components/solidus_admin/payment_methods/edit/component.html.erb index 5224b1e50e2..1319c32e244 100644 --- a/admin/app/components/solidus_admin/payment_methods/edit/component.html.erb +++ b/admin/app/components/solidus_admin/payment_methods/edit/component.html.erb @@ -1,6 +1,4 @@ - - -<%= page id: :resource_modal do %> +<%= page id: :payment_method_form do %> <%= page_header do %> <%= page_header_back(solidus_admin.payment_methods_path) %> <%= page_header_title(t(".title")) %> diff --git a/admin/app/components/solidus_admin/payment_methods/new/component.html.erb b/admin/app/components/solidus_admin/payment_methods/new/component.html.erb index 6c2e2c9ecb8..fcf0b5417a3 100644 --- a/admin/app/components/solidus_admin/payment_methods/new/component.html.erb +++ b/admin/app/components/solidus_admin/payment_methods/new/component.html.erb @@ -1,6 +1,4 @@ - - -<%= page id: :resource_modal do %> +<%= page id: :payment_method_form do %> <%= page_header do %> <%= page_header_back(solidus_admin.payment_methods_path) %> <%= page_header_title(t(".title")) %> diff --git a/admin/app/controllers/solidus_admin/payment_methods_controller.rb b/admin/app/controllers/solidus_admin/payment_methods_controller.rb index 1d7c0d2d3ff..746fb068ad5 100644 --- a/admin/app/controllers/solidus_admin/payment_methods_controller.rb +++ b/admin/app/controllers/solidus_admin/payment_methods_controller.rb @@ -22,5 +22,9 @@ def permitted_resource_params params.require(:payment_method).permit(:name, :description, :auto_capture, :type, :preference_source, :preferred_server, :preferred_test_mode, :active, :available_to_admin, :available_to_users, store_ids: []) end + + def resource_form_container + :payment_method_form + end end end diff --git a/admin/app/controllers/solidus_admin/resources_controller.rb b/admin/app/controllers/solidus_admin/resources_controller.rb index b4656a2f5d2..a0ff326d0a4 100644 --- a/admin/app/controllers/solidus_admin/resources_controller.rb +++ b/admin/app/controllers/solidus_admin/resources_controller.rb @@ -133,7 +133,7 @@ def render_resource_form_with_errors(page_component) render page_component, status: :unprocessable_entity end format.turbo_stream do - render turbo_stream: turbo_stream.replace(:resource_modal, page_component), + render turbo_stream: turbo_stream.replace(resource_form_container, page_component), status: :unprocessable_entity end end @@ -165,5 +165,9 @@ def blueprint_view raise NotImplementedError, "You must implement the blueprint_view method in #{self.class}" end + + def resource_form_container + :resource_modal + end end end From c7f012aab8c943146dce640792469659e2d39886 Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Fri, 4 Jul 2025 20:39:32 +0200 Subject: [PATCH 13/16] Add #switch feature helper --- admin/lib/solidus_admin/testing_support/feature_helpers.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/admin/lib/solidus_admin/testing_support/feature_helpers.rb b/admin/lib/solidus_admin/testing_support/feature_helpers.rb index 6f28ad553cb..cd81a9f46c0 100644 --- a/admin/lib/solidus_admin/testing_support/feature_helpers.rb +++ b/admin/lib/solidus_admin/testing_support/feature_helpers.rb @@ -61,6 +61,11 @@ def clear_search find('button[aria-label="Clear"]').click end end + + def switch(locator, on: true) + checkbox = find(:label, text: locator).find(:checkbox) + on ? checkbox.check : checkbox.uncheck + end end end end From 2c80785b05f8bff2c3eaea97b0070d20732d54ac Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Fri, 4 Jul 2025 20:40:26 +0200 Subject: [PATCH 14/16] Update payment methods feature tests --- admin/spec/features/payment_methods_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/admin/spec/features/payment_methods_spec.rb b/admin/spec/features/payment_methods_spec.rb index 06b4dc0f3ab..efa768c7dc0 100644 --- a/admin/spec/features/payment_methods_spec.rb +++ b/admin/spec/features/payment_methods_spec.rb @@ -41,8 +41,8 @@ expect(page).to be_axe_clean select_row("Check") - click_on "Delete" - expect(page).to have_content("Payment Methods were successfully removed.") + accept_confirm("Are you sure you want to delete 1 payment method?") { 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 From 16dbc8cc5d1508add44605407e8fa4c738782b80 Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Fri, 4 Jul 2025 21:02:05 +0200 Subject: [PATCH 15/16] Add missing feature tests --- admin/spec/features/payment_methods_spec.rb | 70 +++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/admin/spec/features/payment_methods_spec.rb b/admin/spec/features/payment_methods_spec.rb index efa768c7dc0..a3aeb7d8295 100644 --- a/admin/spec/features/payment_methods_spec.rb +++ b/admin/spec/features/payment_methods_spec.rb @@ -52,4 +52,74 @@ let(:displayed_attribute) { :name } let(:path) { solidus_admin.payment_methods_path } end + + context "creating payment method" do + before { create(:store, name: "Store") } + context "with valid attributes" do + it "creates payment method" do + visit "/admin/payment_methods" + click_on "Add new" + + expect(page).to have_current_path("/admin/payment_methods/new") + expect(page).to be_axe_clean + + fill_in "Name", with: "Checking" + fill_in "Description", with: "Payment Method Description" + switch "Auto Capture" + solidus_select "Check Payments", from: "Type" + fill_in "Server", with: "test" + switch "Test Mode" + check "Active" + solidus_select("Store", from: "Stores") + check "Available to Admin" + check "Available to Users" + + click_on "Save" + + expect(page).to have_content("Payment method was successfully created.") + expect(page).to have_content("Checking") + expect(page).to have_content("Check Payments") + end + end + + context "with invalid attributes" do + it "shows validation errors" do + visit "/admin/payment_methods" + click_on "Add new" + click_on "Save" + expect(page).to have_content("can't be blank") + end + end + end + + context "updating payment method" do + before { create(:payment_method, name: "Check payments") } + + context "with valid attributes" do + it "updates payment method" do + visit "/admin/payment_methods" + click_on "Check payments" + + fill_in "Name", with: "Checking payments" + solidus_select "Check Payments", from: "Type" + click_on "Save" + + expect(page).to have_content("Payment method was successfully updated.") + expect(page).to have_content("Checking payments") + expect(page).to have_content("Check Payments") + end + end + + context "with invalid attributes" do + it "shows validation errors" do + visit "/admin/payment_methods" + click_on "Check payments" + + fill_in "Name", with: "" + click_on "Save" + + expect(page).to have_content("can't be blank") + end + end + end end From e701f96ce823895001ff58b9cc8c9dccddd1a2cf Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Fri, 4 Jul 2025 21:11:05 +0200 Subject: [PATCH 16/16] Add request tests --- admin/spec/requests/solidus_admin/payment_methods_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/admin/spec/requests/solidus_admin/payment_methods_spec.rb b/admin/spec/requests/solidus_admin/payment_methods_spec.rb index 5cc99dae2ab..e452d8f0fab 100644 --- a/admin/spec/requests/solidus_admin/payment_methods_spec.rb +++ b/admin/spec/requests/solidus_admin/payment_methods_spec.rb @@ -2,10 +2,17 @@ require "spec_helper" require "solidus_admin/testing_support/shared_examples/moveable" +require 'solidus_admin/testing_support/shared_examples/crud_resource_requests' RSpec.describe "SolidusAdmin::PaymentMethodsController", type: :request do it_behaves_like "requests: moveable" do let(:factory) { :payment_method } let(:request_path) { solidus_admin.move_payment_method_path(record, format: :js) } end + + include_examples "CRUD resource requests", "payment_method" do + let(:resource_class) { Spree::PaymentMethod } + let(:valid_attributes) { { name: "Credit Card", type: "Spree::PaymentMethod::BogusCreditCard" } } + let(:invalid_attributes) { { name: "", type: "" } } + end end