Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Ensure we're always associating Payment Method with the current store

Damian Legawiec65b4c13a6cbf
diff --git a/backend/app/controllers/spree/admin/payment_methods_controller.rb b/backend/app/controllers/spree/admin/payment_methods_controller.rb
index d8f6a4c707..bb679ddcb1 100644
--- a/backend/app/controllers/spree/admin/payment_methods_controller.rb
+++ b/backend/app/controllers/spree/admin/payment_methods_controller.rb
@@ -10,7 +10,7 @@ module Spree
def create
@payment_method = params[:payment_method].delete(:type).constantize.new(payment_method_params)
@object = @payment_method
- set_store
+ ensure_current_store
invoke_callbacks(:create, :before)
if @payment_method.save
invoke_callbacks(:create, :after)
@@ -30,7 +30,7 @@ module Spree
type: payment_method_type,
updated_at: Time.current
)
- @payment_method = PaymentMethod.find(params[:id])
+ @payment_method = scope.find(params[:id])
end
attributes = payment_method_params.merge(preferences_params)
@@ -39,6 +39,7 @@ module Spree
end
if @payment_method.update(attributes)
+ ensure_current_store
invoke_callbacks(:update, :after)
flash[:success] = Spree.t(:successfully_updated, resource: Spree.t(:payment_method))
redirect_to spree.edit_admin_payment_method_path(@payment_method)
diff --git a/backend/app/views/spree/admin/payment_methods/_form.html.erb b/backend/app/views/spree/admin/payment_methods/_form.html.erb
index b60b4c9306..bef9dc34ec 100644
--- a/backend/app/views/spree/admin/payment_methods/_form.html.erb
+++ b/backend/app/views/spree/admin/payment_methods/_form.html.erb
@@ -24,10 +24,12 @@
<%= text_area :payment_method, :description, { cols: 60, rows: 6, class: 'form-control' } %>
<% end %>
- <div data-hook="store" class="form-group">
- <%= label_tag :payment_method_stores, Spree.t(:stores) %>
- <%= collection_select(:payment_method, :store_ids, @stores, :id, :unique_name, {}, { multiple: true, class: 'select2' }) %>
- </div>
+ <% if @payment_method.persisted? %>
+ <div data-hook="store" class="form-group">
+ <%= label_tag :payment_method_stores, Spree.t(:stores) %>
+ <%= collection_select(:payment_method, :store_ids, @stores, :id, :unique_name, {}, { multiple: true, class: 'select2' }) %>
+ </div>
+ <% end %>
<div data-hook="display" class="form-group">
<%= label_tag :payment_method_display_on, Spree.t(:display) %>
diff --git a/backend/spec/features/admin/configuration/payment_methods_spec.rb b/backend/spec/features/admin/configuration/payment_methods_spec.rb
index 14c410847d..c292914c0b 100644
--- a/backend/spec/features/admin/configuration/payment_methods_spec.rb
+++ b/backend/spec/features/admin/configuration/payment_methods_spec.rb
@@ -66,43 +66,39 @@ describe 'Payment Methods', type: :feature do
expect(page).to have_select('payment_method_store_ids', selected: store.unique_name)
end
-
- it 'is able to create a new payment method with 2 stores' do
- within find('#contentHeader') do
- click_link 'admin_new_payment_methods_link'
- end
-
- expect(page).to have_content('New Payment Method')
- fill_in 'payment_method_name', with: 'check90'
- fill_in 'payment_method_description', with: 'check90 desc'
- select 'PaymentMethod::Check', from: 'gtwy-type'
-
- select2 store_1.unique_name, from: 'Stores'
- select2 store_3.unique_name, from: 'Stores'
-
- click_button 'Create'
- expect(page).to have_content('successfully created!')
- expect(page).to have_selector('li.select2-selection__choice', text: store_1.unique_name)
- expect(page).not_to have_selector('li.select2-selection__choice', text: store_2.unique_name)
- expect(page).to have_selector('li.select2-selection__choice', text: store_3.unique_name)
- end
end
context 'admin editing a payment method', js: true do
before do
- create(:check_payment_method)
- refresh
-
- within('table#listing_payment_methods') do
- click_icon(:edit)
- end
+ visit spree.edit_admin_payment_method_path(check_payment_method)
end
+ let!(:check_payment_method) { create(:check_payment_method, name: 'Check') }
+
it 'is able to edit an existing payment method' do
fill_in 'payment_method_name', with: 'Payment 99'
click_button 'Update'
expect(page).to have_content('successfully updated!')
expect(page).to have_field('payment_method_name', with: 'Payment 99')
+
+ expect(check_payment_method.reload.stores).to contain_exactly(store)
+ end
+
+ it 'is able to associate payment method with 2 stores' do
+ expect(page).to have_content('Check')
+
+ select2_open label: 'Stores'
+ select2_search store_1.unique_name, from: 'Stores'
+ select2_select store_1.unique_name, from: 'Stores', match: :first
+
+ select2_open label: 'Stores'
+ select2_search store_3.unique_name, from: 'Stores'
+ select2_select store_3.unique_name, from: 'Stores', match: :first
+
+ click_button 'Update'
+ expect(page).to have_content('successfully updated!')
+
+ expect(check_payment_method.reload.stores).to contain_exactly(store, store_1, store_3)
end
it 'displays validation errors' do