Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v4.1] Deprecate Spree::Deprecation in favor of Spree.deprecator #6103

Merged
merged 4 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ Lint/SuppressedException:

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

Rails/FindEach:
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def recalculator
@recalculator ||= Spree::Config.order_recalculator_class.new(self)
end
alias_method :updater, :recalculator
deprecate updater: :recalculator, deprecator: Spree::Deprecation
deprecate updater: :recalculator, deprecator: Spree.deprecator

def assign_billing_to_shipping_address
self.ship_address = bill_address if bill_address
Expand Down Expand Up @@ -513,7 +513,7 @@ def check_shipments_and_restart_checkout
end

alias_method :ensure_updated_shipments, :check_shipments_and_restart_checkout
deprecate ensure_updated_shipments: :check_shipments_and_restart_checkout, deprecator: Spree::Deprecation
deprecate ensure_updated_shipments: :check_shipments_and_restart_checkout, deprecator: Spree.deprecator

def restart_checkout_flow
return if state == 'cart'
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/order_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def recalculate
end
end
alias_method :update, :recalculate
deprecate update: :recalculate, deprecator: Spree::Deprecation
deprecate update: :recalculate, deprecator: Spree.deprecator

# Updates the +shipment_state+ attribute according to the following logic:
#
Expand Down
2 changes: 1 addition & 1 deletion core/lib/generators/spree/dummy/templates/rails/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@

# Raise on deprecation warnings
if ENV['SOLIDUS_RAISE_DEPRECATIONS'].present?
Spree::Deprecation.behavior = :raise
Spree.deprecator.behavior = :raise

Check warning on line 36 in core/lib/generators/spree/dummy/templates/rails/test.rb

View check run for this annotation

Codecov / codecov/patch

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

Added line #L36 was not covered by tests
end
end
10 changes: 8 additions & 2 deletions core/lib/spree/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
require "active_storage/engine"
require "sprockets/railtie"

require 'active_support/deprecation'
require 'spree/deprecated_instance_variable_proxy'
require 'acts_as_list'
require 'awesome_nested_set'
require 'cancan'
Expand All @@ -19,13 +21,17 @@
require 'ransack'
require 'state_machines-activerecord'

require 'spree/deprecation'

# This is required because ActiveModel::Validations#invalid? conflicts with the
# invalid state of a Payment. In the future this should be removed.
StateMachines::Machine.ignore_method_conflicts = true

module Spree
def self.deprecator
@deprecator ||= ActiveSupport::Deprecation.new('5.0', 'Solidus')
end

autoload :Deprecation, 'spree/deprecation'

mattr_accessor :user_class, default: 'Spree::LegacyUser'

def self.user_class
Expand Down
6 changes: 6 additions & 0 deletions core/lib/spree/core/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@
ActionMailer::Base.preview_path = app.config.action_mailer.preview_path
end

initializer "spree.deprecator" do |app|
if app.respond_to?(:deprecators)
app.deprecators[:spree] = Spree.deprecator

Check warning on line 80 in core/lib/spree/core/engine.rb

View check run for this annotation

Codecov / codecov/patch

core/lib/spree/core/engine.rb#L80

Added line #L80 was not covered by tests
end
end

config.after_initialize do
Spree::Config.check_load_defaults_called('Spree::Config')
Spree::Config.static_model_preferences.validate!
Expand Down
57 changes: 57 additions & 0 deletions core/lib/spree/deprecated_instance_variable_proxy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

require 'active_support/deprecation'

module Spree
# This DeprecatedInstanceVariableProxy transforms instance variable to
# deprecated instance variable.
#
# It differs from ActiveSupport::DeprecatedInstanceVariableProxy since
# it allows to define a custom message.
#
# class Example
# def initialize(deprecator)
# @request = Spree::DeprecatedInstanceVariableProxy.new(self, :request, :@request, deprecator, "Please, do not use this thing.")
# @_request = :a_request
# end
#
# def request
# @_request
# end
#
# def old_request
# @request
# end
# end
#
# When someone execute any method on @request variable this will trigger
# +warn+ method on +deprecator_instance+ and will fetch <tt>@_request</tt>
# variable via +request+ method and execute the same method on non-proxy
# instance variable.
#
# Default deprecator is <tt>Spree.deprecator</tt>.
class DeprecatedInstanceVariableProxy < ActiveSupport::Deprecation::DeprecationProxy
def initialize(instance, method_or_var, var = "@#{method}", deprecator = Spree.deprecator, message = nil)
@instance = instance
@method_or_var = method_or_var
@var = var
@deprecator = deprecator
@message = message

