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

Fix calculator class check bug #2501

Merged
merged 1 commit into from
Jan 15, 2018
Merged

Fix calculator class check bug #2501

merged 1 commit into from
Jan 15, 2018

Conversation

pelargir
Copy link
Contributor

@pelargir pelargir commented Jan 11, 2018

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 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
Copy link
Contributor

mamhoff commented Jan 11, 2018

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 }

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.
@pelargir
Copy link
Contributor Author

@mamhoff done!

Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

Copy link
Contributor

@mamhoff mamhoff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @pelargir!

@jhawthorn jhawthorn merged commit bd15e16 into solidusio:master Jan 15, 2018
@pelargir pelargir deleted the fix-calculator-bug branch October 10, 2019 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants