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 4, 2023
1 parent 016743c commit 2a6e976
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
7 changes: 1 addition & 6 deletions lib/ancestry/has_ancestry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,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
10 changes: 3 additions & 7 deletions lib/ancestry/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def touch_ancestors_callback
def increase_parent_counter_cache
return if ancestry_callbacks_disabled? || !parent_id

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 @@ -76,24 +76,20 @@ def decrease_parent_counter_cache
return if defined?(@_trigger_destroy_callback) && !@_trigger_destroy_callback
return if ancestry_callbacks_disabled? || !parent_id

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
return if ancestry_callbacks_disabled?
return unless saved_change_to_attribute?(self.ancestry_base_class.ancestry_column)

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 && increase_parent_counter_cache
end

def _counter_cache_column
self.ancestry_base_class.counter_cache_column.to_s
end

# Ancestors

def has_parent?
Expand Down

0 comments on commit 2a6e976

Please sign in to comment.