-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Fix calculator class check bug #2501
Conversation
Hey @pelargir, thank you! Would you mind adding a spec for the intended behaviour? I know there is one :) |
|
||
describe '#calculator_type=' do | ||
subject { Calculable.new } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace detected.
Using #is_a? to check the calculator class doesn't play well with calculators that are subclasses of each other. For example, given calculator A and calculator B, where B is a subclass of A: if a shipping method is configured to use calculator B, it becomes impossible to switch the shipping method to calculator A since A.is_a?(B) returns true. Whereas A.instance_of?(B) returns false. If the intention is to prevent the same calculator from being set twice, #instance_of? is the correct choice since it disregards subclasses and only returns true if both are the exact same class.
@mamhoff done! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @pelargir!
This PR fixes a bug found when attempting to change a shipping method's calculator to a different calculator that is a subclass of its current calculator. The existing implementation of this code prevents switching between calculators that subclass other calculators.
Using
#is_a?
to check the calculator class doesn't play well with calculators that are subclasses of each other. For example, given calculator A and calculator B, where B is a subclass of A: if a shipping method is configured to use calculator B, it becomes impossible to switch the shipping method to calculator A sinceA.is_a?(B)
returns true. WhereasA.instance_of?(B)
returns false.If the intention is to prevent the same calculator from being set twice,
#instance_of?
is the correct choice since it disregards subclasses and only returns true if both are the exact same class.