Skip to content

Commit 556d11c

Browse files
authored
Merge pull request #5289 from nebulab/kennyadsl/deprecation-updates
Deprecate `Spree::Deprecation` in favor of `Spree.deprecator`
2 parents 1ca7101 + a7dd17b commit 556d11c

File tree

27 files changed

+108
-90
lines changed

27 files changed

+108
-90
lines changed

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ Lint/SuppressedException:
337337

338338
Lint/MissingSuper:
339339
Exclude:
340-
- 'core/lib/spree/deprecation.rb' # this is a known class that doesn't require super
340+
- 'core/lib/spree/deprecated_instance_variable_proxy.rb' # this is a known class that doesn't require super
341341
- 'core/lib/spree/preferences/configuration.rb' # this class has no superclass defining `self.inherited`
342342

343343
Rails/FindEach:

backend/app/controllers/spree/admin/stock_items_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def load_stock_management_data
4545
view_context,
4646
:@stock_locations,
4747
:stock_item_stock_locations,
48-
Spree::Deprecation,
48+
Spree.deprecator,
4949
"Please, do not use @stock_item_stock_locations anymore in the views, use @stock_locations",
5050
)
5151
@variant_display_attributes = self.class.variant_display_attributes

backend/app/helpers/spree/admin/navigation_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ def tab(*args, &block)
5050
css_classes = Array(options[:css_class])
5151

5252
if options.key?(:route)
53-
Spree::Deprecation.warn "Passing a route to #tab is deprecated. Please pass a url instead."
53+
Spree.deprecator.warn "Passing a route to #tab is deprecated. Please pass a url instead."
5454
options[:url] ||= spree.send("#{options[:route]}_path")
5555
end
5656

5757
if args.any?
58-
Spree::Deprecation.warn "Passing resources to #tab is deprecated. Please use the `label:` and `match_path:` options instead."
58+
Spree.deprecator.warn "Passing resources to #tab is deprecated. Please use the `label:` and `match_path:` options instead."
5959
options[:label] ||= args.first
6060
options[:url] ||= spree.send("admin_#{args.first}_path")
6161
options[:selected] = args.include?(controller.controller_name.to_sym)

backend/app/views/spree/admin/shared/_product_sub_menu.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<% Spree::Deprecation.warn "Using the #{@virtual_path.inspect} partial is deprecated, please use MenuItem#children instead." %>
1+
<% Spree.deprecator.warn "Using the #{@virtual_path.inspect} partial is deprecated, please use MenuItem#children instead." %>
22

33
<ul class="admin-subnav" data-hook="admin_product_sub_tabs">
44
<% if can? :admin, Spree::Product %>

backend/app/views/spree/admin/shared/_promotion_sub_menu.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<% Spree::Deprecation.warn "Using the #{@virtual_path.inspect} partial is deprecated, please use MenuItem#children instead." %>
1+
<% Spree.deprecator.warn "Using the #{@virtual_path.inspect} partial is deprecated, please use MenuItem#children instead." %>
22

33
<ul class="admin-subnav" data-hook="admin_promotion_sub_tabs">
44
<% if can? :admin, Spree::Promotion %>

backend/app/views/spree/admin/shared/_settings_sub_menu.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<% Spree::Deprecation.warn "Using the #{@virtual_path.inspect} partial is deprecated, please use MenuItem#children instead." %>
1+
<% Spree.deprecator.warn "Using the #{@virtual_path.inspect} partial is deprecated, please use MenuItem#children instead." %>
22

33
<ul class="admin-subnav" data-hook="admin_settings_sub_tabs">
44
<% if can?(:admin, Spree::Store) %>

