From 762fbe47f4ef29ae16e8be06ac65d7bd8ade9b24 Mon Sep 17 00:00:00 2001 From: Alberto Vena Date: Fri, 1 Mar 2019 15:31:18 +0100 Subject: [PATCH 1/2] Add a Spree::DeprecatedInstanceVariableProxy to allow custom messages The default Rails class does not allow to pass a custom message to the deprecation. This custom class extends its behavior to make it possible. Note that I also opened a PR in Rails for fixing this: rails/rails#35442 --- core/lib/spree/deprecation.rb | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/core/lib/spree/deprecation.rb b/core/lib/spree/deprecation.rb index 449803a3061..e2f1cf976e9 100644 --- a/core/lib/spree/deprecation.rb +++ b/core/lib/spree/deprecation.rb @@ -4,4 +4,54 @@ module Spree Deprecation = ActiveSupport::Deprecation.new('3.0', 'Solidus') + + # 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 @_request + # variable via +request+ method and execute the same method on non-proxy + # instance variable. + # + # Default deprecator is Spree::Deprecation. + 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 end From 52374f46ecd2cfd465bc4cb7d2854817c309a2bc Mon Sep 17 00:00:00 2001 From: Alberto Vena Date: Fri, 1 Mar 2019 10:55:37 +0100 Subject: [PATCH 2/2] Deprecate @payment_sources ivar from checkout payments controller This method returns payment sources of the current user. It is no more used into our frontend. We used to assign the content of this method into an ivar (@payment_sources) into the checkout payment step. This method is here only to be able to deprecate this ivar and will be removed. --- .../controllers/spree/checkout_controller.rb | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/frontend/app/controllers/spree/checkout_controller.rb b/frontend/app/controllers/spree/checkout_controller.rb index 48955c33614..d6f6e86e23b 100644 --- a/frontend/app/controllers/spree/checkout_controller.rb +++ b/frontend/app/controllers/spree/checkout_controller.rb @@ -219,9 +219,14 @@ def before_payment @wallet_payment_sources = try_spree_current_user.wallet.wallet_payment_sources @default_wallet_payment_source = @wallet_payment_sources.detect(&:default) || @wallet_payment_sources.first - # TODO: How can we deprecate this instance variable? We could try - # wrapping it in a delegating object that produces deprecation warnings. - @payment_sources = try_spree_current_user.wallet.wallet_payment_sources.map(&:payment_source).select { |ps| ps.is_a?(Spree::CreditCard) } + + @payment_sources = Spree::DeprecatedInstanceVariableProxy.new( + self, + :deprecated_payment_sources, + :@payment_sources, + Spree::Deprecation, + "Please, do not use @payment_sources anymore, use @wallet_payment_sources instead." + ) end end @@ -251,5 +256,22 @@ def insufficient_stock_error end end end + + # This method returns payment sources of the current user. It is no more + # used into our frontend. We used to assign the content of this method + # into an ivar (@payment_sources) into the checkout payment step. This + # method is here only to be able to deprecate this ivar and will be removed. + # + # DO NOT USE THIS METHOD! + # + # @return [Array] Payment sources connected to + # current user wallet. + # @deprecated This method has been added to deprecate @payment_sources + # ivar and will be removed. Use @wallet_payment_sources instead. + def deprecated_payment_sources + try_spree_current_user.wallet.wallet_payment_sources + .map(&:payment_source) + .select { |ps| ps.is_a?(Spree::CreditCard) } + end end end