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

alias_attribute Rails deprecation warnings #11977

Closed
fdocr opened this issue Dec 20, 2023 · 3 comments
Closed

alias_attribute Rails deprecation warnings #11977

fdocr opened this issue Dec 20, 2023 · 3 comments

Comments

@fdocr
Copy link
Contributor

fdocr commented Dec 20, 2023

I wanted to create this issue to see if these deprecation warnings are known/expected on Spree.

Context

I first created a draft PR (here) to see how removing alias_attribute :contact_email, :customer_support_email on Spree::Store model would affect the codebase. That wasn't an issue since this alias wasn't used throughout the project and other spree/spree-contrib gems.

CI passed and this got rid of a loud warning message (printed many times on a single endpoint), but others remain that feel a bit trickier compared to "simply removing them". For example:

DEPRECATION WARNING: Spree::Price model aliases `amount` and has a method called `amount=` defined. Starting in Rails 7.2 `price=` will not be calling `amount=` anymore. You may want to additionally define `price=` to preserve the current behavior. (called from find_or_build_default_price at /Users/fdocr/.rvm/gems/ruby-3.2.2/bundler/gems/spree-b3fca9917dc8/core/app/models/concerns/spree/default_price.rb:22)

This PR in Rails has some more context on the alias_attribute deprecation warning.

Expected Behavior

No alias_attribute deprecation warnings should appear

Actual Behavior

Plenty of warnings get printed on screen

Possible Fix

I think there are two ways out:

  1. Simply remove the alias_attribute declaration
    • Would likely break the gem's usage (i.e. Spree::Price#price fetch and assignment methods)
  2. Replace the alias_attribute with an explicit method
    • i.e. def price=(value); amount = price; end

Steps to Reproduce

  1. Start up spree 4.7 app
  2. Query through UI to visualize products/variants/prices/etc
  3. Check the app's development logs for Deprecation Warnings

Your Environment

  • Version used: 4.7
  • Any relevant stack traces ("Full trace" preferred):
18:43:19 web.1  | DEPRECATION WARNING: Spree::User model aliases `bill_address`, but `bill_address` is not an attribute. Starting in Rails 7.2, alias_attribute with non-attribute targets will raise. Use `alias_method :billing_address, :bill_address` or define the method manually. (called from try_spree_current_user at /Users/fdocr/iandi/ruby/spree/core/lib/spree/core/controller_helpers/auth.rb:67)
18:43:19 web.1  | DEPRECATION WARNING: Spree::User model aliases `ship_address`, but `ship_address` is not an attribute. Starting in Rails 7.2, alias_attribute with non-attribute targets will raise. Use `alias_method :shipping_address, :ship_address` or define the method manually. (called from try_spree_current_user at /Users/fdocr/iandi/ruby/spree/core/lib/spree/core/controller_helpers/auth.rb:67)
18:43:20 web.1  | DEPRECATION WARNING: Spree::Price model aliases `amount` and has a method called `amount=` defined. Starting in Rails 7.2 `price=` will not be calling `amount=` anymore. You may want to additionally define `price=` to preserve the current behavior. (called from find_or_build_default_price at /Users/fdocr/iandi/ruby/spree/core/app/models/concerns/spree/default_price.rb:22)
18:43:20 web.1  | DEPRECATION WARNING: Spree::Price model aliases `compare_at_amount` and has a method called `compare_at_amount=` defined. Starting in Rails 7.2 `compare_at_price=` will not be calling `compare_at_amount=` anymore. You may want to additionally define `compare_at_price=` to preserve the current behavior. (called from find_or_build_default_price at /Users/fdocr/iandi/ruby/spree/core/app/models/concerns/spree/default_price.rb:22)
@rafalcymerys
Copy link
Member

Hi @fdocr - that's correct, they were defined for backwards compatibility reasons. We didn't remove these aliases in 4.7, as we didn't want to introduce unnecessary breaking changes that would affect users' codebases. That said, I think the next version of Spree would be a good moment to sort this out :)

These deprecations not a problem at this moment, and such warnings can be suppressed in Rails configuration if they cause too much noise in the logs.

As for the solution - I'm leaning towards removing the aliases. I was planning to go through the aliased attributes one by one and verify if they're used in any meaningful way in both spree and spree-contrib repos. Given that the aliases are quite simple, I think anyone relying on them in their custom code should also be able to easily upgrade.

@fdocr
Copy link
Contributor Author

fdocr commented Dec 22, 2023

That's great to hear @rafalcymerys, thank you for clarifying!

@damianlegawiec
Copy link
Member

All of these were fixed in v4.8!

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

No branches or pull requests

3 participants