lib/eco/data/locations/node_diff/nodes_diff.rb in eco-helpers-2.7.4 vs lib/eco/data/locations/node_diff/nodes_diff.rb in eco-helpers-2.7.12

- old
+ new

@@ -19,10 +19,11 @@ attr_reader :original_tree def initialize(*args, original_tree:, **kargs, &block) super(*args, **kargs, &block) @original_tree = original_tree + mark_implicit_unarchive! end def diffs @diffs ||= super.select do |dff| # discard entries that are to be inserted and archived at the same time @@ -93,15 +94,44 @@ " • #{count} nodes to archive" end msg.compact.join("\n") end + alias_method :unarchive_src, :unarchive + + # @note we must unarchive destination parents that will get + # some children as well + def unarchive + unarchive_src | @implicit_unarchive + end + private def when_present(list, default = nil) raise ArgumentError, "Expecting block but not given" unless block_given? count = list.count return yield(count) if count&.positive? default + end + + def mark_implicit_unarchive! + @implicit_unarchive = source_results.select do |result| + result.archived_prev && implicit_unarchive?(result) + end.tap do |results| + results.each(&:unarchive!) + end + end + + def implicit_unarchive?(result) + [result.node_id, result.node_id_prev].compact.any? do |id| + target_parent_ids.include?(id) + end + end + + def target_parent_ids + return @target_parent_ids if instance_variable_defined?(:@target_parent_ids) + + @target_parent_ids = insert.map(&:parent_id).uniq + @target_parent_ids |= move.map(&:parent_id) end end end