Check warning on line 39 in core/lib/spree/deprecated_instance_variable_proxy.rb

View check run for this annotation

Codecov / codecov/patch

core/lib/spree/deprecated_instance_variable_proxy.rb#L35-L39

Added lines #L35 - L39 were not covered by tests
end

private

def target
return @instance.instance_variable_get(@method_or_var) if @instance.instance_variable_defined?(@method_or_var)

Check warning on line 45 in core/lib/spree/deprecated_instance_variable_proxy.rb

View check run for this annotation

Codecov / codecov/patch

core/lib/spree/deprecated_instance_variable_proxy.rb#L45

Added line #L45 was not covered by tests

@instance.__send__(@method_or_var)

Check warning on line 47 in core/lib/spree/deprecated_instance_variable_proxy.rb

View check run for this annotation

Codecov / codecov/patch

core/lib/spree/deprecated_instance_variable_proxy.rb#L47

Added line #L47 was not covered by tests
end

def warn(callstack, called, args)
message = @message || "#{@var} is deprecated! Call #{@method_or_var}.#{called} instead of #{@var}.#{called}."
message = [message, "Args: #{args.inspect}"].join(" ") unless args.empty?

Check warning on line 52 in core/lib/spree/deprecated_instance_variable_proxy.rb

View check run for this annotation

Codecov / codecov/patch

core/lib/spree/deprecated_instance_variable_proxy.rb#L51-L52

Added lines #L51 - L52 were not covered by tests

@deprecator.warn(message, callstack)

Check warning on line 54 in core/lib/spree/deprecated_instance_variable_proxy.rb

View check run for this annotation

Codecov / codecov/patch

core/lib/spree/deprecated_instance_variable_proxy.rb#L54

Added line #L54 was not covered by tests
end
end
end
54 changes: 3 additions & 51 deletions core/lib/spree/deprecation.rb
Original file line number Diff line number Diff line change
@@ -1,57 +1,9 @@
# frozen_string_literal: true

require 'active_support/deprecation'
require 'spree/core'

Check warning on line 3 in core/lib/spree/deprecation.rb

View check run for this annotation

Codecov / codecov/patch

core/lib/spree/deprecation.rb#L3

Added line #L3 was not covered by tests

module Spree
Deprecation = ActiveSupport::Deprecation.new('5.0', 'Solidus')
Deprecation = Spree.deprecator

Check warning on line 6 in core/lib/spree/deprecation.rb

View check run for this annotation

Codecov / codecov/patch

core/lib/spree/deprecation.rb#L6

Added line #L6 was not covered by tests

# This DeprecatedInstanceVariableProxy transforms instance variable to
# deprecated instance variable.
#
# It differs from ActiveSupport::DeprecatedInstanceVariableProxy since
# it allows to define a custom message.
#
# class Example
# def initialize(deprecator)
# @request = Spree::DeprecatedInstanceVariableProxy.new(self, :request, :@request, deprecator, "Please, do not use this thing.")
# @_request = :a_request
# end
#
# def request
# @_request
# end
#
# def old_request
# @request
# end
# end
#
# When someone execute any method on @request variable this will trigger
# +warn+ method on +deprecator_instance+ and will fetch <tt>@_request</tt>
# variable via +request+ method and execute the same method on non-proxy
# instance variable.
#
# Default deprecator is <tt>Spree::Deprecation</tt>.
class DeprecatedInstanceVariableProxy < ActiveSupport::Deprecation::DeprecationProxy
def initialize(instance, method, var = "@#{method}", deprecator = Spree::Deprecation, message = nil)
@instance = instance
@method = method
@var = var
@deprecator = deprecator
@message = message
end

private

def target
@instance.__send__(@method)
end

def warn(callstack, called, args)
message = @message || "#{@var} is deprecated! Call #{@method}.#{called} instead of #{@var}.#{called}."
message = [message, "Args: #{args.inspect}"].join(" ")

