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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Move all configuration from DB to runtime (#12165)
Damian Legawiec19102bf1d3d5
diff --git a/core/app/models/spree/price.rb b/core/app/models/spree/price.rbindex 45d2c549e8..00d65c65d5 100644--- a/core/app/models/spree/price.rb+++ b/core/app/models/spree/price.rb@@ -18,13 +18,13 @@ module Spreevalidates :amount, allow_nil: true, numericality: {greater_than_or_equal_to: 0,less_than_or_equal_to: MAXIMUM_AMOUNT- }, if: -> { Spree::RuntimeConfig.allow_empty_price_amount }+ }, if: -> { Spree::Config.allow_empty_price_amount }# new behaviorvalidates :amount, allow_nil: false, numericality: {greater_than_or_equal_to: 0,less_than_or_equal_to: MAXIMUM_AMOUNT- }, unless: -> { Spree::RuntimeConfig.allow_empty_price_amount }+ }, unless: -> { Spree::Config.allow_empty_price_amount }validates :compare_at_amount, allow_nil: true, numericality: {greater_than_or_equal_to: 0,diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex fd09bfd1a2..29dc4620ef 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -455,7 +455,7 @@ module Spreeenddef disable_sku_validation?- Spree::RuntimeConfig[:disable_sku_validation]+ Spree::Config[:disable_sku_validation]endendenddiff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rbindex af41674e48..9a14cfb9b7 100644--- a/core/lib/spree/core.rb+++ b/core/lib/spree/core.rb@@ -78,11 +78,11 @@ module Spreeenddef self.always_use_translations?- Spree::RuntimeConfig.always_use_translations+ Spree::Config.always_use_translationsenddef self.use_translations?- Spree::RuntimeConfig.always_use_translations || I18n.default_locale != I18n.locale+ Spree::Config.always_use_translations || I18n.default_locale != I18n.localeend# Used to configure Spree.diff --git a/core/lib/spree/core/configuration.rb b/core/lib/spree/core/configuration.rbindex c7915c867c..0f3b48f8e0 100644--- a/core/lib/spree/core/configuration.rb+++ b/core/lib/spree/core/configuration.rb@@ -16,19 +16,23 @@# a.preferred_color#require 'spree/core/search/base'-require 'spree/core/preferences/configuration'+require 'spree/core/preferences/configuration' # for compatibility reasons+require 'spree/core/preferences/runtime_configuration'+require 'spree/core/preferences/preferable'module Spreemodule Core- class Configuration < Preferences::Configuration+ class Configuration < Preferences::RuntimeConfiguration# Alphabetized to more easily lookup particular preferencespreference :address_requires_state, :boolean, default: true, deprecated: true # should state/state_name be requiredpreference :address_requires_phone, :boolean, default: true # Determines whether we require phone in addresspreference :allow_checkout_on_gateway_error, :boolean, default: false+ preference :allow_empty_price_amount, :boolean, default: falsepreference :allow_guest_checkout, :boolean, default: true # this is only used in the rails frontend, and is not implemented in APIpreference :alternative_shipping_phone, :boolean, default: false # Request extra phone for ship addrpreference :always_include_confirm_step, :boolean, default: false # Ensures confirmation step is always in checkout_progress bar, but does not force a confirm step if your payment methods do not support it.preference :always_put_site_name_in_title, :boolean, deprecated: true+ preference :always_use_translations, :boolean, default: falsepreference :auto_capture, :boolean, default: false # automatically capture the credit card (as opposed to just authorize and capture later)preference :auto_capture_on_dispatch, :boolean, default: false # Captures payment for each shipment in Shipment#after_ship callback, and makes Shipment.ready when payment authorized.preference :binary_inventory_cache, :boolean, default: false, deprecated: true # only invalidate product cache when a stock item changes whether it is in_stock@@ -36,7 +40,7 @@ module Spreepreference :company, :boolean, default: false # Request company field for billing and shipping addrpreference :currency, :string, default: 'USD', deprecated: truepreference :credit_to_new_allocation, :boolean, default: false- preference :disable_sku_validation, :boolean, default: false, deprecated: true # when turned off disables the built-in SKU uniqueness validation+ preference :disable_sku_validation, :boolean, default: false # when turned off disables the built-in SKU uniqueness validationpreference :disable_store_presence_validation, :boolean, default: false # when turned off disables Store presence validation for Products and Payment Methodspreference :expedited_exchanges, :boolean, default: false # NOTE this requires payment profiles to be supported on your gateway of choice as well as a delayed job handler to be configured with activejob. kicks off an exchange shipment upon return authorization save. charge customer if they do not return items within timely manner.preference :expedited_exchanges_days_window, :integer, default: 14 # the amount of days the customer has to return their item after the expedited exchange is shipped in order to avoid being chargeddiff --git a/core/lib/spree/core/engine.rb b/core/lib/spree/core/engine.rbindex a993b7289e..45e4442bcc 100644--- a/core/lib/spree/core/engine.rb+++ b/core/lib/spree/core/engine.rb@@ -1,13 +1,11 @@require_relative 'dependencies'require_relative 'configuration'-require_relative 'runtime_configuration'module Spreemodule Coreclass Engine < ::Rails::EngineEnvironment = Struct.new(:calculators,:preferences,- :runtime_preferences,:dependencies,:payment_methods,:adjusters,@@ -25,11 +23,11 @@ module Spreeendinitializer 'spree.environment', before: :load_config_initializers do |app|- app.config.spree = Environment.new(SpreeCalculators.new, Spree::Core::Configuration.new, Spree::Core::RuntimeConfiguration.new, Spree::Core::Dependencies.new)+ app.config.spree = Environment.new(SpreeCalculators.new, Spree::Core::Configuration.new, Spree::Core::Dependencies.new)app.config.active_record.yaml_column_permitted_classes ||= []app.config.active_record.yaml_column_permitted_classes << [Symbol, BigDecimal, ActiveSupport::HashWithIndifferentAccess]Spree::Config = app.config.spree.preferences- Spree::RuntimeConfig = app.config.spree.runtime_preferences+ Spree::Config = app.config.spree.preferences # for compatibilitySpree::Dependencies = app.config.spree.dependenciesSpree::Deprecation = ActiveSupport::Deprecation.newenddiff --git a/core/lib/spree/core/preferences/configuration.rb b/core/lib/spree/core/preferences/configuration.rbindex 3bd90f8167..9e81f8c6a9 100644--- a/core/lib/spree/core/preferences/configuration.rb+++ b/core/lib/spree/core/preferences/configuration.rb@@ -21,51 +21,9 @@##-require 'spree/core/preferences/preferable'+require_relative 'runtime_configuration'module Spree::Preferences- class Configuration- include Spree::Preferences::Preferable-- def configure- yield(self) if block_given?- end-- def preferences- ::Spree::Preferences::ScopedStore.new(self.class.name.underscore)- end-- def reset- preferences.each do |name, _value|- set_preference name, preference_default(name)- end- end-- alias [] get_preference- alias []= set_preference-- alias get get_preference-- def set(*args)- options = args.extract_options!- options.each do |name, value|- set_preference name, value- end-- set_preference args[0], args[1] if args.size == 2- end-- def method_missing(method, *args)- name = method.to_s.delete('=')- if has_preference? name- if method.to_s =~ /=$/- set_preference(name, args.first)- else- get_preference name- end- else- super- end- end+ class Configuration < Spree::Preferences::RuntimeConfigurationendenddiff --git a/core/lib/spree/core/preferences/runtime_configuration.rb b/core/lib/spree/core/preferences/runtime_configuration.rbindex 499b5afe1d..f6e40001b1 100644--- a/core/lib/spree/core/preferences/runtime_configuration.rb+++ b/core/lib/spree/core/preferences/runtime_configuration.rb@@ -7,6 +7,12 @@ module Spreeendend+ def reset+ self.class.defaults.each do |key, value|+ self[key] = value+ end+ end+def configureyield(self) if block_given?end@@ -17,8 +23,13 @@ module Spreealias [] get- def set(preference, value)- send("#{preference}=", value)+ def set(*args)+ options = args.extract_options!+ options.each do |name, value|+ send("#{name}=", value)+ end++ send("#{args[0]}=", args[1]) if args.size == 2endalias []= setdiff --git a/core/lib/spree/core/runtime_configuration.rb b/core/lib/spree/core/runtime_configuration.rbdeleted file mode 100644index 4ba23b6c5f..0000000000--- a/core/lib/spree/core/runtime_configuration.rb+++ /dev/null@@ -1,11 +0,0 @@-require 'spree/core/preferences/runtime_configuration'--module Spree- module Core- class RuntimeConfiguration < Preferences::RuntimeConfiguration- preference :always_use_translations, :boolean, default: false- preference :allow_empty_price_amount, :boolean, default: false- preference :disable_sku_validation, :boolean, default: false- end- end-enddiff --git a/core/lib/spree/testing_support/preferences.rb b/core/lib/spree/testing_support/preferences.rbindex bf639bc56b..c3470ef88e 100644--- a/core/lib/spree/testing_support/preferences.rb+++ b/core/lib/spree/testing_support/preferences.rb@@ -12,7 +12,7 @@ module SpreeSpree::Preferences::Store.instance.persistence = falseSpree::Preferences::Store.instance.clear_cache- config = Rails.application.config.spree.preferences+ config = Rails.application.config.spree.preferences.resetconfigure_spree_preferences &config_block if block_given?enddiff --git a/core/spec/lib/spree/core/controller_helpers/locale_spec.rb b/core/spec/lib/spree/core/controller_helpers/locale_spec.rbindex 7afdde5128..fe108f11a4 100644--- a/core/spec/lib/spree/core/controller_helpers/locale_spec.rb+++ b/core/spec/lib/spree/core/controller_helpers/locale_spec.rb@@ -135,7 +135,7 @@ describe Spree::Core::ControllerHelpers::Locale, type: :controller docontext 'when always using translations' dobefore do- allow(Spree::RuntimeConfig).to receive(:always_use_translations).and_return(true)+ allow(Spree::Config).to receive(:always_use_translations).and_return(true)endit 'sets only the current locale' dodiff --git a/core/spec/models/spree/order/currency_updater_spec.rb b/core/spec/models/spree/order/currency_updater_spec.rbindex d06306c324..1c01922ba2 100644--- a/core/spec/models/spree/order/currency_updater_spec.rb+++ b/core/spec/models/spree/order/currency_updater_spec.rb@@ -25,7 +25,7 @@ describe Spree::Order, type: :model docontext 'when there is a price with nil amount' dolet!(:euro_price) do- allow(Spree::RuntimeConfig).to receive(:allow_empty_price_amount).and_return(true)+ allow(Spree::Config).to receive(:allow_empty_price_amount).and_return(true)create(:price, variant: line_item.variant, amount: nil, currency: 'EUR')enddiff --git a/core/spec/models/spree/price_spec.rb b/core/spec/models/spree/price_spec.rbindex cad8ea5068..77c6e00bd2 100644--- a/core/spec/models/spree/price_spec.rb+++ b/core/spec/models/spree/price_spec.rb@@ -88,7 +88,7 @@ describe Spree::Price, type: :model docontext 'legacy behavior' dobefore do- allow(Spree::RuntimeConfig).to receive(:allow_empty_price_amount).and_return(true)+ allow(Spree::Config).to receive(:allow_empty_price_amount).and_return(true)endit { is_expected.to be_valid }diff --git a/core/spec/models/spree/taxon_spec.rb b/core/spec/models/spree/taxon_spec.rbindex c8d290f6b3..8541bb7537 100644--- a/core/spec/models/spree/taxon_spec.rb+++ b/core/spec/models/spree/taxon_spec.rb@@ -329,7 +329,7 @@ describe Spree::Taxon, type: :model docontext 'when `always_use_translations` is disabled' dobefore do- allow(Spree::RuntimeConfig).to receive(:always_use_translations).and_return(false)+ allow(Spree::Config).to receive(:always_use_translations).and_return(false)endit 'sets the pretty name' do@@ -339,7 +339,7 @@ describe Spree::Taxon, type: :model docontext 'when `always_use_translations` is enabled' dobefore do- allow(Spree::RuntimeConfig).to receive(:always_use_translations).and_return(true)+ allow(Spree::Config).to receive(:always_use_translations).and_return(true)endit 'sets the pretty name' dodiff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rbindex 1470a9da60..a3d1a2d45e 100644--- a/core/spec/models/spree/variant_spec.rb+++ b/core/spec/models/spree/variant_spec.rb@@ -51,11 +51,11 @@ describe Spree::Variant, type: :model docontext 'disabled validation' dobefore do- Spree::RuntimeConfig[:disable_sku_validation] = true+ Spree::Config[:disable_sku_validation] = trueendafter do- Spree::RuntimeConfig[:disable_sku_validation] = false+ Spree::Config[:disable_sku_validation] = falseendcontext 'valid' do@@ -177,7 +177,7 @@ describe Spree::Variant, type: :model docontext 'when price do not have amount' dobefore do- allow(Spree::RuntimeConfig).to receive(:allow_empty_price_amount).and_return(true)+ allow(Spree::Config).to receive(:allow_empty_price_amount).and_return(true)endlet!(:price_1) { create(:price, currency: currency, variant: variant, amount: nil) }@@ -197,7 +197,7 @@ describe Spree::Variant, type: :model docontext 'when price do not have amount' dobefore do- allow(Spree::RuntimeConfig).to receive(:allow_empty_price_amount).and_return(true)+ allow(Spree::Config).to receive(:allow_empty_price_amount).and_return(true)endlet!(:price_1) { create(:price, currency: unavailable_currency, variant: variant, amount: nil) }diff --git a/core/spec/services/spree/cart/add_item_spec.rb b/core/spec/services/spree/cart/add_item_spec.rbindex 2c17204957..0804f368fe 100644--- a/core/spec/services/spree/cart/add_item_spec.rb+++ b/core/spec/services/spree/cart/add_item_spec.rb@@ -236,7 +236,7 @@ module Spreelet(:call_service) { subject.call(order: order, variant: variant, quantity: 1) }before do- allow(Spree::RuntimeConfig).to receive(:allow_empty_price_amount).and_return(true)+ allow(Spree::Config).to receive(:allow_empty_price_amount).and_return(true)variant.prices.first.update(amount: nil)end