-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
refactor ActiveRecord's #become by removing not needed code #20497
Conversation
Sorry, but this is a cosmetic change. Thanks ❤️ |
The code you proposed is exactly what it was before 5f6370a, so either the test is wrong, or the bug doesn't exist. |
@rafaelfranca maybe the bug existed and the test is ok, but some changes in another parts of the code now prevent the bug from happening. @kaspth actually there is less code, less behaviour and consequently the code is easier to understand and consequently easier to change. I remember some years ago the core team was interested in keeping the code easier to maintain, sorry if this changed, I didn't know. |
Check with the code without warning if the test passes. |
@dcrec1 be nice now 😄 |
Less and simpler code is fine, just cosmetic change is not. |
As I suspected the tests are failing now. So that unnecessary code is just to make sure our code has no warnings and I think it is fine. Do you have other way how to write that code so we don't have the warnings and it is simpler? |
This might do: became.instance_variable_set(:@changed_attributes, defined?(@changed_attributes) ? @changed_attributes : {}) |
I have another way but it actually changes some behaviour. The change would be removing the
In case this |
If removing the freeze it will work means that we are mutating an object that should not be mutated, so I'd say it may be a dangerous change. |
I've tracked this change to this commit. Couldn't really understand why the |
The reason was exactly to force people to not change that object directly and use the right methods because direct changes of it do not reflect the expected behavior. |
Anyway, after better analyzing and understanding the code, I changed the line to be like:
This new way doesn't throw warning neither removes the freeze and I think it's better than Is this ok? |
But will not this calculate the changed attributes for the case where |
In this case the original |
I see. Yeah that would work, but it will be so much more indirection and the code would be hard to follow. Not sure if it is an improvement over the current code. To avoid one |
But for understanding what this method is doing it shouldn't be necessary to deep into the stack. I think that in this case the variable is well named and explains what it represents and that is very clear that the code is passing the changed attributes to the new class. I agree that the underlying code could be better, maybe |
I'll defer this to @sgrif since he wrote the original code. |
BTW, your solution looks good to me, so if he don't have anything against I'll merge it using |
Sorry I sent a wrong commit, will send again. |
Thank. Lets wait @sgrif review |
refactor ActiveRecord's #become by removing not needed code
This refactor throw some warnings when running the tests:
If this is a problem, I can change the line to be like this: