Fix cache issue with invalid form data

This commit is contained in:
James Wu
2023-01-26 15:01:27 +09:00
committed by David Cook
parent 48753df5f0
commit 41ce4fbc16
2 changed files with 24 additions and 0 deletions

View File

@@ -43,6 +43,7 @@ module Spree
redirect_to spree.edit_admin_payment_method_path(@payment_method)
else
respond_with(@payment_method)
clear_preference_cache
end
end
@@ -184,6 +185,12 @@ module Spree
params_for_update
end
end
def clear_preference_cache
@payment_method.calculator.preferences.each_key do |key|
Rails.cache.delete(@payment_method.calculator.preference_cache_key(key))
end
end
end
end
end

View File

@@ -349,7 +349,10 @@ describe '
context "when updating a payment method with invalid data" do
let(:payment_method) { create(:payment_method, :flat_rate, amount: 10) }
# Persist preferences to test that when preferred values are not found in the cache,
# they are fetched from the database and displayed correctly
before do
Spree::Preferences::Store.instance.persistence = true
login_as_admin_and_visit spree.edit_admin_payment_method_path payment_method
fill_in "payment_method_name", with: ""
fill_in "payment_method_description", with: "Edited description"
@@ -358,6 +361,10 @@ describe '
click_button 'Update'
end
after do
Spree::Preferences::Store.instance.persistence = false
end
it "displays the number of errors" do
expect(page).to have_content("3 errors")
end
@@ -397,5 +404,15 @@ describe '
expect(page).to have_field "payment_method_description", with: "Edited description"
expect(page).to have_unchecked_field "payment_method_distributor_ids_#{@distributors[0].id}"
end
it 'displays data fetched from the database after navigating away from the page' do
click_link 'Back To Payment Methods List'
click_link href: /#{spree.edit_admin_payment_method_path(payment_method)}/
expect(page).to have_field 'Amount', with: '10.0'
expect(page).not_to have_field 'payment_method_name', with: ''
expect(page).not_to have_field 'payment_method_description', with: 'Edited description'
expect(page).not_to have_unchecked_field "payment_method_distributor_ids_#{@distributors[0].id}"
end
end
end