backend/lib/spree/backend_configuration/deprecated_tab_constants.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
Spree::Deprecation.warn(
3+
Spree.deprecator.warn(
44
"Spree::BackendConfiguration::*_TABS is deprecated. Please use Spree::BackendConfiguration::MenuItem(match_path:) instead."
55
)
66

backend/lib/spree/backend_configuration/menu_item.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ class MenuItem
99
def sections # rubocop:disable Style/TrivialAccessors
1010
@sections
1111
end
12-
deprecate sections: :label, deprecator: Spree::Deprecation
12+
deprecate sections: :label, deprecator: Spree.deprecator
1313

1414
attr_accessor :position # rubocop:disable Layout/EmptyLinesAroundAttributeAccessor
15-
deprecate position: nil, deprecator: Spree::Deprecation
16-
deprecate "position=": nil, deprecator: Spree::Deprecation
15+
deprecate position: nil, deprecator: Spree.deprecator
16+
deprecate "position=": nil, deprecator: Spree.deprecator
1717

1818
# @param icon [String] The icon to draw for this menu item
1919
# @param condition [Proc] A proc which returns true if this menu item
@@ -41,16 +41,16 @@ def initialize(
4141
if args.length == 2
4242
sections, icon = args
4343
label ||= sections.first.to_s
44-
Spree::Deprecation.warn "Passing sections to #{self.class.name} is deprecated. Please pass a label instead."
45-
Spree::Deprecation.warn "Passing icon to #{self.class.name} is deprecated. Please use the keyword argument instead."
44+
Spree.deprecator.warn "Passing sections to #{self.class.name} is deprecated. Please pass a label instead."
45+
Spree.deprecator.warn "Passing icon to #{self.class.name} is deprecated. Please use the keyword argument instead."
4646
elsif args.any?
4747
raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 0..2)"
4848
end
4949

5050
if partial.present? && children.blank?
5151
# We only show the deprecation if there are no children, because if there are children,
5252
# then the menu item is already future-proofed.
53-
Spree::Deprecation.warn "Passing a partial to #{self.class.name} is deprecated. Please use the children keyword argument instead."
53+
Spree.deprecator.warn "Passing a partial to #{self.class.name} is deprecated. Please use the children keyword argument instead."
5454
end
5555

5656
@condition = condition || -> { true }

backend/spec/helpers/admin/navigation_helper_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
without_partial_double_verification do
2929
allow(helper).to receive(:admin_orders_path).and_return("/admin/orders")
3030
end
31-
expect(Spree::Deprecation).to receive(:warn)
31+
expect(Spree.deprecator).to receive(:warn)
3232
.with("Passing a route to #tab is deprecated. Please pass a url instead.")
3333
expect(helper.tab(label: :orders, route: :admin_orders)).to include('href="/admin/orders"')
3434
end

backend/spec/lib/spree/backend_configuration/menu_item_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
RSpec.describe Spree::BackendConfiguration::MenuItem do
66
describe '#children' do
77
it 'is the replacement for the deprecated #partial method' do
8-
expect(Spree::Deprecation).to receive(:warn).with(a_string_matching(/partial/))
8+
expect(Spree.deprecator).to receive(:warn).with(a_string_matching(/partial/))
99

1010
described_class.new(partial: 'foo')
1111
end
@@ -67,8 +67,8 @@
6767
describe "deprecated behavior" do
6868
describe "passing `sections` and `icon` sequentially" do
6969
it "warns about the deprecation" do
70-
expect(Spree::Deprecation).to receive(:warn).with(a_string_matching(/sections/))
71-
expect(Spree::Deprecation).to receive(:warn).with(a_string_matching(/icon/))
70+
expect(Spree.deprecator).to receive(:warn).with(a_string_matching(/sections/))
71+
expect(Spree.deprecator).to receive(:warn).with(a_string_matching(/icon/))
7272

7373
described_class.new([:foo, :bar], 'icon')
7474
end

backend/spec/lib/spree/backend_configuration_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def product_path(product)
118118
describe "deprecated behavior" do
119119
describe "loading *_TABS constants" do
120120
it "warns about the deprecation" do
121-
expect(Spree::Deprecation).to receive(:warn).with(a_string_matching(/Spree::BackendConfiguration::\*_TABS is deprecated\./))
121+
expect(Spree.deprecator).to receive(:warn).with(a_string_matching(/Spree::BackendConfiguration::\*_TABS is deprecated\./))
122122

123123
described_class::ORDER_TABS
124124
end

core/app/models/spree/adjustment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Adjustment < Spree::Base
4848
scope :is_included, -> { where(included: true) }
4949
scope :additional, -> { where(included: false) }
5050

51-
singleton_class.deprecate :return_authorization, deprecator: Spree::Deprecation
51+
singleton_class.deprecate :return_authorization, deprecator: Spree.deprecator
5252

5353
extend DisplayMoney
5454
money_methods :amount

core/app/models/spree/order.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def recalculator
264264
@recalculator ||= Spree::Config.order_recalculator_class.new(self)
265265
end
266266
alias_method :updater, :recalculator
267-
deprecate updater: :recalculator, deprecator: Spree::Deprecation
267+
deprecate updater: :recalculator, deprecator: Spree.deprecator
268268

269269
def assign_billing_to_shipping_address
270270
self.ship_address = bill_address if bill_address
@@ -513,7 +513,7 @@ def check_shipments_and_restart_checkout
513513
end
514514

515515
alias_method :ensure_updated_shipments, :check_shipments_and_restart_checkout
516-
deprecate ensure_updated_shipments: :check_shipments_and_restart_checkout, deprecator: Spree::Deprecation
516+
deprecate ensure_updated_shipments: :check_shipments_and_restart_checkout, deprecator: Spree.deprecator
517517

518518
def restart_checkout_flow
519519
return if state == 'cart'

core/app/models/spree/order_updater.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def recalculate
3131
end
3232
end
3333
alias_method :update, :recalculate
34-
deprecate update: :recalculate, deprecator: Spree::Deprecation
34+
deprecate update: :recalculate, deprecator: Spree.deprecator
3535

3636
# Updates the +shipment_state+ attribute according to the following logic:
3737
#

core/lib/generators/spree/dummy/templates/rails/test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333

3434
# Raise on deprecation warnings
3535
if ENV['SOLIDUS_RAISE_DEPRECATIONS'].present?
36-
Spree::Deprecation.behavior = :raise
36+
Spree.deprecator.behavior = :raise
3737
end
3838
end

core/lib/spree/core.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
require "active_storage/engine"
1010
require "sprockets/railtie"
1111

12+
require 'active_support/deprecation'
13+
require 'spree/deprecated_instance_variable_proxy'
1214
require 'acts_as_list'
1315
require 'awesome_nested_set'
1416
require 'cancan'
@@ -19,13 +21,17 @@
1921
require 'ransack'
2022
require 'state_machines-activerecord'
2123

22-
require 'spree/deprecation'
23-
2424
# This is required because ActiveModel::Validations#invalid? conflicts with the
2525
# invalid state of a Payment. In the future this should be removed.
2626
StateMachines::Machine.ignore_method_conflicts = true
2727

2828
module Spree
29+
def self.deprecator
30+
@deprecator ||= ActiveSupport::Deprecation.new('5.0', 'Solidus')
31+
end
32+
33+
autoload :Deprecation, 'spree/deprecation'
34+
2935
mattr_accessor :user_class, default: 'Spree::LegacyUser'
3036

3137
def self.user_class

core/lib/spree/core/engine.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ class Engine < ::Rails::Engine
7575
ActionMailer::Base.preview_path = app.config.action_mailer.preview_path
7676
end
7777

78+
initializer "spree.deprecator" do |app|
79+
if app.respond_to?(:deprecators)
80+
app.deprecators[:spree] = Spree.deprecator
81+
end
82+
end
83+
7884
config.after_initialize do
7985
Spree::Config.check_load_defaults_called('Spree::Config')
8086
Spree::Config.static_model_preferences.validate!
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
require 'active_support/deprecation'
4+
5+
module Spree
6+
# This DeprecatedInstanceVariableProxy transforms instance variable to
7+
# deprecated instance variable.
8+
#
9+
# It differs from ActiveSupport::DeprecatedInstanceVariableProxy since
10+
# it allows to define a custom message.
11+
#
12+
# class Example
13+
# def initialize(deprecator)
14+
# @request = Spree::DeprecatedInstanceVariableProxy.new(self, :request, :@request, deprecator, "Please, do not use this thing.")
15+
# @_request = :a_request
16+
# end
17+
#
18+
# def request
19+
# @_request
20+
# end
21+
#
22+
# def old_request
23+
# @request
24+
# end
25+
# end
26+
#
27+
# When someone execute any method on @request variable this will trigger
28+
# +warn+ method on +deprecator_instance+ and will fetch <tt>@_request</tt>
29+
# variable via +request+ method and execute the same method on non-proxy
30+
# instance variable.
31+
#
32+
# Default deprecator is <tt>Spree.deprecator</tt>.
33+
class DeprecatedInstanceVariableProxy < ActiveSupport::Deprecation::DeprecationProxy
34+
def initialize(instance, method_or_var, var = "@#{method}", deprecator = Spree.deprecator, message = nil)
35+
@instance = instance
36+
@method_or_var = method_or_var
37+
@var = var
38+
@deprecator = deprecator
39+
@message = message
40+
end
41+
42+
private
43+
44+
def target
45+
return @instance.instance_variable_get(@method_or_var) if @instance.instance_variable_defined?(@method_or_var)
46+
47+
@instance.__send__(@method_or_var)
48+
end
49+
50+
def warn(callstack, called, args)
51+
message = @message || "#{@var} is deprecated! Call #{@method_or_var}.#{called} instead of #{@var}.#{called}."
52+
message = [message, "Args: #{args.inspect}"].join(" ") unless args.empty?
53+
54+
@deprecator.warn(message, callstack)
55+
end
56+
end
57+
end

core/lib/spree/deprecation.rb

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,9 @@
11
# frozen_string_literal: true
22

3-
require 'active_support/deprecation'
3+
require 'spree/core'
44

55
module Spree
6-
Deprecation = ActiveSupport::Deprecation.new('5.0', 'Solidus')
6+
Deprecation = Spree.deprecator
77

8-
# This DeprecatedInstanceVariableProxy transforms instance variable to
9-
# deprecated instance variable.
10-
#
11-
# It differs from ActiveSupport::DeprecatedInstanceVariableProxy since
12-
# it allows to define a custom message.
13-
#
14-
# class Example
15-
# def initialize(deprecator)
16-
# @request = Spree::DeprecatedInstanceVariableProxy.new(self, :request, :@request, deprecator, "Please, do not use this thing.")
17-
# @_request = :a_request
18-
# end
19-
#
20-
# def request
21-
# @_request
22-
# end
23-
#
24-
# def old_request
25-
# @request
26-
# end
27-
# end
28-
#
29-
# When someone execute any method on @request variable this will trigger
30-
# +warn+ method on +deprecator_instance+ and will fetch <tt>@_request</tt>
31-
# variable via +request+ method and execute the same method on non-proxy
32-
# instance variable.
33-
#
34-
# Default deprecator is <tt>Spree::Deprecation</tt>.
35-
class DeprecatedInstanceVariableProxy < ActiveSupport::Deprecation::DeprecationProxy
36-
def initialize(instance, method_or_var, var = "@#{method}", deprecator = Spree::Deprecation, message = nil)
37-
@instance = instance
38-
@method_or_var = method_or_var
39-
@var = var
40-
@deprecator = deprecator
41-
@message = message
42-
end
43-
44-
private
45-
46-
def target
47-
return @instance.instance_variable_get(@method_or_var) if @instance.instance_variable_defined?(@method_or_var)
48-
49-
@instance.__send__(@method_or_var)
50-
end
51-
52-
def warn(callstack, called, args)
53-
message = @message || "#{@var} is deprecated! Call #{@method_or_var}.#{called} instead of #{@var}.#{called}."
54-
message = [message, "Args: #{args.inspect}"].join(" ") unless args.empty?
55-
56-
@deprecator.warn(message, callstack)
57-
end
58-
end
8+
Spree.deprecator.warn "Spree::Deprecation is deprecated. Please use Spree.deprecator instead.", caller(2)
599
end

core/lib/spree/preferences/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def check_load_defaults_called(instance_constant_name = nil)
5959
return if load_defaults_called || !Spree::Core.has_install_generator_been_run?
6060

6161
target_name = instance_constant_name || "#{self.class.name}.new"
62-
Spree::Deprecation.warn <<~MSG
62+
Spree.deprecator.warn <<~MSG
6363
It's recommended that you explicitly load the default configuration for
6464
your current Solidus version. You can do it by adding the following call
6565
to your Solidus initializer within the #{target_name} block:

core/lib/spree/preferences/preferable_class_methods.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# frozen_string_literal: true
22

3-
require 'spree/deprecation'
43
require 'spree/encryptor'
54

65
module Spree::Preferences

core/lib/spree/testing_support/dummy_app.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,5 @@ class Application < ::Rails::Application
142142

143143
# Raise on deprecation warnings
144144
if ENV['SOLIDUS_RAISE_DEPRECATIONS'].present?
145-
Spree::Deprecation.behavior = :raise
145+
Spree.deprecator.behavior = :raise
146146
end

core/lib/spree/testing_support/factory_bot.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def self.check_version
3030
MSG
3131
end
3232
end
33-
deprecate :check_version, deprecator: Spree::Deprecation
33+
deprecate :check_version, deprecator: Spree.deprecator
3434

3535
def self.add_definitions!
3636
::FactoryBot.definition_file_paths.unshift(*definition_file_paths).uniq!

core/lib/spree/testing_support/silence_deprecations.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
RSpec.configure do |config|
44
config.around(:each, silence_deprecations: true) do |example|
5-
Spree::Deprecation.silence do
5+
Spree.deprecator.silence do
66
example.run
77
end
88
end

core/spec/lib/spree/migration_helpers_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
context "when the column exists" do
1818
context "and the index does" do
1919
it "passes compatible arguments to index_exists?" do
20-
expect { subject }.to_not raise_error(ArgumentError)
20+
expect { subject }.to raise_error(NotImplementedError) # not ArgumentError
2121
end
2222
end
2323

@@ -27,7 +27,7 @@
2727
end
2828

2929
it "passes compatible arguments to add_index" do
30-
expect { subject }.to_not raise_error(ArgumentError)
30+
expect { subject }.to raise_error(TypeError) # not ArgumentError
3131
end
3232
end
3333
end

0 commit comments

Comments
 (0)