Skip to content

Commit

Permalink
drop _counter_cache_column
Browse files Browse the repository at this point in the history
The value is available, no reason to define a duplicate method
  • Loading branch information
kbrock committed Mar 9, 2023
1 parent 187dc1d commit de525a7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
7 changes: 1 addition & 6 deletions lib/ancestry/has_ancestry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,7 @@ def has_ancestry options = {}
# Create counter cache column accessor and set to option or default
if options[:counter_cache]
cattr_accessor :counter_cache_column

if options[:counter_cache] == true
self.counter_cache_column = :children_count
else
self.counter_cache_column = options[:counter_cache]
end
self.counter_cache_column = options[:counter_cache] == true ? 'children_count' : options[:counter_cache].to_s

after_create :increase_parent_counter_cache, if: :has_parent?
after_destroy :decrease_parent_counter_cache, if: :has_parent?
Expand Down
13 changes: 5 additions & 8 deletions lib/ancestry/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def touch_ancestors_callback

# Counter Cache
def increase_parent_counter_cache
self.ancestry_base_class.increment_counter _counter_cache_column, parent_id
self.ancestry_base_class.increment_counter counter_cache_column, parent_id
end

def decrease_parent_counter_cache
Expand All @@ -86,7 +86,7 @@ def decrease_parent_counter_cache
return if defined?(@_trigger_destroy_callback) && !@_trigger_destroy_callback
return if ancestry_callbacks_disabled?

self.ancestry_base_class.decrement_counter _counter_cache_column, parent_id
self.ancestry_base_class.decrement_counter counter_cache_column, parent_id
end

def update_parent_counter_cache
Expand All @@ -95,14 +95,11 @@ def update_parent_counter_cache
return unless changed

if parent_id_was = parent_id_before_last_save
self.ancestry_base_class.decrement_counter _counter_cache_column, parent_id_was
self.ancestry_base_class.decrement_counter counter_cache_column, parent_id_was
end

parent_id && self.ancestry_base_class.increment_counter(_counter_cache_column, parent_id)
end

def _counter_cache_column
self.ancestry_base_class.counter_cache_column.to_s
def increase_parent_counter_cache
parent_id && increase_parent_counter_cache
end

# Ancestors
Expand Down

0 comments on commit de525a7

Please sign in to comment.