Skip to content

Commit

Permalink
Extend Spree::DeprecatedInstanceVariableProxy to work with methods or…
Browse files Browse the repository at this point in the history
… variables

Previous impleemntation was only working with either attribute readers or
methods, but it was not working with instance variables.
It now checks whether the final target is either a method, property or
an instance variable
  • Loading branch information
softr8 authored and elia committed Jul 17, 2023
1 parent 8a091a2 commit e76677e
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions core/lib/spree/deprecation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ module Spree
#
# Default deprecator is <tt>Spree::Deprecation</tt>.
class DeprecatedInstanceVariableProxy < ActiveSupport::Deprecation::DeprecationProxy
def initialize(instance, method, var = "@#{method}", deprecator = Spree::Deprecation, message = nil)
def initialize(instance, method_or_var, var = "@#{method}", deprecator = Spree::Deprecation, message = nil)
@instance = instance
@method = method
@method_or_var = method_or_var
@var = var
@deprecator = deprecator
@message = message
Expand All @@ -44,12 +44,14 @@ def initialize(instance, method, var = "@#{method}", deprecator = Spree::Depreca
private

def target
@instance.__send__(@method)
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}.#{called} instead of #{@var}.#{called}."
message = [message, "Args: #{args.inspect}"].join(" ")
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
Expand Down

0 comments on commit e76677e

Please sign in to comment.