diff --git a/lib/patches/ancestry_patch.rb b/lib/patches/ancestry_patch.rb index fb9781261fb..ce65a76c0bd 100644 --- a/lib/patches/ancestry_patch.rb +++ b/lib/patches/ancestry_patch.rb @@ -1,10 +1,27 @@ +module AncestryInstanceMethodsPatch + def update_descendants_with_new_ancestry + super + unless ancestry_callbacks_disabled? + if ancestry_changed? && !new_record? && sane_ancestry? + self.clear_memoized_instance_variables + unscoped_descendants.each(&:clear_memoized_instance_variables) + end + end + end +end + module Ancestry module InstanceMethods + prepend AncestryInstanceMethodsPatch def parse_ancestry_column obj obj.to_s.split('/').map! { |id| cast_primary_key(id) } end + def ancestor_ids + @_ancestor_ids ||= parse_ancestry_column(read_attribute(self.ancestry_base_class.ancestry_column)) + end + STRING_BASED_KEYS = [:string, :uuid, :text].freeze def cast_primary_key(key) if STRING_BASED_KEYS.include? primary_key_type @@ -14,5 +31,9 @@ def cast_primary_key(key) end end + def clear_memoized_instance_variables + @_ancestor_ids = nil + end + end end