Skip to content

Commit

Permalink
Deprecate Spree::Deprecation in favor of Spree.deprecator
Browse files Browse the repository at this point in the history
Use autoload to print a deprecation only if the constant is loaded.
  • Loading branch information
kennyadsl authored and elia committed Sep 27, 2023
1 parent e822f3f commit 848bf9f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,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
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
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
end

private

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

@instance.__send__(@method_or_var)
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?

@deprecator.warn(message, callstack)
end
end
end
58 changes: 2 additions & 56 deletions core/lib/spree/deprecation.rb
Original file line number Diff line number Diff line change
@@ -1,63 +1,9 @@
# frozen_string_literal: true

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

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

Deprecation = Spree.deprecator

# 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
end

private

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

@instance.__send__(@method_or_var)
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?

@deprecator.warn(message, callstack)
end
end
Spree.deprecator.warn "Spree::Deprecation is deprecated. Please use Spree.deprecator instead.", caller(2)
end
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

0 comments on commit 848bf9f

Please sign in to comment.