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
Prevent callback from being set twice. #7376
Prevent callback from being set twice. #7376
Conversation
This will need a rebase. @jonleighton @tenderlove what do you think about this feature? |
I've updated the branch with rails/master. Tests pass now. |
Can you add a changelog entry as well, explaining the issue? |
Done. Is that ok, or should I be more descriptive? |
Looks fine I guess, although an example of the issue could be good. |
Done. |
Can you answer something? |
This needs a rebase, But seems fine. |
Done. |
Come on! Is it fine, or it is not, or you are going in 2 weeks again say it needs a rebase? |
@dmitriy-kiriyenko please be patient, we have lots of issues to review and give feedback. I'll review yours again later if anyone else does before me, it's in my todo list. Thanks for your work on that. |
@@ -607,6 +607,45 @@ def save | |||
end | |||
end | |||
|
|||
class OneThoThreeSave |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be OneTwoThreeSave
?
When you add one callack in two separate `set_callback` calls - it is only called once. When you do it in one `set_callback` call - it is called twice. This violates the principle of least astonishment for me. Duplicating callback is usually an error. There is a correct and obvious way to do anything without this "feature". If you want to do before_save :clear_balance, :calculate_tax, :clear_balance or whatever, you should better do before_save :carefully_calculate_tax def carefully_calculate_tax clear_balance calculate_tax clear_balance end And this even opens gates for some advanced refactorings, unlike the first approach. My assumptions are: - Principle of least astonishment is violated, when callbacks are either prevented from duplication, or not. - Duplicating callbacks is usually an error. When it is intentional - it's a smell of a bad design and can be approached without abusing this "feature". My suggestion is: do not allow duplicating callbacks in one callback call, like it is not allowed in separate callbacks call.
Done. |
…n-same-statement Prevent callback from being set twice. Conflicts: activesupport/CHANGELOG.md
Thanks and sorry for the delay. As @carlosantoniodasilva said, we have a lot of things to do and sometimes things can take a little longer than expected. |
When you add one callack in two separate
set_callback
calls - it is only called once.When you do it in one
set_callback
call - it is called twice.This violates the principle of least astonishment for me. Duplicating callback is usually an error. There is a correct and obvious way to do anything without this "feature".
If you want to do:
or whatever, you should better do:
And this even opens gates for some advanced refactorings, unlike the first approach.
My assumptions are:
My suggestion is: do not allow duplicating callbacks in one callback call, like it is not allowed in separate callbacks call.
P.S. It's currently one test on railties failing in master and so in this branch also. It's not me!