From b44430172c9639ba8142411b60dc0721c0443090 Mon Sep 17 00:00:00 2001 From: Alberto Vena Date: Thu, 18 Jul 2019 11:14:07 +0200 Subject: [PATCH 1/2] Do not reset spree preferences starting from 2.9 --- lib/solidus_support.rb | 5 +++++ lib/solidus_support/extension/rails_helper.rb | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/solidus_support.rb b/lib/solidus_support.rb index 26fcfa4..f81992a 100644 --- a/lib/solidus_support.rb +++ b/lib/solidus_support.rb @@ -16,6 +16,11 @@ def solidus_gem_version end end + def reset_spree_preferences_deprecated? + first_version_without_reset = Gem::Requirement.new('>= 2.9') + first_version_without_reset.satisfied_by?(solidus_gem_version) + end + def new_gateway_code? first_version_with_new_gateway_code = Gem::Requirement.new('>= 2.3') first_version_with_new_gateway_code.satisfied_by?(solidus_gem_version) diff --git a/lib/solidus_support/extension/rails_helper.rb b/lib/solidus_support/extension/rails_helper.rb index 9681a03..a4f482a 100644 --- a/lib/solidus_support/extension/rails_helper.rb +++ b/lib/solidus_support/extension/rails_helper.rb @@ -35,7 +35,7 @@ DatabaseCleaner.strategy = RSpec.current_example.metadata[:js] ? :truncation : :transaction DatabaseCleaner.cleaning do - reset_spree_preferences + reset_spree_preferences unless SolidusSupport.reset_spree_preferences_deprecated? example.run end From 097e8fb024c56c69e62bd1cce344e7dbc546f7ca Mon Sep 17 00:00:00 2001 From: Alberto Vena Date: Thu, 18 Jul 2019 12:52:46 +0200 Subject: [PATCH 2/2] Provide a method to safely set preferences in specs Using this method in extensions (and stores) allows to set preferences by stubbing them as suggested in core. Using this helper guarantee to have it working also in past Solidus versions, where stub_spree_preferences method does not exist yet. --- lib/solidus_support/extension/rails_helper.rb | 2 + .../testing_support/preferences.rb | 45 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 lib/solidus_support/testing_support/preferences.rb diff --git a/lib/solidus_support/extension/rails_helper.rb b/lib/solidus_support/extension/rails_helper.rb index a4f482a..7bcb22d 100644 --- a/lib/solidus_support/extension/rails_helper.rb +++ b/lib/solidus_support/extension/rails_helper.rb @@ -15,6 +15,7 @@ require 'spree/testing_support/factories' require 'spree/testing_support/url_helpers' require 'spree/testing_support/preferences' +require 'solidus_support/testing_support/preferences' RSpec.configure do |config| config.include FactoryBot::Syntax::Methods @@ -24,6 +25,7 @@ config.include Spree::TestingSupport::UrlHelpers config.include Spree::TestingSupport::Preferences + config.include SolidusSupport::TestingSupport::Preferences config.before :suite do DatabaseCleaner.clean_with :truncation diff --git a/lib/solidus_support/testing_support/preferences.rb b/lib/solidus_support/testing_support/preferences.rb new file mode 100644 index 0000000..3b515e1 --- /dev/null +++ b/lib/solidus_support/testing_support/preferences.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +module SolidusSupport + module TestingSupport + module Preferences + # This wrapper method allows to stub spree preferences using + # the new standard way of solidus core but also works with + # old versions that does not have the stub_spree_preferences + # method yet. This way we can start using this method in our + # extensions safely. + # + # To have this available, it is needed to require in the + # spec/spec_helper.rb of the extension both: + # + # require 'spree/testing_support/preferences' + # require 'solidus_support/testing_support/preferences' + # + # @example Set a preference on Spree::Config + # stub_spree_preferences(allow_guest_checkout: false) + # + # @example Set a preference on Spree::Api::Config + # stub_spree_preferences(Spree::Api::Config, requires_authentication: false) + # + # @example Set a preference on a custom Spree::CustomExtension::Config + # stub_spree_preferences(Spree::CustomExtension::Config, custom_pref: true) + # + # @param prefs_or_conf_class [Class, Hash] the class we want to stub + # preferences for or the preferences hash (see prefs param). If this + # param is an Hash, preferences will be stubbed on Spree::Config. + # @param prefs [Hash, nil] names and values to be stubbed + def stub_spree_preferences(prefs_or_conf_class, prefs = nil) + super && return if SolidusSupport.reset_spree_preferences_deprecated? + + if prefs_or_conf_class.is_a?(Hash) + preference_store_class = Spree::Config + preferences = prefs_or_conf_class + else + preference_store_class = prefs_or_conf_class + preferences = prefs + end + preference_store_class.set(preferences) + end + end + end +end