Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve mutation changes when updating parent_id #272

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions lib/closure_tree/hierarchy_maintenance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,38 @@ def _ct_after_save
as_5_1 = ActiveSupport.version >= Gem::Version.new('5.1.0')
changes_method = as_5_1 ? :saved_changes : :changes

if public_send(changes_method)[_ct.parent_column_name] || @was_new_record
rebuild!
end
if public_send(changes_method)[_ct.parent_column_name] && !@was_new_record
# Resetting the ancestral collections addresses
# https://github.com/mceachen/closure_tree/issues/68
ancestor_hierarchies.reload
self_and_ancestors.reload
if public_send(changes_method)[_ct.parent_column_name]
_ct_persist_activerecord_state do
if @was_new_record
rebuild!
else
# Resetting the ancestral collections addresses
# https://github.com/mceachen/closure_tree/issues/68
ancestor_hierarchies.reload
self_and_ancestors.reload
end
end
end

@was_new_record = false # we aren't new anymore.
@_ct_skip_sort_order_maintenance = false # only skip once.
true # don't cancel anything.
end

def _ct_persist_activerecord_state &block
tmp_previous_mutation_tracker = @previous_mutation_tracker
tmp_mutation_tracker = @mutation_tracker
tmp_mutations_from_database = @mutations_from_database
tmp_mutations_before_last_save = @mutations_before_last_save

yield block

@previous_mutation_tracker = tmp_previous_mutation_tracker
@mutation_tracker = tmp_mutation_tracker
@mutations_from_database = tmp_mutations_from_database
@mutations_before_last_save = tmp_mutations_before_last_save
end

def _ct_before_destroy
_ct.with_advisory_lock do
delete_hierarchy_references
Expand Down