From 41ce4fbc16fa99983b1319a93c9aa6a99f368ddf Mon Sep 17 00:00:00 2001 From: James Wu Date: Thu, 26 Jan 2023 15:01:27 +0900 Subject: [PATCH] Fix cache issue with invalid form data --- .../spree/admin/payment_methods_controller.rb | 7 +++++++ spec/system/admin/payment_method_spec.rb | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/app/controllers/spree/admin/payment_methods_controller.rb b/app/controllers/spree/admin/payment_methods_controller.rb index 6d43556718..a6114266de 100644 --- a/app/controllers/spree/admin/payment_methods_controller.rb +++ b/app/controllers/spree/admin/payment_methods_controller.rb @@ -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 diff --git a/spec/system/admin/payment_method_spec.rb b/spec/system/admin/payment_method_spec.rb index 6673273835..c4343619bb 100644 --- a/spec/system/admin/payment_method_spec.rb +++ b/spec/system/admin/payment_method_spec.rb @@ -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