rails icon indicating copy to clipboard operation
rails copied to clipboard

Type cast #attribute_changed? :from and :to options

Open andrewn617 opened this issue 1 year ago • 0 comments

Patches https://github.com/rails/rails/issues/45766

Summary

This change fixes a defect in ActiveModel::Dirty#attribute_changed? where the :from and :to options are not being type cast. For example, for an enum attribute, :from and :to should handle String, Symbol or Integer, which was broken. The relevant code is in AttributeMutationTracker#changed?. To fix the issue I get the relevant attribute of @attributes and pass the :from and :to values into #type_cast before comparing them to original / previous value of the attribute.

Other Information

It was not clear to me if / how it's possible to typecast values in the ForcedMutationTracker (which is a subclass of AttributeMutationTracker), since in that case there are no @attributes. However, I was also not able to find a way reproduce the defect going down this code path, so I'm not sure there is an issue to solve. So, this is simply a refactoring for ForcedMutationTracker.

For tests, although this code lives in ActiveModel, it seemed to make more sense to test the enum case in the ActiveRecord tests. Let me know if there is an obvious test case I could add to the ActiveModel tests.

andrewn617 avatar Aug 05 '22 15:08 andrewn617