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

Prevent callback from being set twice. #7376

Merged
merged 1 commit into from Dec 21, 2012
Merged

Prevent callback from being set twice. #7376

merged 1 commit into from Dec 21, 2012

Conversation

dmitriy-kiriyenko
Copy link
Contributor

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.

P.S. It's currently one test on railties failing in master and so in this branch also. It's not me!

 1) Failure:
test_0006_reload constants on development(LoadingTest) [test/application/loading_test.rb:135]:
Expected: "2"
  Actual: "1"

@steveklabnik
Copy link
Member

This will need a rebase.

@jonleighton @tenderlove what do you think about this feature?

@dmitriy-kiriyenko
Copy link
Contributor Author

I've updated the branch with rails/master. Tests pass now.

@carlosantoniodasilva
Copy link
Member

Can you add a changelog entry as well, explaining the issue?

@dmitriy-kiriyenko
Copy link
Contributor Author

Done. Is that ok, or should I be more descriptive?

@carlosantoniodasilva
Copy link
Member

Looks fine I guess, although an example of the issue could be good.

/cc @josevalim @rafaelfranca

@dmitriy-kiriyenko
Copy link
Contributor Author

Done.

@dmitriy-kiriyenko
Copy link
Contributor Author

Can you answer something?

@rafaelfranca
Copy link
Member

This needs a rebase, But seems fine.

@dmitriy-kiriyenko
Copy link
Contributor Author

Done.

@dmitriy-kiriyenko
Copy link
Contributor Author

Come on! Is it fine, or it is not, or you are going in 2 weeks again say it needs a rebase?

@carlosantoniodasilva
Copy link
Member

@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

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.
@dmitriy-kiriyenko
Copy link
Contributor Author

Done.

rafaelfranca added a commit that referenced this pull request Dec 21, 2012
…n-same-statement

Prevent callback from being set twice.

Conflicts:
	activesupport/CHANGELOG.md
@rafaelfranca rafaelfranca merged commit 4a9644a into rails:master Dec 21, 2012
@rafaelfranca
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants