Skip to content

Commit

Permalink
Introduce update_strategy=sql hooks
Browse files Browse the repository at this point in the history
This allows additional functionality to implement sql only hooks

depth_cache_column was extracted with thoughts that in the future, it can be be added
only if depth_cache_column is added.
  • Loading branch information
kbrock committed Apr 11, 2023
1 parent dbd23f2 commit c0db14d
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/ancestry/materialized_path_pg.rb
Expand Up @@ -16,17 +16,21 @@ def update_descendants_with_new_ancestry
update_clause[column] = current_time
end

if self.class.respond_to?(:depth_cache_column)
depth_cache_column = self.class.depth_cache_column
depth_change = self.class.ancestry_depth_change(old_ancestry, new_ancestry)
update_descendants_hook(update_clause, old_ancestry, new_ancestry)
unscoped_descendants_before_last_save.update_all update_clause
end
end

if depth_change != 0
update_clause[depth_cache_column] = Arel.sql("#{depth_cache_column} + #{depth_change}")
end
end
def update_descendants_hook(update_clause, old_ancestry, new_ancestry)
if self.class.respond_to?(:depth_cache_column)
depth_cache_column = self.class.depth_cache_column
depth_change = self.class.ancestry_depth_change(old_ancestry, new_ancestry)

unscoped_descendants_before_last_save.update_all update_clause
if depth_change != 0
update_clause[depth_cache_column] = Arel.sql("#{depth_cache_column} + #{depth_change}")
end
end
update_clause
end
end
end

0 comments on commit c0db14d

Please sign in to comment.