From de525a7bbb36b38ed9d2ffd0240f993a9717f86b Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Fri, 3 Mar 2023 20:59:40 -0500 Subject: [PATCH] drop _counter_cache_column The value is available, no reason to define a duplicate method --- lib/ancestry/has_ancestry.rb | 7 +------ lib/ancestry/instance_methods.rb | 13 +++++-------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/lib/ancestry/has_ancestry.rb b/lib/ancestry/has_ancestry.rb index 87ebc1ed..c30801d2 100644 --- a/lib/ancestry/has_ancestry.rb +++ b/lib/ancestry/has_ancestry.rb @@ -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? diff --git a/lib/ancestry/instance_methods.rb b/lib/ancestry/instance_methods.rb index ebacb87d..64048622 100644 --- a/lib/ancestry/instance_methods.rb +++ b/lib/ancestry/instance_methods.rb @@ -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 @@ -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 @@ -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