Skip to content

Commit 7205ff0

Browse files
authored
Merge pull request solidusio#6050 from mamhoff/configure-line-item-comparison-hooks-in-config
Move line_item_comparison_hooks config to Spree::Config
2 parents a25b831 + 81bf6f6 commit 7205ff0

File tree

6 files changed

+73
-26
lines changed

6 files changed

+73
-26
lines changed

core/app/models/spree/order.rb

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,24 @@ def self.find_by_param!(value)
163163
delegate :name, to: :bill_address, prefix: true, allow_nil: true
164164
alias_method :billing_name, :bill_address_name
165165

166-
class_attribute :line_item_comparison_hooks
167-
self.line_item_comparison_hooks = Set.new
166+
delegate :line_item_comparison_hooks, to: :class
167+
class << self
168+
def line_item_comparison_hooks=(value)
169+
Spree::Config.line_item_comparison_hooks = value.to_a
170+
end
171+
line_item_hooks_deprecation_msg = "Use Spree::Config.line_item_comparison_hooks instead."
172+
deprecate :line_item_comparison_hooks= => line_item_hooks_deprecation_msg, :deprecator => Spree.deprecator
173+
174+
def line_item_comparison_hooks
175+
Spree::Config.line_item_comparison_hooks
176+
end
177+
deprecate line_item_comparison_hooks: line_item_hooks_deprecation_msg, deprecator: Spree.deprecator
178+
179+
def register_line_item_comparison_hook(hook)
180+
Spree::Config.line_item_comparison_hooks << hook
181+
end
182+
deprecate register_line_item_comparison_hook: line_item_hooks_deprecation_msg, deprecator: Spree.deprecator
183+
end
168184

169185
scope :created_between, ->(start_date, end_date) { where(created_at: start_date..end_date) }
170186
scope :completed_between, ->(start_date, end_date) { where(completed_at: start_date..end_date) }
@@ -198,12 +214,6 @@ def self.not_canceled
198214
where.not(state: 'canceled')
199215
end
200216

201-
# Use this method in other gems that wish to register their own custom logic
202-
# that should be called when determining if two line items are equal.
203-
def self.register_line_item_comparison_hook(hook)
204-
line_item_comparison_hooks.add(hook)
205-
end
206-
207217
# For compatiblity with Calculator::PriceSack
208218
def amount
209219
line_items.sum(&:amount)
@@ -356,7 +366,7 @@ def find_line_item_by_variant(variant, options = {})
356366
def line_item_options_match(line_item, options)
357367
return true unless options
358368

