Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions admin/app/components/solidus_admin/base_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%= page id: :payment_method_form 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 %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class SolidusAdmin::PaymentMethods::Edit::Component < SolidusAdmin::Resources::Edit::Component
include SolidusAdmin::Layout::PageHelpers
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
en:
back: "Back"
discard: "Discard"
save: "Save"
title: "Edit Payment Method"
Original file line number Diff line number Diff line change
@@ -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 %>
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
en:
availability: "Availability"
deployment: "Deployment"
hints:
autocapture: >-
<p>Auto-capture setting charges customer's account upon transaction authorization.</p>
<p>Enable to reduce manual intervention and streamline the payment process.</p>
test_mode: >-
<p>Payment methods test mode allows users to simulate transactions using dummy data, ensuring the payment gateway's functionality without real transactions.</p>
<p>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.</p>
preference_source_none: "Custom"
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -40,6 +40,7 @@ def batch_actions
action: solidus_admin.payment_methods_path,
method: :delete,
icon: 'delete-bin-7-line',
require_confirmation: true
},
]
end
Expand All @@ -59,13 +60,13 @@ def columns
{
header: :name,
data: ->(payment_method) do
content_tag :div, payment_method.name
link_to payment_method.name, edit_path(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, edit_path(payment_method), class: "body-link"
end
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%= page id: :payment_method_form 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 %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class SolidusAdmin::PaymentMethods::New::Component < SolidusAdmin::Resources::New::Component
include SolidusAdmin::Layout::PageHelpers
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
en:
back: "Back"
discard: "Discard"
save: "Save"
title: "New Payment Method"
28 changes: 11 additions & 17 deletions admin/app/controllers/solidus_admin/payment_methods_controller.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -11,26 +10,21 @@ 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 resources_collection = resource_class.all

def destroy
@payment_methods = Spree::PaymentMethod.where(id: params[:id])
def resources_sorting_options = { position: :asc }

Spree::PaymentMethod.transaction { @payment_methods.destroy_all }
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

flash[:notice] = t('.success')
redirect_back_or_to payment_methods_path, status: :see_other
def resource_form_container
:payment_method_form
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions admin/app/helpers/solidus_admin/solidus_form_helper.rb
Original file line number Diff line number Diff line change
@@ -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
58 changes: 58 additions & 0 deletions admin/app/lib/solidus_admin/form_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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)

Check warning on line 13 in admin/app/lib/solidus_admin/form_builder.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/lib/solidus_admin/form_builder.rb#L13

Added line #L13 was not covered by tests
end

def select(method, choices, **options)
render component("ui/forms/field").select(self, method, choices, **options)
end

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 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)
render component("ui/checkbox_row").new(form: self, method:, options:, row_title:, **attrs)

Check warning on line 34 in admin/app/lib/solidus_admin/form_builder.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/lib/solidus_admin/form_builder.rb#L34

Added line #L34 was not covered by tests
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)

Check warning on line 40 in admin/app/lib/solidus_admin/form_builder.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/lib/solidus_admin/form_builder.rb#L38-L40

Added lines #L38 - L40 were not covered by tests
end

def hidden_field(method, **options)
input(method, type: :hidden, autocomplete: "off", **options)

Check warning on line 44 in admin/app/lib/solidus_admin/form_builder.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/lib/solidus_admin/form_builder.rb#L44

Added line #L44 was not covered by tests
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)

Check warning on line 56 in admin/app/lib/solidus_admin/form_builder.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/lib/solidus_admin/form_builder.rb#L56

Added line #L56 was not covered by tests
end
end
6 changes: 5 additions & 1 deletion admin/config/locales/payment_methods.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
2 changes: 1 addition & 1 deletion admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 5 additions & 0 deletions admin/lib/solidus_admin/testing_support/feature_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading