Skip to content

Refactor counter cache create and destroy#14765

Merged
rafaelfranca merged 5 commits intorails:masterfrom
byroot:refactor-counter-cache-create-and-destroy
Apr 15, 2014
Merged

Refactor counter cache create and destroy#14765
rafaelfranca merged 5 commits intorails:masterfrom
byroot:refactor-counter-cache-create-and-destroy

Conversation

@byroot
Copy link
Copy Markdown
Member

@byroot byroot commented Apr 15, 2014

Follow up of #14735

Move destroy and create counter cache callbacks code inside the CounterCache model concern.

The rationale is that the after_destroy callback need to access the affected_rows variable, and it's cleaner to be in the inheritance chain to access it than to leak it as yet another state on the model.

This refactoring do not move the update part of counter caches because Its way more complex and I prefer to do it in another PR.

@tenderlove What do you think?

cc @arthurnn

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add # :nodoc: to remove this method from the public API

@rafaelfranca
Copy link
Copy Markdown
Member

Very good refactoring. I think this is a good direction to take.

@byroot
Copy link
Copy Markdown
Member Author

byroot commented Apr 15, 2014

@rafaelfranca: I addressed both of your style concerns.

And thanks :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# :nodoc: needs to be after the method name or it will remove all the documentation after it. Sorry for not being clear.

def decrement_counters # :nodoc:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad. It's fixed now.

@rafaelfranca
Copy link
Copy Markdown
Member

Could you squash your last two commits? The first four are fine to merge separately but the last two one are only review changes.

rafaelfranca added a commit that referenced this pull request Apr 15, 2014
…nd-destroy

Refactor counter cache create and destroy
@rafaelfranca rafaelfranca merged commit e482696 into rails:master Apr 15, 2014
@byroot
Copy link
Copy Markdown
Member Author

byroot commented Apr 15, 2014

@rafaelfranca I already did it 2 hours ago :p

@byroot byroot deleted the refactor-counter-cache-create-and-destroy branch April 15, 2014 20:21
@rafaelfranca
Copy link
Copy Markdown
Member

Yes. When I commented the PR updated 😄

bquorning added a commit to zendesk/zombie_record that referenced this pull request Aug 24, 2015
Previously we have just overridden the behavior of
`ActiveRecord::Persistence#destroy_row` to apply soft-delete
functionality. This does not work, since Rails depends on inheritance
to add extra functionality to `#destroy_row`. In Rails 4.2, this
includes `ActiveRecord::CounterCache#destroy_row` calling `super`, as
well as `ActiveRecord::Locking::Optimistic#destroy_row` also calling
`super`.

See also

rails/rails#14735
rails/rails#14765
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants