From ddb140c7da58fc13a1875b9d18b28906dadf8716 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Sat, 21 Dec 2024 17:02:07 +0100 Subject: [PATCH] Move line_item_comparison_hooks config to Spree::Config Setting line item comparison hooks in a `config.to_prepare` block will currently autoload the Spree::Order model (and subsequently any model referenced in that class definition), slowing down the boot process in development mode (when we want our app/ directory to be autoloaded. This moves setting those hooks to Spree::Config, which is already loaded by the time initializers run. This allows us to move code out of `config.to_prepare` hooks, and stop the order class from being inadvertently eager loaded. --- core/app/models/spree/order.rb | 12 ++++++++++-- core/lib/spree/app_configuration.rb | 7 +++++++ core/spec/models/spree/order_merger_spec.rb | 7 +------ core/spec/models/spree/order_spec.rb | 7 +------ promotions/lib/solidus_promotions/engine.rb | 5 +---- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb index 97cee9f75c3..a2ebb0e2845 100644 --- a/core/app/models/spree/order.rb +++ b/core/app/models/spree/order.rb @@ -163,8 +163,16 @@ def self.find_by_param!(value) delegate :name, to: :bill_address, prefix: true, allow_nil: true alias_method :billing_name, :bill_address_name - class_attribute :line_item_comparison_hooks - self.line_item_comparison_hooks = Set.new + def line_item_comparison_hooks + Set.new(Spree::Config.line_item_comparison_hooks) + end + + class << self + def line_item_comparison_hooks=(value) + Spree::Config.line_item_comparison_hooks = value.to_a + end + deprecate :line_item_comparison_hooks=, deprecator: Spree.deprecator + end scope :created_between, ->(start_date, end_date) { where(created_at: start_date..end_date) } scope :completed_between, ->(start_date, end_date) { where(completed_at: start_date..end_date) } diff --git a/core/lib/spree/app_configuration.rb b/core/lib/spree/app_configuration.rb index 631a609d18d..6e0379487fe 100644 --- a/core/lib/spree/app_configuration.rb +++ b/core/lib/spree/app_configuration.rb @@ -168,6 +168,13 @@ class AppConfiguration < Preferences::Configuration # @return [String] template to use for layout on the frontend (default: +"spree/layouts/spree_application"+) preference :layout, :string, default: 'spree/layouts/spree_application' + # !@attribute [rw] line_item_comparison_hooks + # @return [Array] An array of methods to call on {Spree::Order} to determine if a line item is equal to another + # (default: +[]+) + # @example + # config.line_item_comparison_hooks << :my_custom_method + preference :line_item_comparison_hooks, :array, default: [] + # @!attribute [rw] logo # @return [String] URL of logo used on frontend (default: +'logo/solidus.svg'+) preference :logo, :string, default: 'logo/solidus.svg' diff --git a/core/spec/models/spree/order_merger_spec.rb b/core/spec/models/spree/order_merger_spec.rb index 80841109232..71a3a51a65d 100644 --- a/core/spec/models/spree/order_merger_spec.rb +++ b/core/spec/models/spree/order_merger_spec.rb @@ -67,12 +67,7 @@ module Spree context "merging using extension-specific line_item_comparison_hooks" do before do - Spree::Order.register_line_item_comparison_hook(:foos_match) - end - - after do - # reset to avoid test pollution - Spree::Order.line_item_comparison_hooks = Set.new + stub_spree_preferences(line_item_comparison_hooks: [:foos_match]) end context "2 equal line items" do diff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rb index 86c75f11ec3..682e406fd96 100644 --- a/core/spec/models/spree/order_spec.rb +++ b/core/spec/models/spree/order_spec.rb @@ -741,12 +741,7 @@ def merge!(other_order, user = nil) context "match line item with options", partial_double_verification: false do before do - Spree::Order.register_line_item_comparison_hook(:foos_match) - end - - after do - # reset to avoid test pollution - Spree::Order.line_item_comparison_hooks = Set.new + stub_spree_preferences(line_item_comparison_hooks: [:foos_match]) end it "matches line item when options match" do diff --git a/promotions/lib/solidus_promotions/engine.rb b/promotions/lib/solidus_promotions/engine.rb index 29df050ba6d..a7a395ed596 100644 --- a/promotions/lib/solidus_promotions/engine.rb +++ b/promotions/lib/solidus_promotions/engine.rb @@ -40,10 +40,7 @@ class Engine < Rails::Engine initializer "solidus_promotions.spree_config", after: "spree.load_config_initializers" do Spree::Config.adjustment_promotion_source_types << "SolidusPromotions::Benefit" - - Rails.application.config.to_prepare do - Spree::Order.line_item_comparison_hooks << :free_from_order_benefit? - end + Spree::Config.line_item_comparison_hooks << :free_from_order_benefit? end initializer "solidus_promotions.core.pub_sub", after: "spree.core.pub_sub" do |app|