Skip to content

Commit

Permalink
Move line_item_comparison_hooks config to Spree::Config
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mamhoff committed Jan 3, 2025
1 parent c5772f5 commit cd8ae9f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
12 changes: 10 additions & 2 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
7 changes: 7 additions & 0 deletions core/lib/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<Symbol>] 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'
Expand Down
7 changes: 1 addition & 6 deletions core/spec/models/spree/order_merger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions core/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions promotions/lib/solidus_promotions/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,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|
Expand Down

0 comments on commit cd8ae9f

Please sign in to comment.