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

[Spree upgrade] Workaround Rails inheritance bug in Spree::Gateway #3370

Merged
merged 2 commits into from
Feb 13, 2019
Merged

[Spree upgrade] Workaround Rails inheritance bug in Spree::Gateway #3370

merged 2 commits into from
Feb 13, 2019

Conversation

mkllnk
Copy link
Member

@mkllnk mkllnk commented Jan 23, 2019

What? Why?

Closes #3121

Due to a bug in ActiveRecord we need to load the tagging code in Gateway which should have inherited it from its parent PaymentMethod. We have to call it before loading the PaymentMethod decorator because the tagging code won't load twice within the inheritance chain.

I tested what happens when you assign tags to a PayPalExpress gateway and they are correctly stored with :taggable_type => "Spree::PaymentMethod".

I also considered a tagging implementation that doesn't require decorating, but we would be working against the used gem which assumes to be called on the tagged ActiveRecord model. It may still be an option, but needs deeper study of the acts_as_taggable_on code.

What should we test?

If we test this: Create a PayPal payment method from the admin interface.

Please note: the affected spec/features/admin/payment_method_spec.rb:72 executes further, but does not pass. The following failure means that this pull request is successful:

     Failure/Error: expect(page).to have_field 'Password', with: ''
       expected to find visible field "Password" that is not disabled with value "" but there were no matches. Also found "", which matched the selector but not all filters.
     # ./spec/features/admin/payment_method_spec.rb:108:in `block (2 levels) in <top (required)>'

How is this related to the Spree upgrade?

This is part of the Spree upgrade.

@mkllnk mkllnk self-assigned this Jan 23, 2019
Copy link
Contributor

@luisramos0 luisramos0 left a comment

Choose a reason for hiding this comment

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

Awesome work!!!
If the spec is not green yet, maybe we leave #3121 open to cover the task of fixing the spec?

@mkllnk
Copy link
Member Author

mkllnk commented Jan 23, 2019

I created a new issue #3379. The old one became a bit long and the agenda changed already.

@mkllnk
Copy link
Member Author

mkllnk commented Jan 23, 2019

Semaphore failed at another point in the spec:

     Failure/Error: expect(first('tags-input .tag-list ti-tag-item')).to have_content "member"
       expected to find text "member" in ""
     # ./spec/features/admin/payment_method_spec.rb:94:in `block (2 levels) in <top (required)>'

That has to do with tags, so I should double-check my PR.

@mkllnk
Copy link
Member Author

mkllnk commented Jan 24, 2019

Ah, I needed to include attr_accessible :tag_list as well. I'm still wondering if there is a better solution to implement tags without modifying Spree models...

Copy link
Contributor

@luisramos0 luisramos0 left a comment

Choose a reason for hiding this comment

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

ah, yes, there were two attributes.

@luisramos0
Copy link
Contributor

this PR fixes the same problem in 3 specs in spec/features/admin/subscriptions_spec.rb
👍

Copy link
Member

@kristinalim kristinalim left a comment

Choose a reason for hiding this comment

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

Nice investigation, @mkllnk!

I tested what happens when you assign tags to a PayPalExpress gateway and they are correctly stored with :taggable_type => "Spree::PaymentMethod".

Could you cover this with a test, so we will find out if this changes in future implementations or upgrade-related work?


module Spree
describe PaymentMethod do
it_behaves_like "taggable"
Copy link
Contributor

Choose a reason for hiding this comment

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

why not moving this to the payment_method_spec.rb? That is where I'd go check that if I had to.

Copy link
Member Author

Choose a reason for hiding this comment

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

This spec is actually not about the PaymentMethod but about its descendants. Since I'm testing several classes for the same thing I do it in one place instead of scattering these tests in the spec files of every descendant. We are actually testing Ruby's inheritance here which doesn't belong to one specific class.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added more comments to explain this so that the next developer doesn't have the same question. Thank you for prompting. 👍 :-)

describe Gateway::StripeConnect do
it_behaves_like "taggable"
end
end
Copy link
Contributor

Choose a reason for hiding this comment

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

The same for the other models.

Spree::Gateway.class_eval do
acts_as_taggable
attr_accessible :tag_list
end
Copy link
Contributor

Choose a reason for hiding this comment

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

but if essentially this is a order-related bug doesn't a require 'spree/payment_method' in the appropriate place helps us here? apart from that, from rails/rails#3847 looks like some gem could be messing things up as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it is order-related. Yes, it could be a gem. It could even be Spree.

A require 'spree/payment_method' doesn't help, because the inheritance already happened when we access the Spree code. I tried that in many places, for hours, after Luis tried that in many places, for hours. You can have another go and maybe you can find a better solution. But we spent several dev days on this already and couldn't find any better solution.

Since this bug is hopefully fixed in the next version of Rails we shouldn't invest much more time.

One experiment I would support as a tech-debt task: Instead of adding tags to Spree::PaymentMethod, we could try to have a new class called TaggablePaymentMethod which basically decorates any other payment method. All code using tagging interface would use that class instead of using the payment method instance directly.

What do you think? Should I create an issue for that?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's fine. It doesn't make sense to spend any more time on this. I wouldn't add the tech debt task either for now.

Due to a bug in ActiveRecord we need to load the tagging code in Gateway which
should have inherited it from its parent PaymentMethod.
We have to call it before loading the PaymentMethod decorator because the
tagging code won't load twice within the inheritance chain.

#3121
@mkllnk
Copy link
Member Author

mkllnk commented Feb 13, 2019

@kristinalim, thank you for nudging me to increase the test coverage. I hesitated because it makes everything more complicated. It actually took me several hours to write a spec I'm content with. The difficult trade-off was between a flexible shared example that works with any payment method and using our factories. Let me know what you think about the new commit.

Copy link
Contributor

@luisramos0 luisramos0 left a comment

Choose a reason for hiding this comment

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

awesome @mkllnk, lets move on :-)

@sauloperez
Copy link
Contributor

Waiting for @kristinalim 's answer to merge this.

Copy link
Member

@kristinalim kristinalim left a comment

Choose a reason for hiding this comment

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

I think this is great. 🙂

@luisramos0 luisramos0 merged commit 576c203 into openfoodfoundation:2-0-stable Feb 13, 2019
@luisramos0
Copy link
Contributor

🎉

@mkllnk mkllnk deleted the 3121-spree-gateway-inheritance branch February 18, 2019 22:51
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.

4 participants