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

Deprecate @payment_sources ivar in checkout controller #3128

Merged
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
50 changes: 50 additions & 0 deletions core/lib/spree/deprecation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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
end
28 changes: 25 additions & 3 deletions frontend/app/controllers/spree/checkout_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<Spree::PaymentSource>] 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