@deprecator.warn(message, callstack)
end
end
Spree.deprecator.warn "Spree::Deprecation is deprecated. Please use Spree.deprecator instead.", caller(2)

Check warning on line 8 in core/lib/spree/deprecation.rb

View check run for this annotation

Codecov / codecov/patch

core/lib/spree/deprecation.rb#L8

Added line #L8 was not covered by tests
end
2 changes: 1 addition & 1 deletion core/lib/spree/preferences/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def check_load_defaults_called(instance_constant_name = nil)
return if load_defaults_called || !Spree::Core.has_install_generator_been_run?

target_name = instance_constant_name || "#{self.class.name}.new"
Spree::Deprecation.warn <<~MSG
Spree.deprecator.warn <<~MSG
It's recommended that you explicitly load the default configuration for
your current Solidus version. You can do it by adding the following call
to your Solidus initializer within the #{target_name} block:
Expand Down
1 change: 0 additions & 1 deletion core/lib/spree/preferences/preferable_class_methods.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require 'spree/deprecation'
require 'spree/encryptor'

module Spree::Preferences
Expand Down
2 changes: 1 addition & 1 deletion core/lib/spree/testing_support/dummy_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,5 @@ class Application < ::Rails::Application

# Raise on deprecation warnings
if ENV['SOLIDUS_RAISE_DEPRECATIONS'].present?
Spree::Deprecation.behavior = :raise
Spree.deprecator.behavior = :raise
end
2 changes: 1 addition & 1 deletion core/lib/spree/testing_support/factory_bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def self.check_version
MSG
end
end
deprecate :check_version, deprecator: Spree::Deprecation
deprecate :check_version, deprecator: Spree.deprecator

def self.add_definitions!
::FactoryBot.definition_file_paths.unshift(*definition_file_paths).uniq!
Expand Down
2 changes: 1 addition & 1 deletion core/lib/spree/testing_support/silence_deprecations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.configure do |config|
config.around(:each, silence_deprecations: true) do |example|
Spree::Deprecation.silence do
Spree.deprecator.silence do

Check warning on line 5 in core/lib/spree/testing_support/silence_deprecations.rb

View check run for this annotation

Codecov / codecov/patch

core/lib/spree/testing_support/silence_deprecations.rb#L5

Added line #L5 was not covered by tests
example.run
end
end
Expand Down
4 changes: 2 additions & 2 deletions core/spec/lib/spree/migration_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
context "when the column exists" do
context "and the index does" do
it "passes compatible arguments to index_exists?" do
expect { subject }.to_not raise_error(ArgumentError)
expect { subject }.to raise_error(NotImplementedError) # not ArgumentError
end
end

Expand All @@ -27,7 +27,7 @@
end

it "passes compatible arguments to add_index" do
expect { subject }.to_not raise_error(ArgumentError)
expect { subject }.to raise_error(TypeError) # not ArgumentError
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion core/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def merge!(other_order, user = nil)
let(:subject) { order.ensure_updated_shipments }

it "is deprecated" do
expect(Spree::Deprecation).to receive(:warn).with(/ensure_updated_shipments is deprecated.*use check_shipments_and_restart_checkout instead/, any_args)
expect(Spree.deprecator).to receive(:warn).with(/ensure_updated_shipments is deprecated.*use check_shipments_and_restart_checkout instead/, any_args)

subject
end
Expand Down
6 changes: 3 additions & 3 deletions core/spec/models/spree/preferences/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,21 @@
it 'does not emit a warning' do
config.load_defaults '3.1'

expect(Spree::Deprecation).not_to receive(:warn)
expect(Spree.deprecator).not_to receive(:warn)

config.check_load_defaults_called
end
end

context 'when load_defaults_called is false' do
it 'emits a warning' do
expect(Spree::Deprecation).to receive(:warn).with(/adding.*load_defaults/m)
expect(Spree.deprecator).to receive(:warn).with(/adding.*load_defaults/m)

config.check_load_defaults_called
end

it 'includes constant name in the message when given' do
expect(Spree::Deprecation).to receive(:warn).with(/Spree::Config/, any_args)
expect(Spree.deprecator).to receive(:warn).with(/Spree::Config/, any_args)

config.check_load_defaults_called('Spree::Config')
end
Expand Down
Loading