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

Define callbacks on descendants. #31913

Merged
merged 2 commits into from Apr 12, 2018

Conversation

Projects
None yet
6 participants
@rywall
Contributor

rywall commented Feb 6, 2018

Summary

We call set_callbacks on all descendants, so we need to make sure that callbacks are also defined on all descendants. Otherwise, you will get an error when running set_callback for a callback that was defined after subclasses were added.

class Foo < ActiveRecord::Base
end

ActiveRecord::Base.define_model_callbacks :greet
ActiveRecord::Base.after_greet { puts 'goodbye' }
=> NoMethodError: undefined method `prepend' for nil:NilClass
@rails-bot

This comment has been minimized.

Show comment
Hide comment
@rails-bot

rails-bot Feb 6, 2018

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @pixeltrix (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

rails-bot commented Feb 6, 2018

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @pixeltrix (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

Define callbacks on descendants.
We set callbacks on all descendants, so we need to make sure that they are also defined on all descendants as well.
@rywall

This comment has been minimized.

Show comment
Hide comment
@rywall

rywall Feb 15, 2018

Contributor

@pixeltrix @rafaelfranca Let me know if I can improve this PR in any way or explain the reasoning behind it a little more clearly. FYI, I tried to provide a concise example of the problem in my initial PR comment.

Contributor

rywall commented Feb 15, 2018

@pixeltrix @rafaelfranca Let me know if I can improve this PR in any way or explain the reasoning behind it a little more clearly. FYI, I tried to provide a concise example of the problem in my initial PR comment.

@matthewd

This comment has been minimized.

Show comment
Hide comment
@matthewd

matthewd Feb 15, 2018

Member

I don't think there's any need to define the methods on the descendants?

Member

matthewd commented Feb 15, 2018

I don't think there's any need to define the methods on the descendants?

@rywall

This comment has been minimized.

Show comment
Hide comment
@rywall

rywall Feb 15, 2018

Contributor

@matthewd Is my test case an invalid use case? I do something similar in my app where some descendants of ActiveRecord::Base are already defined and I'm trying to add a callback to ActiveRecord::Base.

class Foo < ActiveRecord::Base
end

ActiveRecord::Base.define_model_callbacks :greet
ActiveRecord::Base.after_greet { puts 'goodbye' }
=> NoMethodError: undefined method `prepend' for nil:NilClass
Contributor

rywall commented Feb 15, 2018

@matthewd Is my test case an invalid use case? I do something similar in my app where some descendants of ActiveRecord::Base are already defined and I'm trying to add a callback to ActiveRecord::Base.

class Foo < ActiveRecord::Base
end

ActiveRecord::Base.define_model_callbacks :greet
ActiveRecord::Base.after_greet { puts 'goodbye' }
=> NoMethodError: undefined method `prepend' for nil:NilClass
@rywall

This comment has been minimized.

Show comment
Hide comment
@rywall

rywall Feb 15, 2018

Contributor

@matthewd Also, I double checked that the test I wrote fails on the master branch.

Master:
image

This Branch:
image

Contributor

rywall commented Feb 15, 2018

@matthewd Also, I double checked that the test I wrote fails on the master branch.

Master:
image

This Branch:
image

@rywall

This comment has been minimized.

Show comment
Hide comment
@rywall

rywall Feb 16, 2018

Contributor

@matthewd @pixeltrix Also confirmed this is a regression in 5.1/master.

5-0-stable:
image

Contributor

rywall commented Feb 16, 2018

@matthewd @pixeltrix Also confirmed this is a regression in 5.1/master.

5-0-stable:
image

@matthewd

This comment has been minimized.

Show comment
Hide comment
@matthewd

matthewd Feb 20, 2018

Member

I understand the extra set_callbacks calls; I'm questioning the need for the method definitions.

Member

matthewd commented Feb 20, 2018

I understand the extra set_callbacks calls; I'm questioning the need for the method definitions.

@rywall

This comment has been minimized.

Show comment
Hide comment
@rywall

rywall Feb 24, 2018

Contributor

@matthewd Ahh I understand now. Good point. I've updated the PR. What do you think now?

Contributor

rywall commented Feb 24, 2018

@matthewd Ahh I understand now. Good point. I've updated the PR. What do you think now?

@rywall

This comment has been minimized.

Show comment
Hide comment
@rywall

rywall Mar 12, 2018

Contributor

@matthewd @pixeltrix Is there anything else I can do to address your questions/concerns about this PR?

Contributor

rywall commented Mar 12, 2018

@matthewd @pixeltrix Is there anything else I can do to address your questions/concerns about this PR?

@rywall

This comment has been minimized.

Show comment
Hide comment
@rywall

rywall Mar 19, 2018

Contributor

@matthewd @pixeltrix Please let me know if I can help get this PR merged. I could just submit the failing test case if you'd rather fix it in a different way?

Contributor

rywall commented Mar 19, 2018

@matthewd @pixeltrix Please let me know if I can help get this PR merged. I could just submit the failing test case if you'd rather fix it in a different way?

@rywall

This comment has been minimized.

Show comment
Hide comment
@rywall

rywall Apr 9, 2018

Contributor

@matthewd @pixeltrix Hi just checking in on this PR again. Hoping to make a contribution. Would be willing to make any necessary changes.

Contributor

rywall commented Apr 9, 2018

@matthewd @pixeltrix Hi just checking in on this PR again. Hoping to make a contribution. Would be willing to make any necessary changes.

@matthewd

This comment has been minimized.

Show comment
Hide comment
@matthewd

matthewd Apr 12, 2018

Member

Sorry this sat for a while. Looks great, thanks!

Member

matthewd commented Apr 12, 2018

Sorry this sat for a while. Looks great, thanks!

@matthewd matthewd merged commit 77e3b4f into rails:master Apr 12, 2018

1 of 2 checks passed

continuous-integration/travis-ci/pr The Travis CI build failed
Details
codeclimate All good!
Details

@rywall rywall deleted the rywall:define-callbacks-desc branch Apr 12, 2018

@rywall

This comment has been minimized.

Show comment
Hide comment
@rywall

rywall Apr 12, 2018

Contributor

@matthewd Thank you! 💛

Contributor

rywall commented Apr 12, 2018

@matthewd Thank you! 💛

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