359-
line_item_comparison_hooks.all? { |hook|
369+
Spree::Config.line_item_comparison_hooks.all? { |hook|
360370
send(hook, line_item, options)
361371
}
362372
end

core/app/models/spree/order_merger.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def merge!(other_order, user = nil)
7878
def find_matching_line_item(other_order_line_item)
7979
order.line_items.detect do |my_li|
8080
my_li.variant == other_order_line_item.variant &&
81-
order.line_item_comparison_hooks.all? do |hook|
81+
Spree::Config.line_item_comparison_hooks.all? do |hook|
8282
order.send(hook, my_li, other_order_line_item.serializable_hash)
8383
end
8484
end

core/lib/spree/app_configuration.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ class AppConfiguration < Preferences::Configuration
168168
# @return [String] template to use for layout on the frontend (default: +"spree/layouts/spree_application"+)
169169
preference :layout, :string, default: 'spree/layouts/spree_application'
170170

171+
# !@attribute [rw] line_item_comparison_hooks
172+
# @return [Array<Symbol>] An array of methods to call on {Spree::Order} to determine if a line item is equal to another
173+
# (default: +[]+)
174+
# @example
175+
# config.line_item_comparison_hooks << :my_custom_method
176+
preference :line_item_comparison_hooks, :array, default: []
177+
171178
# @!attribute [rw] logo
172179
# @return [String] URL of logo used on frontend (default: +'logo/solidus.svg'+)
173180
preference :logo, :string, default: 'logo/solidus.svg'

core/spec/models/spree/order_merger_spec.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,7 @@ module Spree
6767

6868
context "merging using extension-specific line_item_comparison_hooks" do
6969
before do
70-
Spree::Order.register_line_item_comparison_hook(:foos_match)
71-
end
72-
73-
after do
74-
# reset to avoid test pollution
75-
Spree::Order.line_item_comparison_hooks = Set.new
70+
stub_spree_preferences(line_item_comparison_hooks: [:foos_match])
7671
end
7772

7873
context "2 equal line items" do

core/spec/models/spree/order_spec.rb

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,49 @@
1313
it { is_expected.to contain_exactly("user", "line_items", "shipments", "bill_address", "ship_address") }
1414
end
1515

16+
describe ".line_item_comparison_hooks=" do
17+
it "allows setting the line item comparison hooks but emits a deprecation message" do
18+
expect(Spree::Config).to receive(:line_item_comparison_hooks=).with([:foos_match])
19+
expect(Spree.deprecator).to receive(:warn)
20+
.with(
21+
"line_item_comparison_hooks= is deprecated and will be removed from Solidus 5.0 (Use Spree::Config.line_item_comparison_hooks instead.)",
22+
an_instance_of(Array)
23+
)
24+
described_class.line_item_comparison_hooks = [:foos_match]
25+
end
26+
end
27+
28+
describe ".line_item_comparison_hooks" do
29+
before do |example|
30+
stub_spree_preferences(line_item_comparison_hooks: [:foos_match])
31+
end
32+
33+
it "allows getting the comparison hooks but emits a deprecation message" do
34+
expect(Spree.deprecator).to receive(:warn)
35+
.with(
36+
"line_item_comparison_hooks is deprecated and will be removed from Solidus 5.0 (Use Spree::Config.line_item_comparison_hooks instead.)",
37+
an_instance_of(Array)
38+
)
39+
described_class.line_item_comparison_hooks
40+
end
41+
end
42+
43+
describe ".register_line_item_comparison_hook" do
44+
after do
45+
expect(Spree::Config.line_item_comparison_hooks).to be_empty
46+
end
47+
it "allows setting the line item comparison hooks but emits a deprecation message" do
48+
expect(Spree::Config.line_item_comparison_hooks).to receive(:<<).with(:foos_match)
49+
50+
expect(Spree.deprecator).to receive(:warn)
51+
.with(
52+
"register_line_item_comparison_hook is deprecated and will be removed from Solidus 5.0 (Use Spree::Config.line_item_comparison_hooks instead.)",
53+
an_instance_of(Array)
54+
)
55+
Spree::Order.register_line_item_comparison_hook(:foos_match)
56+
end
57+
end
58+
1659
context '#store' do
1760
it { is_expected.to respond_to(:store) }
1861

@@ -741,12 +784,7 @@ def merge!(other_order, user = nil)
741784

742785
context "match line item with options", partial_double_verification: false do
743786
before do
744-
Spree::Order.register_line_item_comparison_hook(:foos_match)
745-
end
746-
747-
after do
748-
# reset to avoid test pollution
749-
Spree::Order.line_item_comparison_hooks = Set.new
787+
stub_spree_preferences(line_item_comparison_hooks: [:foos_match])
750788
end
751789

752790
it "matches line item when options match" do

promotions/lib/solidus_promotions/engine.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ class Engine < Rails::Engine
4040

4141
initializer "solidus_promotions.spree_config", after: "spree.load_config_initializers" do
4242
Spree::Config.adjustment_promotion_source_types << "SolidusPromotions::Benefit"
43-
44-
Rails.application.config.to_prepare do
45-
Spree::Order.line_item_comparison_hooks << :free_from_order_benefit?
46-
end
43+
Spree::Config.line_item_comparison_hooks << :free_from_order_benefit?
4744
end
4845

4946
initializer "solidus_promotions.core.pub_sub", after: "spree.core.pub_sub" do |app|

0 commit comments

Comments
 (0)