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

Add ApplicationRecord for customisations of models #7425

Merged
merged 4 commits into from
Apr 21, 2021
Merged

Add ApplicationRecord for customisations of models #7425

merged 4 commits into from
Apr 21, 2021

Conversation

mkllnk
Copy link
Member

@mkllnk mkllnk commented Apr 15, 2021

What? Why?

Rails 5 introduced this new class to confine application-specific monkey patches to our models only, and not leak into other libraries using ActiveRecord::Base.

For context: I was adding a new model for #6328 and noticed that Rails generated the ApplicationRecord class. I looked up the story behind it and found it quite useful.

https://bigbinary.com/blog/application-record-in-rails-5

Bonus: 🔥 +78 −162 lines of code

What should we test?

  • Sign up and create an enterprise.
  • Open a shop (create an order cycle).
  • Make sure that you can visit the shop and that the URL matches the enterprise name.

Release notes

Changelog Category: Technical changes

We now use the Rails convention to inherit all models from ApplicationRecord. App-specific patches now apply only to ApplicationRecord instead of ActiveRecord::Base. This should lead to fewer unintended side effects.

@mkllnk mkllnk self-assigned this Apr 15, 2021
@codecov
Copy link

codecov bot commented Apr 15, 2021

Codecov Report

Merging #7425 (2a26ae7) into master (0c182c4) will increase coverage by 0.01%.
The diff coverage is 100.00%.

❗ Current head 2a26ae7 differs from pull request most recent head 5c0cdce. Consider uploading reports for the commit 5c0cdce to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master    #7425      +/-   ##
==========================================
+ Coverage   93.07%   93.08%   +0.01%     
==========================================
  Files         633      634       +1     
  Lines       18144    18142       -2     
==========================================
  Hits        16888    16888              
+ Misses       1256     1254       -2     
Impacted Files Coverage Δ
lib/spree/core/delegate_belongs_to.rb 81.08% <ø> (-0.50%) ⬇️
lib/spree/core/permalinks.rb 87.09% <ø> (+3.31%) ⬆️
app/models/adjustment_metadata.rb 100.00% <100.00%> (ø)
app/models/application_record.rb 100.00% <100.00%> (ø)
app/models/column_preference.rb 100.00% <100.00%> (ø)
app/models/coordinator_fee.rb 100.00% <100.00%> (ø)
app/models/customer.rb 100.00% <100.00%> (ø)
app/models/distributor_shipping_method.rb 100.00% <100.00%> (ø)
app/models/enterprise.rb 97.23% <100.00%> (ø)
app/models/enterprise_fee.rb 100.00% <100.00%> (ø)
... and 64 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0c182c4...5c0cdce. Read the comment docs.

mkllnk added 4 commits April 15, 2021 15:59
Rails 5 introduced this new class to confine application-specific monkey
patches to our models only, and not leak into other libraries using
ActiveRecord::Base.

https://bigbinary.com/blog/application-record-in-rails-5
We were also patching ActiveRecord::Relation for the `#find_by_param`
methods but we are not using those any more. They were deprecated a
while ago. We now use `find_by(permalink: ...)`.
Copy link
Contributor

@Matt-Yorkley Matt-Yorkley left a comment

Choose a reason for hiding this comment

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

Awesome 💯

@@ -70,6 +62,3 @@ def save_permalink(permalink_value = to_param)
end
end
end

ActiveRecord::Base.include(Spree::Core::Permalinks)
ActiveRecord::Relation.include(Spree::Core::Permalinks)
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if not including it in ActiveRecord::Relation will have any consequences..?

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 wondered that, too. But I think that the reason for this were the find_by_param methods. Spree could do something like Product.in_stock.find_by_param. But we don't use those any more and I removed them. I couldn't find any usage of the other methods on scopes.

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.

nice 👍

@filipefurtad0 filipefurtad0 self-assigned this Apr 20, 2021
@filipefurtad0 filipefurtad0 added pr-staged-au staging.openfoodnetwork.org.au and removed pr-staged-au staging.openfoodnetwork.org.au labels Apr 20, 2021
@filipefurtad0
Copy link
Contributor

filipefurtad0 commented Apr 21, 2021

Hey @mkllnk ,

Thank you for the notes on what to test.

I followed your suggestions, and verified:

  • signed up and created an enterprise - OK

  • opened a shop (create an order cycle) - OK

  • make sure that you can visit the shop and that the URL matches the enterprise name - OK
    The shop URL as seen by the customer matches the URL and permalink for the shop, as displayed on the admin section, under Dashboard -> Primary details -> Link to shop front

I noticed this PR touches many files! So I thought I just run some quick additional tests, like:

  • creating products with stock
  • adding them to the order cycle
  • creating shipment and payment methods
  • checking out as a customer
  • checking order states
  • capturing (cash) payments

Looks good and ready to go 👍

@filipefurtad0 filipefurtad0 removed the pr-staged-au staging.openfoodnetwork.org.au label Apr 21, 2021
@Matt-Yorkley Matt-Yorkley merged commit 6dc2380 into openfoodfoundation:master Apr 21, 2021
@mkllnk mkllnk deleted the application-record branch July 15, 2021 03:14
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